Skip to content

Commit

Permalink
Add better error messages for from_texts and from_documents
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed May 13, 2024
1 parent 06c69fb commit 00d46b5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pinecone/control/langchain_import_warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pinecone.utils import docslinks

KB_ARTICLE = docslinks['LANGCHAIN_IMPORT_KB_ARTICLE']

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 https://github.com/pinecone-io/pinecone-python-client. 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
3 changes: 3 additions & 0 deletions pinecone/utils/docslinks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docslinks = {
'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}"

0 comments on commit 00d46b5

Please sign in to comment.