Skip to content

AutoMergingRetriever counts a leaf document retrieved more than once as several matched children #13505

Closed

Description

@TemitopeKadri

Describe the bug

AutoMergingRetriever counts every copy of a matched leaf document when it decides whether to return the parent. If the same leaf is in documents more than once, for example because the results of two retrievers were concatenated before reaching it, each copy counts as another matched child. The parent can then be returned even though fewer distinct children than threshold matched.

When the parent isn't returned, the duplicate leaf is returned once per copy.

Error message

None. The output is wrong silently.

Expected behavior

Each leaf document is counted once, so the merge decision depends on how many distinct children of the parent were matched. A leaf retrieved twice is returned once, the copy with the highest score.

Additional context

run and run_async group the input documents by __parent_id and compute len(child_docs) / len(parent.meta["__children_ids"]) without deduplicating child_docs. Other components that merge results (DocumentJoiner, MultiRetriever, the multi-query retrievers) already deduplicate by document ID with _deduplicate_documents. I have a PR ready that applies the same deduplication before merging.

To Reproduce

On current main (8e38c6331):

from haystack import Document
from haystack.components.retrievers import AutoMergingRetriever
from haystack.document_stores.in_memory import InMemoryDocumentStore

store = InMemoryDocumentStore()
parent = Document(id="p", content="abc", meta={"__children_ids": ["a", "b", "c"], "__level": 0, "__block_size": 3})
leaves = [
    Document(id=i, content=i, meta={"__parent_id": "p", "__level": 1, "__block_size": 1}) for i in ("a", "b", "c")
]
store.write_documents([parent, *leaves])

retriever = AutoMergingRetriever(store, threshold=0.5)
print([d.id for d in retriever.run([leaves[0]])["documents"]])
# >> ['a']    1 of 3 children matched, below the threshold
print([d.id for d in retriever.run([leaves[0], leaves[0]])["documents"]])
# >> ['p']    the same single child, passed twice, returns the parent

print([d.id for d in AutoMergingRetriever(store, threshold=0.9).run([leaves[0], leaves[0]])["documents"]])
# >> ['a', 'a']

FAQ Check

System:

  • Haystack version (commit or version number): main @ 8e38c6331

Activity

  1. senthurapandi2000 commented on Oct 31, 2026

    @senthurapandi2000
    Contributor

    Hi, I'd be interested in contributing to this issue! I'd like to help reproduce the bug, implement deduplication by document ID, and add regression tests to ensure the highest-scoring document is retained.

    I noticed you mentioned having a PR ready. Is this issue still available for contributions, or would there be an opportunity to help with testing or the fix?

    Thanks!

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

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions