diff --git a/integrations/chroma/src/haystack_integrations/document_stores/chroma/document_store.py b/integrations/chroma/src/haystack_integrations/document_stores/chroma/document_store.py index d39158db4..49cfced2e 100644 --- a/integrations/chroma/src/haystack_integrations/document_stores/chroma/document_store.py +++ b/integrations/chroma/src/haystack_integrations/document_stores/chroma/document_store.py @@ -428,8 +428,12 @@ def _query_result_to_documents(result: QueryResult) -> List[List[Document]]: } # prepare metadata - if metadatas := result.get("metadatas"): - document_dict["meta"] = dict(metadatas[i][j]) + metadatas = result.get("metadatas") + try: + if metadatas and metadatas[i][j] is not None: + document_dict["meta"] = metadatas[i][j] + except IndexError: + pass if embeddings := result.get("embeddings"): document_dict["embedding"] = np.array(embeddings[i][j]) diff --git a/integrations/chroma/tests/test_document_store.py b/integrations/chroma/tests/test_document_store.py index 4e1181ae2..774096a15 100644 --- a/integrations/chroma/tests/test_document_store.py +++ b/integrations/chroma/tests/test_document_store.py @@ -92,6 +92,21 @@ def test_delete_not_empty_nonexisting(self, document_store: ChromaDocumentStore) assert document_store.filter_documents(filters={"id": doc.id}) == [doc] + def test_search(self): + document_store = ChromaDocumentStore() + documents = [ + Document(content="First document", meta={"author": "Author1"}), + Document(content="Second document"), # No metadata + Document(content="Third document", meta={"author": "Author2"}), + Document(content="Fourth document"), # No metadata + ] + document_store.write_documents(documents) + result = document_store.search(["Third"], top_k=1) + + # Assertions to verify correctness + assert len(result) == 1 + assert result[0][0].content == "Third document" + @pytest.mark.integration def test_to_json(self, request): ds = ChromaDocumentStore(