Skip to content

fix: don't mutate HybridSearchConfig during hybrid search - #338

Open
ankurjindal (ecomlisters) wants to merge 1 commit into
langchain-ai:mainfrom
jindalankuraws:fix/hybrid-search-config-mutation
Open

ankurjindal (ecomlisters) wants to merge 1 commit into
langchain-ai:mainfrom
jindalankuraws:fix/hybrid-search-config-mutation

Conversation

@ecomlisters

Copy link
Copy Markdown

Fixes #337.

asimilarity_search and asimilarity_search_with_score stored the query in hybrid_search_config.fts_query, and __query_collection wrote fetch_top_k into hybrid_search_config.fusion_function_parameters. Both are the caller's objects. Once the first search set fts_query, later searches on the same store used the first query for the keyword half of hybrid search.

Changes

  • Pass the search query via kwargs["fts_query"], which __query_collection already reads as a fallback, instead of assigning it to the config. An fts_query set explicitly in HybridSearchConfig still takes precedence.
  • Build a copy of fusion_function_parameters with fetch_top_k set, and pass that to the fusion function instead of writing into the user's dict. The effective fetch_top_k is unchanged (it is still the search's k).

Tests
Added to tests/unit_tests/v2/test_async_pg_vectorstore_search.py, covering both asimilarity_search and asimilarity_search_with_score:

  • A store-level or per-call config is not mutated by a search.
  • Two consecutive searches with different queries each keyword-search their own query.
  • An explicit fts_query in the config still takes precedence.

The first two fail on main and pass with this change. make test: 761 passed. make lint is clean.

asimilarity_search and asimilarity_search_with_score stored the query in
hybrid_search_config.fts_query, and __query_collection wrote fetch_top_k
into hybrid_search_config.fusion_function_parameters. Both are the
caller's objects, so after the first search every later search on the
same store used the first query for the keyword half of hybrid search.

Pass the query via kwargs["fts_query"] instead, and give the fusion
function a copy of fusion_function_parameters with fetch_top_k set. An
fts_query set explicitly in HybridSearchConfig still takes precedence.

This branch has not been deployed

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

Hybrid search mutates the caller's HybridSearchConfig, so later searches reuse the first query for keyword search

1 participant