Skip to content

fix: validate top_k in HuggingFaceTEIRanker#11743

Closed
Kunal-Somani wants to merge 2 commits into
deepset-ai:mainfrom
Kunal-Somani:fix/hugging-face-tei-validate-top-k
Closed

fix: validate top_k in HuggingFaceTEIRanker#11743
Kunal-Somani wants to merge 2 commits into
deepset-ai:mainfrom
Kunal-Somani:fix/hugging-face-tei-validate-top-k

Conversation

@Kunal-Somani

Copy link
Copy Markdown
Contributor

Related Issues

  • No related issue (discovered via code review and found a validation-parity gap across the rankers component family)

Proposed Changes:

HuggingFaceTEIRanker had no validation on its top_k parameter, unlike five of its six sibling ranker components (LLMRanker, LostInTheMiddleRanker, MetaFieldRanker, SentenceTransformersDiversityRanker, SentenceTransformersSimilarityRanker, TransformersSimilarityRanker), all of which raise ValueError(f"top_k must be > 0, but got {top_k}") for invalid values.

Confirmed via reproduction that HuggingFaceTEIRanker(url=..., top_k=0) and HuggingFaceTEIRanker(url=..., top_k=-5) both construct without error, and the invalid value silently flows into ranking logic via min(top_k or self.top_k, len(result)).

This PR adds the same validation pattern at:

  • __init__ time (for the default top_k)
  • run()'s optional top_k override
  • run_async()'s optional top_k override

using the exact same validation logic and error message already used by the sibling components.

How did you test it?

  • Reproduced the gap manually before writing the fix
  • Ran the full test suite locally: PYTHONPATH="" hatch run test:unit test/components/rankers/test_hugging_face_tei.py — 16 passed
  • Ran type checks: hatch run test:types — no issues in 365 source files
  • Ran formatter: hatch run fmt — all checks passed
  • Confirmed via git diff that the change is purely additive (+9 lines, 0 removed), with zero unrelated formatting changes to the existing file

Notes for the reviewer

Not a breaking change — no previously-valid top_k value is affected by this fix; only inputs that were already invalid (zero or negative) now fail clearly instead of silently producing broken behavior. Note that HuggingFaceTEIRanker is deprecated (moving to huggingface-api-haystack in 3.0), but its equally-deprecated siblings SentenceTransformersDiversityRanker and SentenceTransformersSimilarityRanker already maintain this same validation, so I kept it consistent here too.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@Kunal-Somani Kunal-Somani requested a review from a team as a code owner June 23, 2026 19:23
@Kunal-Somani Kunal-Somani requested review from davidsbatista and removed request for a team June 23, 2026 19:23
@vercel

vercel Bot commented Jun 23, 2026

Copy link
Copy Markdown

@Kunal-Somani is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/rankers
  hugging_face_tei.py
Project Total  

This report was generated by python-coverage-comment-action

@sjrl

sjrl commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

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.

@Kunal-Somani

Copy link
Copy Markdown
Contributor Author

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.

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.

3 participants