fix(chroma): forward filter/where_document in _similarity_search_with_relevance_scores
#34694
+264
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ensure chroma parameters are properly passed in
_similarity_search_with_relevance_scoresWhat this PR fixes
Fixes parameter forwarding in
_similarity_search_with_relevance_scoresso chroma-specificfilterandwhere_documentare correctly passed, and the customrelevance_score_fnis applied.Bug explanation
The base implementation forwards all parameters via
**kwargs. Chroma'ssimilarity_search_with_scoreexplicitly acceptsfilterandwhere_document. When these are passed tosimilarity_search_with_relevance_scores, extracting them explicitly ensures correct forwarding and preserves compatibility with chroma's filtering.How to reproduce
Solution
Override
_similarity_search_with_relevance_scoresto explicitly extractfilterandwhere_documentand pass them tosimilarity_search_with_score. This:**kwargsusageThe override follows the same pattern as the base implementation, with explicit parameter extraction for chroma's API.
ChromaDB repository dependency
This PR depends on chromaDB's
Collection.querymethod (defined at chromadb/api/models/Collection.py, lines 189-226), which acceptswhereandwhere_documentparameters.Test coverage
Five new test cases verify the fix works correctly and maintains backward compatibility:
test_chroma_similarity_search_with_relevance_scores_with_filter: Verifies that when you pass afilterparameter explicitly (likefilter={"first_letter": "f"}), the filtering works correctly and the customrelevance_score_fnis still applied to transform the scores.test_chroma_similarity_search_with_relevance_scores_with_filter_kwargs: Tests backward compatibility by passingfilterinside**kwargs(like**{"filter": {"first_letter": "b"}}). This ensures old code that passes filters via kwargs continues to work correctly.test_chroma_similarity_search_with_relevance_scores_with_where_document: Verifies that thewhere_documentparameter (used to filter by document content, like{"$contains": "there"}) is properly forwarded and works together with the relevance score transformation.test_chroma_similarity_search_with_relevance_scores_with_other_kwargs: Ensures that whenfilteris passed via kwargs through the public API (simulating how the base class calls the method), our override correctly extracts it from kwargs and uses it. This maintains backward compatibility with existing code that doesn't use explicit parameters.test_chroma_similarity_search_with_relevance_scores_both_params: Tests the combination scenario where bothfilterandwhere_documentare used together, verifying that both parameters are correctly forwarded and the relevance score transformation still works.