Skip to content

fix: unchecked key lookup in Python dict #4517

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

k-yang
Copy link

@k-yang k-yang commented May 9, 2025

Description of changes

This pull request refines error handling in the from_json method of the chromadb/api/configuration.py file.

Improvements to error handling:

  • Enhanced the from_json method to raise a specific error message when the _type key is missing in the input json_map. This ensures clearer error reporting for debugging.

Context

When I was playing around with the Chroma Cloud, I received the following error message when trying to create collection:

f"Trying to instantiate configuration of type {cls.__name__} from JSON with type {json_map['_type']}"
                                                                                  ~~~~~~~~^^^^^^^^^
KeyError: '_type'

The error arises from trying to grab the '_type' key from json_map via [] syntax without first checking if the key exists, which leads to the Python KeyError. It's unfortunate that the [] syntax is used instead of the .get() syntax which is safer because it returns None by default.

The line above actually uses the correct .get() syntax:

if cls.__name__ != json_map.get("_type", None):

and it's apparent that the '_type' key might not exist on json_map, so accessing json_map['_type'] in the next line is dangerous.

My guess is that Chroma Cloud returns a config object when creating a collection, and sometimes that config schema can get out of sync with the client's version. I'm running chromadb==0.6.3 locally.

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs section?

Copy link

github-actions bot commented May 9, 2025

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

Comment on lines +209 to +210
raise ValueError(
f"Trying to instantiate configuration of type {cls.__name__} from JSON with type {json_map.get('_type')}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

The error message improvement is good, but there's a potential inefficiency in accessing json_map.get('_type') twice. Since you're already checking if '_type' is in json_map, you can access it directly in the error message instead of using get again.

Suggested change
raise ValueError(
f"Trying to instantiate configuration of type {cls.__name__} from JSON with type {json_map.get('_type')}"
raise ValueError(
f"Trying to instantiate configuration of type {cls.__name__} from JSON with type {json_map['_type']}"
)

Committable suggestion

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that json_map might not have '_type'.

Copy link
Contributor

This PR improves error handling in the from_json method by safely checking for the existence of the _type key in the input json_map. Instead of directly accessing the key with bracket notation which causes a KeyError when missing, the code now checks if the key exists and provides a more informative error message.

This summary was automatically generated by @propel-code-bot

@jairad26
Copy link
Contributor

Hi, Chroma Cloud does not support 0.6.3 of the python client. You can fix this issue by upgrading to >=1.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants