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

[ENH]: Updates to validation errors #1703

Merged
merged 1 commit into from
Mar 6, 2024
Merged
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
32 changes: 21 additions & 11 deletions chromadb/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ def __call__(self, uris: URIs) -> L:
def validate_ids(ids: IDs) -> IDs:
"""Validates ids to ensure it is a list of strings"""
if not isinstance(ids, list):
raise ValueError(f"Expected IDs to be a list, got {ids}")
raise ValueError(f"Expected IDs to be a list, got {type(ids).__name__} as IDs")
if len(ids) == 0:
raise ValueError(f"Expected IDs to be a non-empty list, got {ids}")
raise ValueError(f"Expected IDs to be a non-empty list, got {len(ids)} IDs")
seen = set()
dups = set()
for id_ in ids:
Expand Down Expand Up @@ -259,32 +259,38 @@ def validate_ids(ids: IDs) -> IDs:
def validate_metadata(metadata: Metadata) -> Metadata:
"""Validates metadata to ensure it is a dictionary of strings to strings, ints, floats or bools"""
if not isinstance(metadata, dict) and metadata is not None:
raise ValueError(f"Expected metadata to be a dict or None, got {metadata}")
raise ValueError(
f"Expected metadata to be a dict or None, got {type(metadata).__name__} as metadata"
)
if metadata is None:
return metadata
if len(metadata) == 0:
raise ValueError(f"Expected metadata to be a non-empty dict, got {metadata}")
raise ValueError(
f"Expected metadata to be a non-empty dict, got {len(metadata)} metadata attributes"
)
for key, value in metadata.items():
if key == META_KEY_CHROMA_DOCUMENT:
raise ValueError(
f"Expected metadata to not contain the reserved key {META_KEY_CHROMA_DOCUMENT}"
)
if not isinstance(key, str):
raise TypeError(
f"Expected metadata key to be a str, got {key} which is a {type(key)}"
f"Expected metadata key to be a str, got {key} which is a {type(key).__name__}"
)
# isinstance(True, int) evaluates to True, so we need to check for bools separately
if not isinstance(value, bool) and not isinstance(value, (str, int, float)):
raise ValueError(
f"Expected metadata value to be a str, int, float or bool, got {value} which is a {type(value)}"
f"Expected metadata value to be a str, int, float or bool, got {value} which is a {type(value).__name__}"
)
return metadata


def validate_update_metadata(metadata: UpdateMetadata) -> UpdateMetadata:
"""Validates metadata to ensure it is a dictionary of strings to strings, ints, floats or bools"""
if not isinstance(metadata, dict) and metadata is not None:
raise ValueError(f"Expected metadata to be a dict or None, got {metadata}")
raise ValueError(
f"Expected metadata to be a dict or None, got {type(metadata)}"
)
if metadata is None:
return metadata
if len(metadata) == 0:
Expand Down Expand Up @@ -471,14 +477,17 @@ def validate_n_results(n_results: int) -> int:
def validate_embeddings(embeddings: Embeddings) -> Embeddings:
"""Validates embeddings to ensure it is a list of list of ints, or floats"""
if not isinstance(embeddings, list):
raise ValueError(f"Expected embeddings to be a list, got {embeddings}")
raise ValueError(
f"Expected embeddings to be a list, got {type(embeddings).__name__}"
)
if len(embeddings) == 0:
raise ValueError(
f"Expected embeddings to be a list with at least one item, got {embeddings}"
f"Expected embeddings to be a list with at least one item, got {len(embeddings)} embeddings"
)
if not all([isinstance(e, list) for e in embeddings]):
raise ValueError(
f"Expected each embedding in the embeddings to be a list, got {embeddings}"
"Expected each embedding in the embeddings to be a list, got "
f"{list(set([type(e).__name__ for e in embeddings]))}"
)
for i, embedding in enumerate(embeddings):
if len(embedding) == 0:
Expand All @@ -492,7 +501,8 @@ def validate_embeddings(embeddings: Embeddings) -> Embeddings:
]
):
raise ValueError(
f"Expected each value in the embedding to be a int or float, got {embeddings}"
"Expected each value in the embedding to be a int or float, got an embedding with "
f"{list(set([type(value).__name__ for value in embedding]))} - {embedding}"
)
return embeddings

Expand Down
Loading