Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] fix reraise from validation_context (#3233) #3235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions chromadb/api/models/CollectionCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions chromadb/test/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# type: ignore
import traceback
import httpx
import json

import chromadb
from chromadb.errors import ChromaError
Expand Down Expand Up @@ -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"],
Expand Down