Skip to content

fix(openai_api_compatible): honor top_n=0 in text rerank - #4142

Open
lisry-1 wants to merge 1 commit into
langgenius:mainfrom
lisry-1:fix/rerank-topn-zero
Open

lisry-1 wants to merge 1 commit into
langgenius:mainfrom
lisry-1:fix/rerank-topn-zero

Conversation

@lisry-1

@lisry-1 lisry-1 commented Oct 28, 2026

Copy link
Copy Markdown

What changed

The text-mode _invoke truncated rerank results with a truthiness check:

if top_n:
    rerank_docs = rerank_docs[:top_n]

top_n=0 is falsy, so a caller asking for zero results received every result the gateway returned. The multimodal mode already used an is not None check, and the payload builder in the same text-mode method explicitly documented handling top_n=0, so the two paths disagreed:

path result truncation top_n=0 behaviour
_invoke (text) if top_n: ignored, returns all results
_invoke_multimodal if top_n is not None: returns empty list

This switches the text mode to if top_n is not None:, aligning it with the multimodal path and with the intent already expressed in the payload builder's comment.

Verification

New test file tests/test_rerank_topn_zero.py (3 cases). Red/green control on the unmodified source:

source reverted:  1 failed, 2 passed
with the fix:     3 passed

Rerank-related suite and full plugin suite:

uv run pytest tests/test_rerank_*.py -q  ->  29 passed
uv run pytest tests -q                   ->  127 passed

ruff check and ruff format --check are clean on the new/changed lines.

Version

manifest.yaml bumped 0.0.73 -> 0.0.74 per the repo convention.

Note: this is independent of my other open PRs (#4131, #4141), which also bump to 0.0.74 from the same base. Happy to rebase and re-bump whichever lands last.

The text-mode _invoke truncated results with a truthiness check on top_n,
which treats top_n=0 as falsy and skips the truncation, so a caller asking
for zero results got every result back. The multimodal mode already used an
is-not-None check, and the payload builder explicitly documented handling
top_n=0, so the two paths disagreed.

Switch to an is-not-None check so top_n=0 truncates to an empty list,
matching the multimodal path and the comment on the payload builder.
Bumps the plugin version to 0.0.74.
@lisry-1
lisry-1 force-pushed the fix/rerank-topn-zero branch from 3c3c929 to 71d8568 Compare October 29, 2026 09:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant