feat(rerank): bound reranker input width (max_chars_per_doc) - #2999
Open
chethanuk wants to merge 1 commit into
Open
feat(rerank): bound reranker input width (max_chars_per_doc)#2999chethanuk wants to merge 1 commit into
chethanuk wants to merge 1 commit into
Conversation
This was referenced Jul 4, 2026
chethanuk
force-pushed
the
feat/2880-rerank-max-chars
branch
from
July 4, 2026 02:56
7dc1e47 to
9d96ae7
Compare
Contributor
Author
|
Please review cc: @qin-ctx @ZaynJarvis @zhoujh01 @yufeng201 @chenjw - Please let me know if you require any changes, or if the project is currently accepting contributions |
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.
Summary
Adds an optional
RerankConfig.max_chars_per_docthat truncates each document to N characters before it is sent torerank_batchinsideHierarchicalRetriever._rerank_scores. This bounds the reranker's input width so a single oversized abstract can no longer overflow the model and fail the whole batch open to vector scores — the exact failure #2880 describes.This is v0 of a deliberately small PR ladder (follow-ups: truncation-observability counter, provider-portable
score_threshold, forwarding token-truncation to providers).max_docs+ score-fusion were evaluated and dropped — the rerank batch is already count-bounded byGLOBAL_SEARCH_TOPK=10andlimit*2, so per-doc width is the axis that actually matters here.How it works
flowchart TD A["global_results → document abstracts"] --> B{"cap = max_chars_per_doc"} B -- "cap == 0 (default, OFF)" --> C["model_inputs = documents<br/>byte-identical parity"] B -- "cap > 0" --> D["truncate each doc to cap chars<br/>MODEL INPUT only"] C --> E["rerank_batch(query, model_inputs)"] D --> E E --> F["scores scatter back onto full result dict<br/>stored / returned abstract untouched"] F --> G["score > threshold → keep / recurse"]Behavior
default=0= unbounded, byte-identical parity with previous behavior (if cap > 0 else documentspasses the original list through untouched).Field(default=0, ge=0, strict=True)rejects negatives and string coercion;extra="forbid"makes a typo'd key fatal._rerank_scores, at both rerank sites (global + child recursion); the query is never truncated.str[:cap]) — safe for CJK/multibyte, not grapheme-aware (documented). The char≠token gap for CJK is a known limitation closed by the token-forwarding follow-up.Tests / docs
Extends
tests/retrieve/test_hierarchical_retriever_rerank.pywith the full matrix:cap=0parity, truncation at both sites, query-not-truncated, boundaries (0/≥len/len+1/1), multibyte + empty, fail-open with the cap on (length invariant intact), QUICK-mode skip, and config rejection (-1,"5", typo key). The pre-existing default-off parity guard remains. Unit suite green;ruff check+formatclean. Rerank-table rows added todocs/en/guides/01-configuration.mdanddocs/zh/guides/01-configuration.md.Recommended-ON value:
2000chars (~500 EN tokens) for 512-token rerankers (BGE/MiniLM/VikingDB) — above the instability floor, below the model window. CJK caveat: 2000 chars ≠ 500 tokens, so a char cap can still overflow a 512-token reranker (closed by the token-forwarding follow-up).Review
Inline review-bot feedback (gemini-code-assist / qodo / codeant-ai) was triaged comment-by-comment and independently verified: the one valid item (test-log warning-spam) was fixed; the
getattrguard and the strict-int "env var" concern were declined with rationale (the config loader expands unquoted${VAR}into a JSON number thatstrict=Trueaccepts, and this is the only strict numeric field in the repo). No production behavior changed.Closes #2880