LostInTheMiddleRanker bypasses non-textual validation for a single selected document
Describe the bug
LostInTheMiddleRanker.run() returns a non-textual document without raising the documented ValueError when only one document remains after deduplication and top_k selection. The same non-textual document is rejected when the selected list has two entries. This makes validation depend on the number of selected documents and lets a blob-only document reach components expecting text.
Error message
No error is raised in the single-document case. With two selected documents, the existing error is:
Some provided documents are not textual; LostInTheMiddleRanker can process only text.
Expected behavior
Validate the selected document's content before the single-document shortcut. Continue to ignore documents excluded by top_k, as the current multi-document path does.
To Reproduce
from haystack import Document
from haystack.components.rankers import LostInTheMiddleRanker
from haystack.dataclasses import ByteStream
ranker = LostInTheMiddleRanker()
blob = Document(blob=ByteStream(b"some bytes"))
text = Document(content="text")
ranker.run(documents=[blob]) # returns blob; expected ValueError
ranker.run(documents=[blob, text], top_k=1) # returns blob; expected ValueError
ranker.run(documents=[blob, text]) # raises the documented ValueErrorAdditional context
The len(documents_to_reorder) == 1 return precedes the existing non-textual validation. A minimal fix moves that shortcut below the validation, keeping selection and deduplication unchanged.
Four regression cases (single input, constructor top_k=1, runtime top_k=1, duplicate inputs) all fail against unchanged main with DID NOT RAISE ValueError. With the local fix, all 31 tests in the ranker module pass, including a control that excludes a blob using top_k. Focused mypy, Ruff and all applicable pre-commit hooks pass.
Related PR #13017 (closed without merge) proposed changes to the predicate for non-string content and the word-count implementation. Its patch leaves the single-document shortcut before validation, so it does not address this reproduction. This report is limited to validation order for content=None.
FAQ Check
- Have you had a look at the FAQ page?
System:
- OS: macOS arm64
- Python: 3.12.14
- Haystack: main at
f4e004c34cbc724b3ffb026debdf7a0541e4a21f - Official Hatch environments; local documents, no model downloads or API calls required.
Prepared and tested with the help of Codex.
Source: deepset-ai/haystack