AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Core: `InMemoryVectorStore` Raises `NotImplementedError` For Relevance-Score Search And The `Similarity_score_threshold` Retriever

`InMemoryVectorStore` does not override `_select_relevance_score_fn`, so `similarity_search_with_relevance_scores` and the `similarity_score_threshold` retriever raise `NotImplementedError` even though the store computes cosine similarity internally.

highConfidence 95%Langchain-CoreAffected V1.6.3Affected V1.6.0Affected V1.6.1Affected V1.6.2

Origin Analysis

The base `VectorStore._select_relevance_score_fn` raises `NotImplementedError` by design, and `InMemoryVectorStore` fails to provide an override; its stored cosine similarity scores are in [-1,1] (higher is better), so the existing `_cosine_relevance_score_fn` (which expects a distance and does `1 - distance`) cannot be reused directly.
1. Instantiate `InMemoryVectorStore` with any embedding function. 2. Add texts to the store. 3. Call `vector_store.similarity_search_with_relevance_scores(query, k=1)` or create a retriever via `as_retriever(search_type='similarity_score_threshold', search_kwargs={'score_threshold': 0.5})` and invoke it. 4. Observe `NotImplementedError` for both sync and async paths.

Fixing Code Block

Edge Case Audit

The linear normalization assumes scores are cosine similarities bounded to [-1,1]. If the underlying storage ever changes to distances or another metric, this mapping will be incorrect. Users who previously caught `NotImplementedError` may now receive normalized relevance scores instead of raw cosine similarities; no existing numeric behavior in `similarity_search_with_score` is altered. Rolling back this change will restore the broken exception path, so after rollout keep the override and add regression tests. The lambda is stateless and thread-safe.

Ecosystem Topology