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] Dimension mismatch should not return 500 #3193

Open
wants to merge 3 commits 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
140 changes: 65 additions & 75 deletions chromadb/server/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
from fastapi.responses import JSONResponse, ORJSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.routing import APIRoute
from fastapi import HTTPException, status

from fastapi import status
from chromadb.api.configuration import CollectionConfigurationInternal
from pydantic import BaseModel
from chromadb.api.types import (
Expand All @@ -41,7 +40,6 @@
from chromadb.api import ServerAPI
from chromadb.errors import (
ChromaError,
InvalidDimensionException,
InvalidHTTPVersion,
RateLimitError,
QuotaError,
Expand Down Expand Up @@ -773,46 +771,42 @@ async def add(
collection_id: str,
body: AddEmbedding = Body(...),
) -> bool:
try:

def process_add(request: Request, raw_body: bytes) -> bool:
add = validate_model(AddEmbedding, orjson.loads(raw_body))
self.auth_request(
request.headers,
AuthzAction.ADD,
tenant,
database_name,
collection_id,
)
self._set_request_context(request=request)
add_attributes_to_current_span({"tenant": tenant})
return self._api._add(
collection_id=_uuid(collection_id),
ids=add.ids,
embeddings=cast(
Embeddings,
convert_list_embeddings_to_np(add.embeddings)
if add.embeddings
else None,
),
metadatas=add.metadatas, # type: ignore
documents=add.documents, # type: ignore
uris=add.uris, # type: ignore
tenant=tenant,
database=database_name,
)

return cast(
bool,
await to_thread.run_sync(
process_add,
request,
await request.body(),
limiter=self._capacity_limiter,
def process_add(request: Request, raw_body: bytes) -> bool:
add = validate_model(AddEmbedding, orjson.loads(raw_body))
self.auth_request(
request.headers,
AuthzAction.ADD,
tenant,
database_name,
collection_id,
)
self._set_request_context(request=request)
add_attributes_to_current_span({"tenant": tenant})
return self._api._add(
collection_id=_uuid(collection_id),
ids=add.ids,
embeddings=cast(
Embeddings,
convert_list_embeddings_to_np(add.embeddings)
if add.embeddings
else None,
),
metadatas=add.metadatas, # type: ignore
documents=add.documents, # type: ignore
uris=add.uris, # type: ignore
tenant=tenant,
database=database_name,
)
except InvalidDimensionException as e:
raise HTTPException(status_code=500, detail=str(e))

return cast(
bool,
await to_thread.run_sync(
process_add,
request,
await request.body(),
limiter=self._capacity_limiter,
),
)

@trace_method("FastAPI.update", OpenTelemetryGranularity.OPERATION)
async def update(
Expand Down Expand Up @@ -1639,42 +1633,38 @@ async def delete_collection_v1(
async def add_v1(
self, request: Request, collection_id: str, body: AddEmbedding = Body(...)
) -> bool:
try:

def process_add(request: Request, raw_body: bytes) -> bool:
add = validate_model(AddEmbedding, orjson.loads(raw_body))
self.auth_and_get_tenant_and_database_for_request(
request.headers,
AuthzAction.ADD,
None,
None,
collection_id,
)
return self._api._add(
collection_id=_uuid(collection_id),
ids=add.ids,
embeddings=cast(
Embeddings,
convert_list_embeddings_to_np(add.embeddings)
if add.embeddings
else None,
),
metadatas=add.metadatas, # type: ignore
documents=add.documents, # type: ignore
uris=add.uris, # type: ignore
)

return cast(
bool,
await to_thread.run_sync(
process_add,
request,
await request.body(),
limiter=self._capacity_limiter,
def process_add(request: Request, raw_body: bytes) -> bool:
add = validate_model(AddEmbedding, orjson.loads(raw_body))
self.auth_and_get_tenant_and_database_for_request(
request.headers,
AuthzAction.ADD,
None,
None,
collection_id,
)
return self._api._add(
collection_id=_uuid(collection_id),
ids=add.ids,
embeddings=cast(
Embeddings,
convert_list_embeddings_to_np(add.embeddings)
if add.embeddings
else None,
),
metadatas=add.metadatas, # type: ignore
documents=add.documents, # type: ignore
uris=add.uris, # type: ignore
)
except InvalidDimensionException as e:
raise HTTPException(status_code=500, detail=str(e))

return cast(
bool,
await to_thread.run_sync(
process_add,
request,
await request.body(),
limiter=self._capacity_limiter,
),
)

@trace_method("FastAPI.update_v1", OpenTelemetryGranularity.OPERATION)
async def update_v1(
Expand Down
31 changes: 29 additions & 2 deletions chromadb/test/api/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from chromadb.api import ClientAPI
import numpy as np

from chromadb.errors import InvalidDimensionException


def test_duplicate_collection_create(
client: ClientAPI,
) -> None:
collection = client.create_collection(
_ = client.create_collection(
name="test",
metadata={"hnsw:construction_ef": 128, "hnsw:search_ef": 128, "hnsw:M": 128},
)
Expand All @@ -28,10 +31,34 @@ def test_not_existing_collection_delete(
client: ClientAPI,
) -> None:
try:
collection = client.delete_collection(
_ = client.delete_collection(
name="test101",
)
assert False, "Expected exception"
except Exception as e:
print("Collection deletion failed as expected with error ", e)
assert "does not exist" in e.args[0]


def test_collection_dimension_mismatch(
client: ClientAPI,
) -> None:
collection = client.create_collection(
name="test",
)
D = 768
N = 5
embeddings = np.random.random(size=(N, D))
ids = [str(i) for i in range(N)]

collection.add(ids=ids, embeddings=embeddings) # type: ignore[arg-type]

WRONG_D = 512
wrong_embeddings = np.random.random(size=(N, WRONG_D))
try:
collection.add(ids=ids, embeddings=wrong_embeddings) # type: ignore[arg-type]
assert False, "Expected exception"
except InvalidDimensionException:
print("Dimension mismatch failed as expected")
except Exception as e:
assert False, f"Unexpected exception {e}"
Loading