Skip to content

Commit 805a786

Browse files
Python: chroma - catch NotFoundError directly and wrap other errors
The connector only imports on chromadb 1.x (it needs chromadb.api.collection_configuration), so the ValueError fallback for chromadb < 1.0 could never run; it is removed, with the two tests it gated. collection_exists now wraps any other error in VectorStoreOperationException, matching ensure_collection_deleted here and the Cosmos NoSQL change in #14515.
1 parent 49ef924 commit 805a786

2 files changed

Lines changed: 19 additions & 38 deletions

File tree

‎python/semantic_kernel/connectors/chroma.py‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111
from chromadb.api.collection_configuration import CreateCollectionConfiguration, CreateHNSWConfiguration
1212
from chromadb.api.types import EmbeddingFunction, Space
1313
from chromadb.config import Settings
14-
15-
try: # chromadb >= 1.0 raises NotFoundError for a missing collection
16-
from chromadb.errors import NotFoundError as _ChromaNotFoundError
17-
18-
COLLECTION_NOT_FOUND: tuple[type[Exception], ...] = (_ChromaNotFoundError,)
19-
except ImportError: # chromadb < 1.0 had no NotFoundError and raised ValueError
20-
COLLECTION_NOT_FOUND = (ValueError,)
14+
from chromadb.errors import NotFoundError
2115

2216
from semantic_kernel.connectors.ai.embedding_generator_base import EmbeddingGeneratorBase
2317
from semantic_kernel.data.vector import (
@@ -138,14 +132,19 @@ async def collection_exists(self, **kwargs: Any) -> bool:
138132
"""Check if the collection exists.
139133
140134
Returns False only when Chroma says the collection is not there. Anything else -
141-
an unreachable server, a rejected request - is raised, because the caller cannot
142-
tell it apart from an absent collection otherwise.
135+
an unreachable server, a rejected request - is raised as a
136+
VectorStoreOperationException, because the caller cannot tell it apart from an
137+
absent collection otherwise.
143138
"""
144139
try:
145140
self.client.get_collection(name=self.collection_name, embedding_function=self.embedding_func)
146141
return True
147-
except COLLECTION_NOT_FOUND:
142+
except NotFoundError:
148143
return False
144+
except Exception as e:
145+
raise VectorStoreOperationException(
146+
f"Failed to check whether collection {self.collection_name} exists with error: {e}"
147+
) from e
149148

150149
@override
151150
async def ensure_collection_exists(self, **kwargs: Any) -> None:
@@ -196,7 +195,7 @@ async def ensure_collection_deleted(self, **kwargs: Any) -> None:
196195
"""Delete the collection."""
197196
try:
198197
self.client.delete_collection(name=self.collection_name)
199-
except COLLECTION_NOT_FOUND:
198+
except NotFoundError:
200199
logger.info(f"Collection {self.collection_name} could not be deleted because it doesn't exist.")
201200
except Exception as e:
202201
raise VectorStoreOperationException(

‎python/tests/unit/connectors/memory/test_chroma.py‎

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,15 @@
55
import pytest
66
from chromadb.api import ClientAPI
77
from chromadb.api.models.Collection import Collection
8+
from chromadb.errors import NotFoundError
89

910
from semantic_kernel.connectors.chroma import ChromaCollection, ChromaStore
1011
from semantic_kernel.exceptions.vector_store_exceptions import VectorStoreOperationException
1112

12-
try: # mirrors the connector, resolved here so these tests do not depend on it
13-
from chromadb.errors import NotFoundError as _ChromaNotFoundError
14-
15-
COLLECTION_NOT_FOUND_IS_VALUE_ERROR = False
16-
except ImportError: # chromadb < 1.0
17-
_ChromaNotFoundError = ValueError
18-
COLLECTION_NOT_FOUND_IS_VALUE_ERROR = True
19-
2013

2114
def _not_found() -> Exception:
22-
"""The error Chroma raises for a missing collection, on whichever version is installed.
23-
24-
Resolved here rather than imported from the connector, so these tests run unchanged
25-
against the code before and after the fix.
26-
"""
27-
return _ChromaNotFoundError("Collection test_collection does not exist.")
15+
"""The error Chroma raises for a missing collection."""
16+
return NotFoundError("Collection test_collection does not exist.")
2817

2918

3019
@pytest.fixture
@@ -135,9 +124,9 @@ async def test_chroma_collection_search(chroma_collection, mock_client, include_
135124

136125
# -- what a Chroma failure means, and what "not there" means --
137126
#
138-
# chromadb changed the error for a missing collection inside the supported range
139-
# (chromadb >= 0.5, < 1.6): 0.5 raised ValueError, 1.0 raises errors.NotFoundError,
140-
# which is not a ValueError. collection_exists caught bare Exception, so every failure
127+
# chromadb 1.x raises errors.NotFoundError for a missing collection, which is not a
128+
# ValueError (this connector only imports on chromadb 1.x: it needs
129+
# chromadb.api.collection_configuration). collection_exists caught bare Exception, so every failure
141130
# read as "absent"; ensure_collection_deleted caught only ValueError, so on a modern
142131
# chromadb a genuinely absent collection raised instead of being tolerated.
143132

@@ -152,8 +141,9 @@ async def test_collection_exists_raises_when_the_server_cannot_be_reached(chroma
152141
"""False means the collection is not there. A refused connection must not say that."""
153142
mock_client.get_collection.side_effect = ConnectionError("connection refused")
154143

155-
with pytest.raises(ConnectionError):
144+
with pytest.raises(VectorStoreOperationException) as raised:
156145
await chroma_collection.collection_exists()
146+
assert isinstance(raised.value.__cause__, ConnectionError)
157147

158148

159149
async def test_ensure_collection_deleted_tolerates_an_absent_collection(chroma_collection, mock_client):
@@ -170,22 +160,14 @@ async def test_ensure_collection_deleted_still_raises_on_a_real_failure(chroma_c
170160
await chroma_collection.ensure_collection_deleted()
171161

172162

173-
@pytest.mark.skipif(
174-
COLLECTION_NOT_FOUND_IS_VALUE_ERROR,
175-
reason="on chromadb < 1.0 a ValueError IS the missing-collection signal",
176-
)
177163
async def test_collection_exists_raises_on_an_unrelated_value_error(chroma_collection, mock_client):
178164
"""Where Chroma has a dedicated NotFoundError, a ValueError is not 'absent'."""
179165
mock_client.get_collection.side_effect = ValueError("some other problem")
180166

181-
with pytest.raises(ValueError):
167+
with pytest.raises(VectorStoreOperationException):
182168
await chroma_collection.collection_exists()
183169

184170

185-
@pytest.mark.skipif(
186-
COLLECTION_NOT_FOUND_IS_VALUE_ERROR,
187-
reason="on chromadb < 1.0 a ValueError IS the missing-collection signal",
188-
)
189171
async def test_ensure_collection_deleted_does_not_swallow_an_unrelated_value_error(chroma_collection, mock_client):
190172
"""The more dangerous half: a swallowed ValueError here would silently skip a delete."""
191173
mock_client.delete_collection.side_effect = ValueError("some other problem")

0 commit comments

Comments
 (0)