fix: avoid NaN cosine scores for zero-norm embeddings in InMemoryDocumentStore#11628
Merged
julian-risch merged 3 commits intoJun 16, 2026
Conversation
…mentStore embedding_retrieval normalized embeddings by their L2 norm for cosine similarity, dividing by zero when a document or the query had a zero-norm embedding and producing NaN scores that silently corrupt ranking. Guard the normalization so zero-norm vectors stay zero (score 0.0) instead of NaN.
|
@i-anubhav-anand is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
julian-risch
approved these changes
Jun 16, 2026
julian-risch
left a comment
Member
There was a problem hiding this comment.
Looks good to me! Thank you for your contribution @i-anubhav-anand !
Updated formatting for code snippets in release notes.
Document.score is Optional[float], so math.isnan() needs a None guard. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Self-found bug (no existing issue).
InMemoryDocumentStore.embedding_retrievalreturnsNaNsimilarity scores when a document (or the query) has a zero-norm embedding, silently corrupting ranking.Proposed Changes:
For
cosinesimilarity, embeddings are normalized by their L2 norm:A zero-norm vector (e.g. a zero embedding, which some models emit for empty/whitespace input) makes this divide by zero, producing
NaNscores (numpy even emits aRuntimeWarning: invalid value encountered in divide).NaNthen sorts unpredictably and silently corrupts the ranking.This guards the normalization so zero-norm vectors stay zero (denominator forced to
1.0), giving such documents a cosine score of0.0instead ofNaN. Non-zero embeddings are unaffected.Reproduction (before the fix):
How did you test it?
Added
test_embedding_retrieval_with_zero_vector_does_not_produce_nanintest/document_stores/test_in_memory.py: a zero-embedding document no longer yields aNaNscore (it gets0.0) while a normal document is unaffected. It fails onmain(NaN) and passes with this change. Ranhatch run test:unit test/document_stores/test_in_memory.py(148 passed, 4 skipped),hatch run fmt(clean),hatch run test:types haystack/document_stores/in_memory/document_store.py(mypy clean), and added a release note.Notes for the reviewer
Behavior for non-zero embeddings is unchanged; only the zero-norm edge case is guarded.
Checklist
fix:).hatch run fmtand fixed any issue.