fix(retriever): skip FAISS padding slots instead of returning the last document - #518
Merged
xhd0728 merged 1 commit intoSep 19, 2026
Merged
Conversation
…t document FAISS fills the result with -1 when the index holds fewer vectors than the requested `top_k`. `BaseIndexBackend.contents` is a plain list, so reading `contents[-1]` for those slots returned the LAST document rather than a miss: a 3-passage index queried with `top_k=5` came back as 5 passages, two of them copies of the last one. Downstream that padding is indistinguishable from a real hit, so it is fed to the generator as retrieved evidence. Skip the -1 slots. The other backends are unaffected because they look the content up by the payload the store returns, not by position. Verified with a 3-vector index and top_k=5: before, FAISS returned [0, 2, 1, -1, -1] and the backend produced ['DOC_A', 'DOC_C', 'DOC_B', 'DOC_C', 'DOC_C']; after, it produces ['DOC_A', 'DOC_C', 'DOC_B'].
xhd0728
approved these changes
Sep 19, 2026
Collaborator
|
Thanks for the focused fix. |
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
Proposed Changes:
FAISS pads its result with
-1when the index holds fewer vectors than therequested
top_k.BaseIndexBackend.contentsis a plain list(
index_backends/base.py:30), soself.contents[-1]wraps around to the lastdocument instead of signalling a miss: the padding came back as duplicated
passages, indistinguishable from real hits downstream.
The change skips those slots:
MilvusIndexBackendandQdrantIndexBackendare unaffected — they resolvecontent from the payload the store returns rather than by positional index into
contents.How did you test it?
Ran it, with
faiss-cpu1.15.1. A 3-passage index queried withtop_k=5:search()returns[0, 2, 1, -1, -1]['DOC_A', 'DOC_C', 'DOC_B', 'DOC_C', 'DOC_C'][0, 2, 1, -1, -1]['DOC_A', 'DOC_C', 'DOC_B']This PR adds
tests/servers/retriever/test_faiss_search_padding.py, four casescovering the padding,
top_kbelow the index size,top_kequal to it, and asingle-passage index:
With the fix reverted, the two padding cases fail
(
expected 3 real hits, got 5: ['DOC_A', 'DOC_C', 'DOC_B', 'DOC_C', 'DOC_C'])and the two normal-path cases still pass, so the tests pin the behaviour rather
than the implementation.
uv run pytest tests/ servers/retriever/tests servers/generation/testscouldnot be collected in my environment (
openai,litellmnot installed), so Ihave not run the rest of the suite — the change is confined to one module.
Notes for the reviewer
PR #425 proposed the same guard, bundled with five other fixes across five
files, and was closed. I opened this as a single-file change with a regression
test in case the bundling was what sank it. If the team's reason for closing
that PR applies to this fix as well, please say so and I will close this one
rather than resubmit.
Checklist
tests/servers/retriever/.ruff checkandruff format --checkclean on the new test file. (faiss_backend.pyreports seven pre-existing rules — I001, UP035, UP045, UP006 — on lines this change does not touch; I left them alone rather than bundle unrelated edits.)