Skip to content

Commit

Permalink
[ENH]: Updates to validation errors (#1703)
Browse files Browse the repository at this point in the history
Refs: #1694

## Description of changes

*Summarize the changes made by this PR.*
 - Improvements & Bug fixes
	 - Shortened validation error printouts to improve log readability
## Test plan
*How are these changes tested?*

- [x] Tests pass locally with `pytest` for python, `yarn test` for js

## Documentation Changes
N/A
  • Loading branch information
tazarov authored Mar 6, 2024
1 parent 73a1630 commit 362d350
Showing 1 changed file with 21 additions and 11 deletions.
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

0 comments on commit 362d350

Please sign in to comment.