Skip to content

FilterPolicy.MERGE accumulates runtime filters across repeated retriever runs #12500

Closed

Description

@CoralGarden52

Describe the bug

FilterPolicy.MERGE mutates the initialization filter when an in-memory retriever is reused. A runtime filter from an earlier run remains active in later runs, so later queries can return incomplete results.

This affects InMemoryBM25Retriever and InMemoryEmbeddingRetriever in both sync and async paths.

Error message

No exception is raised. This is a silent retrieval-correctness bug.

Expected behavior

Each run should combine the fixed initialization filters with only that run's runtime filters. The initialization condition should remain active, but previous runtime conditions should not.

With initialization tenant == "a", first-run kind == "article", and second-run year == 2019, the second run should use:

tenant == "a" AND year == 2019

It currently uses:

tenant == "a" AND kind == "article" AND year == 2019

Additional context

In haystack/document_stores/types/filter_policy.py, combine_runtime_comparison_and_init_logical_filters() assigns init_logical_filter["conditions"] directly and then appends to it. The merged result therefore changes both retriever.filters` and the caller-owned initialization dictionary.

This is especially problematic for long-lived retrievers in dynamic, multi-tenant, or concurrent pipelines, where the same component instance is reused across requests.

This report is distinct from #12065/#12066: those fixed initialization filters being dropped during one MERGE call; this bug leaks a previous runtime filter into later calls.

To Reproduce

from haystack import Document
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.document_stores.types import FilterPolicy

store = InMemoryDocumentStore()
store.write_documents([
    Document(content="python article 2019", meta={"tenant": "a", "kind": "article", "year": 2019}),
    Document(content="python article 2020", meta={"tenant": "a", "kind": "article", "year": 2020}),
    Document(content="python blog 2019", meta={"tenant": "a", "kind": "blog", "year": 2019}),
])

init_filters = {
    "operator": "AND",
    "conditions": [{"field": "meta.tenant", "operator": "==", "value": "a"}],
}
retriever = InMemoryBM25Retriever(
    store, filters=init_filters, filter_policy=FilterPolicy.MERGE, top_k=10
)

retriever.run(query="python", filters={"field": "meta.kind", "operator": "==", "value": "article"})
result = retriever.run(query="python", filters={"field": "meta.year", "operator": "==", "value": 2019})

print([(doc.meta["kind"], doc.meta["year"]) for doc in result["documents"]])
print(init_filters)

Expected second result:

[("article", 2019), ("blog", 2019)]

Actual second result:

[("article", 2019)]

init_filters is also modified to contain the previous kind condition. The same behavior is reproducible with InMemoryEmbeddingRetriever and run_async.

FAQ Check

System:

  • OS: Ubuntu Linux 20.04, x86_64
  • Python: 3.12.13
  • Haystack version: main source checkout, commit 82c1f14 (v3.2.0-rc0-69-g82c1f14d4)

Activity

  1. CoralGarden52 commented on Aug 28, 2026

    @CoralGarden52
    ContributorAuthor

    I reproduced this on the latest upstream baseline and prepared a fix on CoralGarden52:fix/filter-policy-merge-mutation. The merge helpers now build independent conditions lists and avoid mutating caller-owned logical filters, so reused BM25 and embedding retrievers no longer carry runtime conditions into later sync or async runs. The branch includes regression tests for both retrievers and direct input immutability checks; the focused suite passes (48 tests), and Hatch formatting plus core-source type checks pass.

  2. self-assigned this
    on Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions