Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better error messages for mistaken from_texts and from_documents #342

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pinecone/control/langchain_import_warnings.py
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}
"""

8 changes: 8 additions & 0 deletions pinecone/control/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ConfigureIndexRequestSpecPod
)
from pinecone.models import ServerlessSpec, PodSpec, IndexList, CollectionList
from .langchain_import_warnings import _build_langchain_attribute_error_message

from pinecone.data import Index

Expand Down Expand Up @@ -545,6 +546,13 @@ def _get_status(self, name: str):
response = api_instance.describe_index(name)
return response["status"]

@staticmethod
def from_texts(*args, **kwargs):
raise AttributeError(_build_langchain_attribute_error_message("from_texts"))

@staticmethod
def from_documents(*args, **kwargs):
raise AttributeError(_build_langchain_attribute_error_message("from_documents"))

def Index(self, name: str = '', host: str = '', **kwargs):
"""
Expand Down
3 changes: 2 additions & 1 deletion pinecone/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from .fix_tuple_length import fix_tuple_length
from .convert_to_list import convert_to_list
from .normalize_host import normalize_host
from .setup_openapi_client import setup_openapi_client
from .setup_openapi_client import setup_openapi_client
from .docslinks import docslinks
4 changes: 4 additions & 0 deletions pinecone/utils/docslinks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docslinks = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, good thinking. 👍

'GITHUB_REPO': 'https://github.com/pinecone-io/pinecone-python-client',
'LANGCHAIN_IMPORT_KB_ARTICLE': 'https://docs.pinecone.io/troubleshooting/pinecone-attribute-errors-with-langchain'
}
19 changes: 19 additions & 0 deletions tests/unit/test_langchain_helpful_errors.py
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)

10 changes: 10 additions & 0 deletions tests/unit/utils/test_docs_links.py
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}"