-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better error messages for mistaken
from_texts
and `from_documen…
…ts` (#342) ## Problem Sometimes people using Pinecone with Langchain accidentally try to invoke `from_texts` and `from_documents` methods on our `Pinecone` class even though these are not methods by us. This is mostly name-related confusion because Langchain used to have an export called `Pinecone` that later got renamed to the less ambiguous `PineconeVectorStore`. ## Solution Add some stub methods to the `Pinecone` class to guide users in the right direction. <img width="920" alt="Screenshot 2024-05-13 at 2 36 51 PM" src="https://github.com/pinecone-io/pinecone-python-client/assets/1326365/09bfa6bf-b307-43e9-8756-bcf807b0e3b0"> ## Type of Change - [x] New feature (non-breaking change which adds functionality) ## Test Plan Added unit tests. And also an integration test to verify the docs link resolves to a valid page.
- Loading branch information
Showing
6 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from pinecone.utils import docslinks | ||
|
||
KB_ARTICLE = docslinks['LANGCHAIN_IMPORT_KB_ARTICLE'] | ||
GITHUB_REPO = docslinks['GITHUB_REPO'] | ||
|
||
def _build_langchain_attribute_error_message(method_name: str): | ||
return f"""{method_name} is not a top-level attribute of the Pinecone class provided by pinecone's official python package developed at {GITHUB_REPO}. You may have a name collision with an export from another dependency in your project that wraps Pinecone functionality and exports a similarly named class. Please refer to the following knowledge base article for more information: {KB_ARTICLE} | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
docslinks = { | ||
'GITHUB_REPO': 'https://github.com/pinecone-io/pinecone-python-client', | ||
'LANGCHAIN_IMPORT_KB_ARTICLE': 'https://docs.pinecone.io/troubleshooting/pinecone-attribute-errors-with-langchain' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
from pinecone import Pinecone | ||
|
||
class TestLangchainErrorMessages(): | ||
def test_error_from_texts_positional_args(self): | ||
with pytest.raises(AttributeError) as e: | ||
Pinecone.from_texts("texts", "id") | ||
assert "from_texts is not a top-level attribute of the Pinecone class" in str(e.value) | ||
|
||
def test_error_from_texts_kwargs(self): | ||
with pytest.raises(AttributeError) as e: | ||
Pinecone.from_texts(foo="texts", bar="id", num_threads=1) | ||
assert "from_texts is not a top-level attribute of the Pinecone class" in str(e.value) | ||
|
||
def test_error_from_documents(self): | ||
with pytest.raises(AttributeError) as e: | ||
Pinecone.from_documents("documents", "id") | ||
assert "from_documents is not a top-level attribute of the Pinecone class" in str(e.value) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
import requests | ||
from pinecone.utils import docslinks | ||
|
||
urls = list(docslinks.values()) | ||
|
||
@pytest.mark.parametrize("url", urls) | ||
def test_valid_links(url): | ||
response = requests.get(url) | ||
assert response.status_code == 200, f"Docs link is invalid: {url}" |