fix(vector_stores): return None from ChromaDB.get() for missing IDs (#5561)

Co-authored-by: ly-wang19 <ly-wang19@users.noreply.github.com>
This commit is contained in:
ly-wang19
2026-06-15 18:41:03 +08:00
committed by GitHub
parent b90e3c0b76
commit 3ef034a9e4
2 changed files with 14 additions and 3 deletions
+4 -3
View File
@@ -187,7 +187,7 @@ class ChromaDB(VectorStoreBase):
"""
self.collection.update(ids=vector_id, embeddings=vector, metadatas=payload)
def get(self, vector_id: str) -> OutputData:
def get(self, vector_id: str) -> Optional[OutputData]:
"""
Retrieve a vector by ID.
@@ -195,10 +195,11 @@ class ChromaDB(VectorStoreBase):
vector_id (str): ID of the vector to retrieve.
Returns:
OutputData: Retrieved vector.
Optional[OutputData]: Retrieved vector, or None if the ID is not found.
"""
result = self.collection.get(ids=[vector_id])
return self._parse_output(result)[0]
parsed = self._parse_output(result)
return parsed[0] if parsed else None
def list_cols(self) -> List[chromadb.Collection]:
"""
+10
View File
@@ -154,6 +154,16 @@ def test_get_vector(chromadb_instance):
assert result.payload == {"name": "vector1"}
def test_get_missing_vector_returns_none(chromadb_instance):
# Chroma returns empty lists for an unknown id; get() must return None
# rather than raising IndexError (parity with qdrant/pgvector/faiss).
chromadb_instance.collection.get.return_value = {"ids": [], "metadatas": []}
result = chromadb_instance.get(vector_id="does-not-exist")
assert result is None
def test_list_vectors(chromadb_instance):
mock_result = {
"ids": [["id1", "id2"]],