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

fix: skip unsupported meta fields in PineconeDB #1009

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@


DEFAULT_STARTER_PLAN_SPEC = {"serverless": {"region": "us-east-1", "cloud": "aws"}}
METADATA_SUPPORTED_TYPES = str, int, bool # List[str] is supported and checked separately
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved


class PineconeDocumentStore:
Expand Down Expand Up @@ -295,6 +296,29 @@ def _convert_query_result_to_documents(self, query_result: Dict[str, Any]) -> Li

return documents

@staticmethod
def check_metadata(document: Document):
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved
def valid_type(value: Any):
return isinstance(value, METADATA_SUPPORTED_TYPES) or (
isinstance(value, list) and all(isinstance(i, str) for i in value)
)

if document.meta:
discarded_keys = []
for key, value in document.meta.items():
if not valid_type(value):
discarded_keys.append(key)
document.meta[key] = "IGNORED"
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved

if discarded_keys:
msg = (
f"Document {document.id} has metadata fields with unsupported types: {discarded_keys}. "
f"Only str, int, bool, and List[str] are supported. The values of these fields will be ignored."
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved
)
logger.warning(msg)

return document

def _convert_documents_to_pinecone_format(self, documents: List[Document]) -> List[Dict[str, Any]]:
documents_for_pinecone = []
for document in documents:
Expand All @@ -305,6 +329,10 @@ def _convert_documents_to_pinecone_format(self, documents: List[Document]) -> Li
"A dummy embedding will be used, but this can affect the search results. "
)
embedding = self._dummy_vector

if document.meta:
self.check_metadata(document)

doc_for_pinecone = {"id": document.id, "values": embedding, "metadata": dict(document.meta)}

# we save content/dataframe as metadata
Expand Down
36 changes: 36 additions & 0 deletions integrations/pinecone/tests/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,42 @@ def test_convert_dict_spec_to_pinecone_object_fail():
PineconeDocumentStore._convert_dict_spec_to_pinecone_object(dict_spec)


def test_check_metadata_invalid():
invalid_metadata_doc = Document(
content="The moonlight shimmered ",
meta={
"source_id": "62049ba1d1e1d5ebb1f6230b0b00c5356b8706c56e0b9c36b1dfc86084cd75f0",
"page_number": 1,
"split_id": 0,
"split_idx_start": 0,
"_split_overlap": [
{"doc_id": "68ed48ba830048c5d7815874ed2de794722e6d10866b6c55349a914fd9a0df65", "range": (0, 20)}
],
},
)
pinecone_doc = PineconeDocumentStore.check_metadata(invalid_metadata_doc)

assert pinecone_doc.meta["source_id"] == "62049ba1d1e1d5ebb1f6230b0b00c5356b8706c56e0b9c36b1dfc86084cd75f0"
assert pinecone_doc.meta["page_number"] == 1
assert pinecone_doc.meta["split_id"] == 0
assert pinecone_doc.meta["split_idx_start"] == 0
assert pinecone_doc.meta["_split_overlap"] == "IGNORED"


def test_check_metadata_valid():
valid_metadata_doc = Document(
content="The moonlight shimmered ",
meta={
"source_id": "62049ba1d1e1d5ebb1f6230b0b00c5356b8706c56e0b9c36b1dfc86084cd75f0",
"page_number": 1,
},
)
pinecone_doc = PineconeDocumentStore.check_metadata(valid_metadata_doc)

assert pinecone_doc.meta["source_id"] == "62049ba1d1e1d5ebb1f6230b0b00c5356b8706c56e0b9c36b1dfc86084cd75f0"
assert pinecone_doc.meta["page_number"] == 1


@pytest.mark.integration
@pytest.mark.skipif("PINECONE_API_KEY" not in os.environ, reason="PINECONE_API_KEY not set")
def test_serverless_index_creation_from_scratch(sleep_time):
Expand Down