-
Notifications
You must be signed in to change notification settings - Fork 2k
[ENH] Add validation when multiple embedding functions set on client #4507
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
Conversation
Reviewer ChecklistPlease leverage this checklist to ensure your code review is thorough before approving Testing, Bugs, Errors, Logs, Documentation
System Compatibility
Quality
|
|
Validation for Conflicting Embedding Functions on Collection Operations This PR introduces validation logic on Python and TypeScript client APIs (sync, async, and JS) to ensure that if an embedding function is specified both via a direct function argument and within the collection configuration, a conflict is detected and an error is raised if they are incompatible. New helper functions have been added to encapsulate this logic for Key Changes: Affected Areas: Potential Impact: Functionality: Prevents ambiguous embedding function assignment and raises explicit errors if two incompatible embedding functions are provided during collection creation or retrieval. This reduces potential runtime errors or silent misconfigurations. Performance: Negligible; added validation logic is straightforward and only impacts control flow during configuration setup. Security: No new risks introduced. Scalability: No significant impact. Review Focus: Testing Needed• Run all existing and new tests (Python via pytest, Code Quality Assessmentclients/js/packages/chromadb-core/src/ChromaClient.ts: Logic is clearly ported; equality checks for JS objects should use deep comparison for future robustness. clients/js/packages/chromadb-core/src/CollectionConfiguration.ts: Helper function provided, but uses reference equality for object configs, which may need improvement (suggest deep-equals). chromadb/api/client.py: Encapsulated conflict logic; followup refactoring may further DRY up entrypoints. chromadb/api/async_client.py: Validation encapsulated in helpers, code is clear. chromadb/api/collection_configuration.py: Adds clear helpers, docstrings provided, respects error handling. chromadb/test/configurations/test_collection_configuration.py: Extensive and systematic new tests ensure correctness and prevent regressions. Best PracticesValidation: Error Handling: Potential Issues• JS (TS) config comparison uses object reference (===) rather than deep value comparison for embedding function configs-could produce false negatives on functionally identical but separate objects. This summary was automatically generated by @propel-code-bot |
7ea7b69 to
872c86d
Compare
| efConfig.name !== embeddingFunction.name || | ||
| efConfig !== collConfigEfConfig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[DataTypeCheck]
The check for equality of embedding function configurations in the TypeScript client uses efConfig !== collConfigEfConfig (and similarly for .name). This will almost always be true for different objects with identical contents. Use a deep comparison method to accurately compare the configs, such as JSON.stringify() or a dedicated deep-equality utility.
| efConfig.name !== embeddingFunction.name || | |
| efConfig !== collConfigEfConfig | |
| if ( | |
| efConfig.name !== embeddingFunction.name || | |
| JSON.stringify(efConfig) !== JSON.stringify(collConfigEfConfig) | |
| ) { | |
| throw new Error( | |
| "Multiple embedding functions provided. Please provide only one.", | |
| ); | |
| } |
⚡ 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.
872c86d to
79619c2
Compare
| embeddingFunction.name !== configuration.embedding_function.name || | ||
| efConfig !== collConfigEfConfig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[DataTypeCheck]
The equality check between efConfig and collConfigEfConfig on lines 246 and 333 will not work as expected for objects in JavaScript, since !== only checks reference equality. Use a deep comparison function (like lodash's isEqual) or JSON serialization for robust configuration checks.
| embeddingFunction.name !== configuration.embedding_function.name || | |
| efConfig !== collConfigEfConfig | |
| if ( | |
| embeddingFunction.name !== configuration.embedding_function.name || | |
| JSON.stringify(efConfig) !== JSON.stringify(collConfigEfConfig) | |
| ) { | |
| throw new Error( | |
| "Multiple embedding functions provided. Please provide only one.", | |
| ); | |
| } |
Repeat this update for all similar blocks checking configuration equality.
⚡ 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.
chromadb/api/async_client.py
Outdated
| if ( | ||
| embedding_function is not None | ||
| and configuration.get("embedding_function") is None | ||
| and embedding_function.name() != "default" | ||
| and configuration_ef is not None | ||
| ): | ||
| ef_configuration = embedding_function.get_config() | ||
| coll_config_ef_configuration = configuration_ef.get_config() | ||
| if ( | ||
| embedding_function.name() != configuration_ef.name() | ||
| or ef_configuration != coll_config_ef_configuration | ||
| ): | ||
| raise ValueError( | ||
| "Multiple embedding functions provided. Please provide only one." | ||
| ) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[BestPractice]
The logic for validating embedding function conflicts (checking names and configurations if both an embedding_function parameter and an embedding function from the configuration dictionary are present and non-default) is duplicated in create_collection (here, lines 193-207), get_collection (lines 245-259), and get_or_create_collection (lines 285-299). This pattern also exists in the synchronous chromadb/api/client.py. Consider extracting this validation into a private helper method within each class to improve maintainability and reduce redundancy. The helper could handle the comparison of names and configurations and raise the appropriate ValueError.
chromadb/api/async_client.py
Outdated
| "Multiple embedding functions provided. Please provide only one." | ||
| ) | ||
|
|
||
| # If ef provided in function params and collection config is None, set the collection config to the function params |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this comment is wrong
chromadb/api/async_client.py
Outdated
|
|
||
| # if ef has been loaded from the collection config, and a new ef is provided, check if they are the same | ||
| # if not, raise an error | ||
| if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we encapsulate this logic?
chromadb/api/async_client.py
Outdated
| configuration_ef = configuration.get("embedding_function") | ||
|
|
||
| # if ef has been loaded from the collection config, and a new ef is provided, check if they are the same | ||
| # if not, raise an error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we encapsulate this logic?
HammadB
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Encapsulate the shared logic
79619c2 to
db8ef03
Compare
db8ef03 to
a462980
Compare
e8ffaa9 to
1aff970
Compare
a462980 to
b64e7f3
Compare
1aff970 to
457a780
Compare
457a780 to
5e58b94
Compare
b64e7f3 to
7ae1379
Compare
13b451c to
a2d3143
Compare
7f58958 to
2f350ad
Compare
HammadB
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the error into the function
chromadb/api/async_client.py
Outdated
|
|
||
| configuration_ef = configuration.get("embedding_function") | ||
|
|
||
| has_conflict, error_message = has_embedding_function_conflict_on_create( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just let this raise? why catch the bool and raise?
chromadb/api/async_client.py
Outdated
| ) | ||
|
|
||
| if has_conflict: | ||
| raise ValueError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or set it to the existing one?
2f350ad to
bf4d47c
Compare
…hroma-core#4507) ## Description of changes This PR adds validation on create, get, and get_or_create collection to ensure that if embedding functions are provided in both places, it raises an error for the user if they are incompatible ## 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](https://github.com/chroma-core/chroma/tree/main/docs/docs.trychroma.com)?_

Description of changes
This PR adds validation on create, get, and get_or_create collection to ensure that if embedding functions are provided in both places, it raises an error for the user if they are incompatible
Test plan
How are these changes tested?
pytestfor python,yarn testfor js,cargo testfor rustDocumentation Changes
Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs section?