diff --git a/chromadb/api/models/CollectionCommon.py b/chromadb/api/models/CollectionCommon.py index d2b3cd3789e..675edb2f637 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 ab91408c992..c4c17f42283 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 @@ -735,6 +736,41 @@ def test_where_validation_query(client): 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, request and response) + 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) + + with pytest.raises( + httpx.HTTPStatusError, match="Some HTTP error in add." + ) as exc_info: + # This should trigger the validation_context wrapper + collection.add(ids=["test1"], documents=["test document"]) + + # Verify the original keyword arguments are preserved + assert exc_info.value.response == mock_response + assert exc_info.value.request == mock_request + + operator_records = { "embeddings": [[1.1, 2.3, 3.2], [1.2, 2.24, 3.2]], "ids": ["id1", "id2"],