diff --git a/pinecone/control/langchain_import_warnings.py b/pinecone/control/langchain_import_warnings.py new file mode 100644 index 000000000..fb4a00cac --- /dev/null +++ b/pinecone/control/langchain_import_warnings.py @@ -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} +""" + diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index 3fed74a59..2de6cd4e5 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -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 @@ -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): """ diff --git a/pinecone/utils/__init__.py b/pinecone/utils/__init__.py index 27846fcad..565992935 100644 --- a/pinecone/utils/__init__.py +++ b/pinecone/utils/__init__.py @@ -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 \ No newline at end of file +from .setup_openapi_client import setup_openapi_client +from .docslinks import docslinks \ No newline at end of file diff --git a/pinecone/utils/docslinks.py b/pinecone/utils/docslinks.py new file mode 100644 index 000000000..aa2ba24d5 --- /dev/null +++ b/pinecone/utils/docslinks.py @@ -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' +} \ No newline at end of file diff --git a/tests/unit/test_langchain_helpful_errors.py b/tests/unit/test_langchain_helpful_errors.py new file mode 100644 index 000000000..006572c32 --- /dev/null +++ b/tests/unit/test_langchain_helpful_errors.py @@ -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) + diff --git a/tests/unit/utils/test_docs_links.py b/tests/unit/utils/test_docs_links.py new file mode 100644 index 000000000..deb9f5120 --- /dev/null +++ b/tests/unit/utils/test_docs_links.py @@ -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}" \ No newline at end of file