diff --git a/chromadb/api/models/CollectionCommon.py b/chromadb/api/models/CollectionCommon.py index d2b3cd3789ed..675edb2f6370 100644 --- a/chromadb/api/models/CollectionCommon.py +++ b/chromadb/api/models/CollectionCommon.py @@ -89,8 +89,11 @@ def wrapper(self: Any, *args: Any, **kwargs: Any) -> T: try: return func(self, *args, **kwargs) except Exception as e: - msg = f"{str(e)} in {name}." - raise type(e)(msg).with_traceback(e.__traceback__) + if e.args: + e.args = (f"{e.args[0]} in {name}.",) + e.args[1:] + else: + e.args = (f"{type(e)} in {name}.",) + raise return wrapper diff --git a/chromadb/test/test_api.py b/chromadb/test/test_api.py index ab91408c992a..0a0c0d982090 100644 --- a/chromadb/test/test_api.py +++ b/chromadb/test/test_api.py @@ -1,6 +1,7 @@ # type: ignore import traceback import httpx +import json import chromadb from chromadb.errors import ChromaError @@ -734,6 +735,47 @@ def test_where_validation_query(client): with pytest.raises(ValueError, match="where"): collection.query(query_embeddings=[0, 0, 0], where={"value": {"nested": "5"}}) +def test_validation_context(client): + """Test that the validation_context decorator properly re-raises exceptions + with keyword-only arguments""" + client.reset() + + mock_request = httpx.Request("POST", "https://embedding.com") + mock_response = httpx.Response( + status_code=500, + content=json.dumps({"error": "test error"}).encode(), + request=mock_request + ) + + # An error with keyword-only arguments (namely, response and request) + ef_error = httpx.HTTPStatusError( + message="Some HTTP error", + request=mock_request, + response=mock_response + ) + + class MockEmbeddingFunction: + def __call__(self, input): + raise ef_error + + ef = MockEmbeddingFunction() + collection = client.create_collection("test", embedding_function=ef) + + try: + # This should trigger the validation_context wrapper + collection.add( + ids=["test1"], + documents=["test document"] + ) + pytest.fail("Expected exception was not raised") + # Ensures that we can still catch the original error type + except httpx.HTTPStatusError as e: + # Verify the error message was properly augmented + assert "Some HTTP error in add" in str(e) + # Verify the original keyword arguments are preserved + assert e.response == mock_response + assert e.request == mock_request + operator_records = { "embeddings": [[1.1, 2.3, 3.2], [1.2, 2.24, 3.2]],