Skip to content

[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

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

Conversation

jairad26
Copy link
Contributor

@jairad26 jairad26 commented May 8, 2025

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

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 8, 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)

Copy link
Contributor Author

jairad26 commented May 8, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@jairad26 jairad26 marked this pull request as ready for review May 8, 2025 22:32
Copy link
Contributor

propel-code-bot bot commented May 8, 2025

Add Validation for Multiple Embedding Function Configurations

This PR implements validation across Python and JavaScript clients to prevent conflicts when embedding functions are provided in multiple places. It adds checks to create, get, and get_or_create collection methods to ensure that when embedding functions are specified both in the function parameters and configuration object, they are either identical or an error is raised.

Key Changes:
• Add validation in Python client (sync and async) to detect conflicting embedding functions
• Add validation in JavaScript client to check for duplicate embedding functions
• Add comprehensive test cases to verify validation behavior
• Improve error messaging to help users identify the issue

Affected Areas:
• Python sync/async client APIs
• JavaScript client
• Collection creation, retrieval, and get_or_create flows
• Test configurations

Potential Impact:

Functionality: Users who previously provided embedding functions in multiple places may now receive errors, forcing them to use a single, consistent embedding function specification.

Performance: Minimal impact - only adds small validation checks during collection operations

Security: No security implications

Scalability: No scalability implications

Review Focus:
• Implementation consistency between Python and JavaScript clients
• Thoroughness of validation logic across different client methods
• Potential edge cases in equality checking of embedding functions
• Error message clarity and helpfulness

Testing Needed

• Test using matching embedding functions in both places (should succeed)
• Test using different embedding functions in both places (should fail)
• Test typical use cases with embedding function in only one place
• Verify error messages are clear and actionable

Code Quality Assessment

chromadb/api/client.py: Good validation logic with clear error messaging

chromadb/api/async_client.py: Maintains consistent validation approach with sync client

clients/js/packages/chromadb-core/src/ChromaClient.ts: Potential issue with object equality checking using !== instead of deep comparison

Best Practices

Error Handling:
• Clear error messages
• Validation at API boundaries

Testing:
• Comprehensive test cases for both success and failure paths

Potential Issues

• JavaScript equality check uses !== which may not correctly identify equal objects with different references
• Default embedding functions might need special handling

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

@jairad26 jairad26 marked this pull request as draft May 8, 2025 22:37
@jairad26 jairad26 force-pushed the jai/collection-config-nits branch from 7ea7b69 to 872c86d Compare May 8, 2025 22:55
@jairad26 jairad26 requested a review from HammadB May 8, 2025 22:56
@jairad26 jairad26 marked this pull request as ready for review May 8, 2025 22:56
Comment on lines 245 to 246
efConfig.name !== embeddingFunction.name ||
efConfig !== collConfigEfConfig
Copy link
Contributor

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.

Suggested change
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.

@jairad26 jairad26 force-pushed the jai/collection-config-nits branch from 872c86d to 79619c2 Compare May 8, 2025 23:39
Comment on lines +245 to +246
embeddingFunction.name !== configuration.embedding_function.name ||
efConfig !== collConfigEfConfig
Copy link
Contributor

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.

Suggested change
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.

Comment on lines 193 to +207
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."
)

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 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.

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.

1 participant