fix: validate top_k in HuggingFaceTEIRanker#11743
Conversation
|
@Kunal-Somani is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
|
Hey @Kunal-Somani thanks for opening the PR! I do want to let you know that this component will be soon deprecated since we moved it to core integrations here https://github.com/deepset-ai/haystack-core-integrations/blob/a9aa83f1122a1e3ea3085b1d3c05090366ec700b/integrations/huggingface_api/src/haystack_integrations/components/rankers/huggingface_api/ranker.py#L31 So I'd recommend moving your PR to that repo with your fix. |
Thanks for the advice, moving this to haystack-core-integrations with the same fix. Will close this PR. |
Related Issues
Proposed Changes:
HuggingFaceTEIRankerhad no validation on itstop_kparameter, unlike five of its six sibling ranker components (LLMRanker,LostInTheMiddleRanker,MetaFieldRanker,SentenceTransformersDiversityRanker,SentenceTransformersSimilarityRanker,TransformersSimilarityRanker), all of which raiseValueError(f"top_k must be > 0, but got {top_k}")for invalid values.Confirmed via reproduction that
HuggingFaceTEIRanker(url=..., top_k=0)andHuggingFaceTEIRanker(url=..., top_k=-5)both construct without error, and the invalid value silently flows into ranking logic viamin(top_k or self.top_k, len(result)).This PR adds the same validation pattern at:
__init__time (for the defaulttop_k)run()'s optionaltop_koverriderun_async()'s optionaltop_koverrideusing the exact same validation logic and error message already used by the sibling components.
How did you test it?
PYTHONPATH="" hatch run test:unit test/components/rankers/test_hugging_face_tei.py— 16 passedhatch run test:types— no issues in 365 source fileshatch run fmt— all checks passedgit diffthat the change is purely additive (+9 lines, 0 removed), with zero unrelated formatting changes to the existing fileNotes for the reviewer
Not a breaking change — no previously-valid
top_kvalue is affected by this fix; only inputs that were already invalid (zero or negative) now fail clearly instead of silently producing broken behavior. Note thatHuggingFaceTEIRankeris deprecated (moving tohuggingface-api-haystackin 3.0), but its equally-deprecated siblingsSentenceTransformersDiversityRankerandSentenceTransformersSimilarityRankeralready maintain this same validation, so I kept it consistent here too.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.