Describe the bug
Document.__eq__ can incorrectly report two Document instances as equal when their metadata contains keys that collide with top-level Document fields (for example id, content, or score).
The current implementation compares the output of Document.to_dict(). Since to_dict() defaults to flatten=True, metadata is flattened into the top-level dictionary and any colliding metadata keys are overwritten by the document's own fields before the equality comparison takes place.
As a result, two documents with different metadata can compare as equal even though their logical state differs.
Error message
No exception is raised.
The issue is silent because the equality comparison simply returns the wrong result.
from haystack import Document
doc1 = Document(
id="same_id",
content="hello",
meta={"id": "different1"},
)
doc2 = Document(
id="same_id",
content="hello",
meta={"id": "different2"},
)
print(doc1 == doc2)
Output:
Expected behavior
Documents with different metadata should not compare as equal.
Expected output:
Additional context
While investigating this, I verified the following:
Document.to_dict(flatten=False) preserves the metadata correctly.
- The collision only occurs in the flattened representation.
- Changing
Document.__eq__ locally to compare the unflattened representation fixes the issue.
- I ran the full
test/dataclasses/test_document.py suite after making this change, and all existing tests still passed.
I also have a regression test ready if this is considered a bug.
To Reproduce
from haystack import Document
doc1 = Document(
id="same_id",
content="hello",
meta={"id": "different1"},
)
doc2 = Document(
id="same_id",
content="hello",
meta={"id": "different2"},
)
assert doc1 != doc2
Current result:
Expected result:
FAQ Check
System
- OS: Windows 11
- Haystack version: Current
main branch (verified locally)
Describe the bug
Document.__eq__can incorrectly report twoDocumentinstances as equal when their metadata contains keys that collide with top-levelDocumentfields (for exampleid,content, orscore).The current implementation compares the output of
Document.to_dict(). Sinceto_dict()defaults toflatten=True, metadata is flattened into the top-level dictionary and any colliding metadata keys are overwritten by the document's own fields before the equality comparison takes place.As a result, two documents with different metadata can compare as equal even though their logical state differs.
Error message
No exception is raised.
The issue is silent because the equality comparison simply returns the wrong result.
Output:
Expected behavior
Documents with different metadata should not compare as equal.
Expected output:
Additional context
While investigating this, I verified the following:
Document.to_dict(flatten=False)preserves the metadata correctly.Document.__eq__locally to compare the unflattened representation fixes the issue.test/dataclasses/test_document.pysuite after making this change, and all existing tests still passed.I also have a regression test ready if this is considered a bug.
To Reproduce
Current result:
Expected result:
FAQ Check
System
mainbranch (verified locally)