fix(v2): don't mutate instance hybrid_search_config in similarity searches#309
Open
Humphrey (HumphreySun98) wants to merge 1 commit into
Conversation
…rches `AsyncPGVectorStore.asimilarity_search` and `asimilarity_search_with_score` back-filled `fts_query` on the `hybrid_search_config` object retrieved from kwargs or, by default, from `self.hybrid_search_config`. Because the assignment hit the shared instance variable in place, the very first call's query "stuck" on it and every subsequent search reused that fts_query regardless of the new `query` argument. Same hazard for callers that reuse a single `HybridSearchConfig` across calls. Copy the config before setting `fts_query`. Adds regression tests that bypass `AsyncPGVectorStore.create(...)` (no Postgres needed) and pin all three invariants: instance-level config is preserved, caller- provided config is preserved, and an existing `fts_query` is left alone when the back-fill branch is skipped. Fixes langchain-ai#288
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
`AsyncPGVectorStore.asimilarity_search` and `asimilarity_search_with_score` back-fill `fts_query` on the `HybridSearchConfig` object retrieved from kwargs or, by default, from `self.hybrid_search_config`:
```python
hybrid_search_config = kwargs.get("hybrid_search_config", self.hybrid_search_config)
if hybrid_search_config and not hybrid_search_config.fts_query:
hybrid_search_config.fts_query = query # ← in-place
kwargs["hybrid_search_config"] = hybrid_search_config
```
Because the assignment hits the shared object in place, the very first call's `query` sticks on it and every subsequent search reuses that `fts_query` regardless of the new `query` argument. Same hazard for callers that pass the same `HybridSearchConfig` to multiple searches. This is exactly the symptom reported in #288.
The fix is to copy the config before back-filling, mirroring how other LangChain integrations handle caller-owned input (see e.g. langchain-anthropic's recent `bind_tools` mutation fix).
Relevant issues
Fixes #288
Changes
Testing
```
$ python -m pytest tests/unit_tests/v2/test_async_vectorstore_no_mutation.py -q
4 passed in 0.49s
```
Reverting only the `copy.copy(...)` line while keeping the new tests makes three of the four tests fail (the fourth covers the no-back-fill branch and is unaffected), confirming the regression coverage pins the buggy behavior. `ruff check` and `ruff format --check` pass on both files.
Note
Fix is intentionally minimal — a shallow `copy.copy` is sufficient because `HybridSearchConfig` is a flat dataclass and only `fts_query` (a string) is reassigned. No callers need updating; non-hybrid-search paths are untouched.
Disclaimer: this PR was prepared with the assistance of an AI agent (Claude Code). All code and test changes were reviewed by the author before submission.