Skip to content

Commit

Permalink
[BUG] UUID should be uuid type not str (#1644)
Browse files Browse the repository at this point in the history
## Description of changes

*Summarize the changes made by this PR.*
 - Improvements & Bug fixes
- fastapi_utils needs to push the uuid as a UUID type not str to
get_collection.
	 - Refactored _uuid into a util
 - New functionality
	 - None
  • Loading branch information
HammadB authored Jan 16, 2024
1 parent a669624 commit 22da192
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
3 changes: 2 additions & 1 deletion chromadb/auth/fastapi_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from functools import partial
from typing import Any, Callable, Dict, Optional, Sequence, cast
from chromadb.server.fastapi.utils import string_to_uuid
from chromadb.api import ServerAPI
from chromadb.auth import AuthzResourceTypes

Expand Down Expand Up @@ -46,7 +47,7 @@ def attr_from_collection_lookup(
def _wrap(**kwargs: Any) -> Dict[str, Any]:
_api = cast(ServerAPI, kwargs["api"])
col = _api.get_collection(
id=kwargs["function_kwargs"][collection_id_arg])
id=string_to_uuid(kwargs["function_kwargs"][collection_id_arg]))
return {"tenant": col.tenant, "database": col.database}

return partial(_wrap, **kwargs)
10 changes: 1 addition & 9 deletions chromadb/server/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from chromadb.api import ServerAPI
from chromadb.errors import (
ChromaError,
InvalidUUIDError,
InvalidDimensionException,
InvalidHTTPVersion,
)
Expand All @@ -51,7 +50,7 @@

import logging

from chromadb.server.fastapi.utils import fastapi_json_response
from chromadb.server.fastapi.utils import fastapi_json_response, string_to_uuid as _uuid
from chromadb.telemetry.opentelemetry.fastapi import instrument_fastapi
from chromadb.types import Database, Tenant
from chromadb.telemetry.product import ServerContext, ProductTelemetryClient
Expand Down Expand Up @@ -96,13 +95,6 @@ async def check_http_version_middleware(
return await call_next(request)


def _uuid(uuid_str: str) -> UUID:
try:
return UUID(uuid_str)
except ValueError:
raise InvalidUUIDError(f"Could not parse {uuid_str} as a UUID")


class ChromaAPIRouter(fastapi.APIRouter): # type: ignore
# A simple subclass of fastapi's APIRouter which treats URLs with a trailing "/" the
# same as URLs without. Docs will only contain URLs without trailing "/"s.
Expand Down
9 changes: 8 additions & 1 deletion chromadb/server/fastapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from uuid import UUID
from starlette.responses import JSONResponse

from chromadb.errors import ChromaError
from chromadb.errors import ChromaError, InvalidUUIDError


def fastapi_json_response(error: ChromaError) -> JSONResponse:
return JSONResponse(
content={"error": error.name(), "message": error.message()},
status_code=error.code(),
)

def string_to_uuid(uuid_str: str) -> UUID:
try:
return UUID(uuid_str)
except ValueError:
raise InvalidUUIDError(f"Could not parse {uuid_str} as a UUID")

0 comments on commit 22da192

Please sign in to comment.