Skip to content

fix: Update Document.__eq__ to intelligently compare floats#11828

Open
nigamgauri wants to merge 2 commits into
deepset-ai:mainfrom
nigamgauri:fix-document-eq-float-comparison
Open

fix: Update Document.__eq__ to intelligently compare floats#11828
nigamgauri wants to merge 2 commits into
deepset-ai:mainfrom
nigamgauri:fix-document-eq-float-comparison

Conversation

@nigamgauri

Copy link
Copy Markdown

Related Issues

Proposed Changes:

Document.__eq__ performed strict dictionary equality comparison (self.to_dict() == other.to_dict()) on the serialized representation of two documents. This caused equality checks to fail when float fields (such as score, embedding values, sparse_embedding values, or nested floats in meta) differed very slightly due to floating-point imprecision.

This PR replaces the strict comparison in Document.__eq__ with a recursive structure comparison helper (_recursive_is_close) that uses math.isclose (with a relative/absolute tolerance of 1e-7) when encountering floating-point numbers.

How did you test it?

  • Added four new unit tests in test/dataclasses/test_document.py verifying float comparisons for score, embedding, sparse_embedding, and nested meta fields.
  • Verified that all unit tests, format/lint checks (hatch run fmt), and type checks (hatch run test:types) pass successfully.

@nigamgauri nigamgauri requested a review from a team as a code owner June 30, 2026 09:44
@nigamgauri nigamgauri requested review from sjrl and removed request for a team June 30, 2026 09:44
@vercel

vercel Bot commented Jun 30, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Jun 30, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@sjrl

sjrl commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Hey @nigamgauri please sign the CLA agreement #11828 (comment) otherwise we can't accept your contribution.

I'll proceed with a review once it's signed.

@nigamgauri nigamgauri force-pushed the fix-document-eq-float-comparison branch from 7e5340e to a022b38 Compare July 10, 2026 09:10
…-comparison

# Conflicts:
#	haystack/dataclasses/document.py
#	test/dataclasses/test_document.py
@nigamgauri

Copy link
Copy Markdown
Author

Hey @nigamgauri please sign the CLA agreement #11828 (comment) otherwise we can't accept your contribution.

I'll proceed with a review once it's signed.

Hi @sjrl, the CLA is signed now and I've resolved the merge conflicts with main. Ready for review whenever you get a chance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update Document.__eq__ to intelligently compare floats

3 participants