{{ message }}
Repository navigation
fix(rerankers): validate provider result indexes instead of panicking - #13583
Draft
dudanogueira wants to merge 1 commit into
Draft
dudanogueira wants to merge 1 commit into
dudanogueira wants to merge 1 commit into
Conversation
Every API reranker (cohere, voyageai, jinaai, nvidia, contextualai) wrote scores into a slice sized by the number of results, indexed by the provider-supplied result index, with no bounds check. When a provider returned fewer results than documents, e.g. reranker-contextualai with topN below the number of hits, the rerank either panicked or attached scores to the wrong objects, and rank_result.go then indexed past the end of DocumentScores outside any recover. - Add ent.ToDocumentScores, a shared mapper that rejects out-of-range and duplicate indexes and marks documents without a result as Unscored. - Drop unscored objects from reranked results, so topN limits them. - Error when a reranker returns a score count that does not match the result count, instead of panicking. - Report every 422 detail from NVIDIA instead of repeating the first. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.



Motivation
Every API reranker (cohere, voyageai, jinaai, nvidia, contextualai) trusts the result
indexthe provider sends back.toDocumentScoresallocatedlen(results)slots and wrote todocumentScores[result.Index]without a bounds check. So whenever a provider returns fewer results than documents, or an unexpected index:panic occurred: runtime error: index out of range.DocumentScoresis shorter than the search results.rank_result.gothen indexesresult.DocumentScores[i]in the query path, outside any recover.Users can trigger this today:
reranker-contextualaihas atopNclass setting, and anytopNlower than the number of hits sends back fewer results than documents.Separately, the NVIDIA 422 error handler repeated
Detail[0]for every detail, so only the first validation message was ever shown.Approach
ent.ToDocumentScores(usecases/modulecomponents/ent/rank_result.go): a generic mapper that every API reranker now uses in place of its own copy. Out-of-range and duplicate indexes are errors. Documents without a result are marked with a newDocumentScore.Unscoredfield. Its zero value means "scored", so producers that always score every document (reranker-transformers, reranker-dummy) need no change.rank_result.go: errors when the number of scores doesn't match the number of results, and dropsUnscoredobjects before sorting. SotopNnow actually limits the reranked results, instead of crashing. The module pipeline already reassigns the returned slice (toBeExtended = resArray), so a shorter result set is handled downstream.Detail[i]).Key areas for review
topNunusable whenever it is below the number of hits.topNwith chunking: contextualai still appliestopNper chunk of 1000 documents. With more than 1000 hits you can get more thantopNresults back. That was true before this PR too, and it no longer crashes.Testing
Each regression test was confirmed to fail on
mainbefore the fix:ent.ToDocumentScorestable tests: reordering, partial results, no results, zero scores, past-the-end, negative and duplicate indexes.rankprovider tests: sorting, dropping unscored objects, all unscored, and too few or too many scores. Onmain, too few scores panics withindex out of range [1] with length 1.Rankagainst a mocked server. Onmain, both returnpanic occurred: ....main, it reportedfirst problem first problem.go test -racepasses formodules/reranker-...,usecases/modulecomponents/additional/...,usecases/modulecomponents/entandusecases/modules. golangci-lint v2.13.1 is clean.🤖 Generated with Claude Code