skip to content

A Sentence Transformers CrossEncoder reranker misses answers near the end of long passages; how does max_seq_length truncate a (query, passage) pair?

level: seniorimportance: should knowfreq 9%

answer

  1. one budget for both texts
  2. tokenizer model_max_length
  3. longest_first trims the passage
  4. max_length at load, max_seq_length after
  5. split long documents before scoring

basics

~20 s

A CrossEncoder tokenizes query and passage as one sequence capped at max_seq_length, truncating with longest_first, which trims the longer text, usually the passage tail. Text past the cap is never seen, so split long documents into shorter passages.

solid answer

~40 s

A CrossEncoder encodes the query and passage together, so its length limit is shared by both texts plus the special tokens. `max_seq_length` reads the tokenizer's `model_max_length`, which Sentence Transformers caps at the model's `max_position_embeddings` when you do not set it. Text is tokenized with `truncation="longest_first"`, which removes tokens from whichever text is longer — in reranking almost always the passage — so the end of a long passage silently disappears and its score reflects only the beginning. You set the limit with the `max_length` constructor argument or the `model.max_seq_length` property (renamed from `max_length` in v5.4), but you cannot usefully exceed the position-embedding limit. The real fix is to split long documents into passages that fit, score each, and aggregate per document.

go deeper

for a junior

Recall that a CrossEncoder reads query and passage as one sequence, so their combined length is capped and long passages lose their ends.

for a middle

Explain where the cap comes from, the tokenizer's model_max_length capped at max_position_embeddings, and how longest_first decides which text loses tokens.

for a senior

Show you measure how many real pairs exceed the cap, then fix it by chunking documents into passages that fit and aggregating chunk scores per document.

for a principal

Weigh a longer-window reranker against chunking and aggregation, considering throughput per pair, how answers distribute inside documents, and evaluation cost.

## One sequence, one budget A **bi-encoder** encodes a query and a passage separately, each with its own length limit. An encoder-style **CrossEncoder** reranker does not: it concatenates the pair into a single input of the form `[CLS] query [SEP] passage [SEP]` (the exact special tokens depend on the tokenizer) and runs one forward pass. The length limit therefore applies to **query + passage + special tokens together**. In Sentence Transformers that limit is **`max_seq_length`**: - It is read from the tokenizer's **`model_max_length`**. - When you load a model without overriding it, the library caps `model_max_length` at the model config's **`max_position_embeddings`**, the largest position the network was built for. - If you pass **`max_length=...`** to the `CrossEncoder` constructor, it is forwarded as `model_max_length` to the tokenizer. ## How the pair gets truncated Text inputs are tokenized with `padding=True` and **`truncation="longest_first"`**. That is a standard tokenizer strategy for pairs: while the pair is too long, it removes a token from whichever of the two sequences is currently longer. In reranking the query is a handful of tokens and the passage is hundreds, so in practice: 1. The **query survives intact**. 2. The **passage is cut from its end** until the pair fits. 3. Nothing warns you. The score is computed over the truncated pair and looks perfectly normal. That produces the symptom in the question: a passage whose answer sits in its last paragraphs gets a low score, because the model literally never reads the answer. ## Setting the limit | Where | Spelling | Notes | |---|---|---| | At load | `CrossEncoder(name, max_length=512)` | keyword-only since v5.4; forwarded to the tokenizer's `model_max_length` | | After load | `model.max_seq_length = 512` | the property was called `max_length` before v5.4; the old name still works with a deprecation warning | | One call only | `model.predict(pairs, processing_kwargs={"text": {"max_length": 256, "truncation": True}})` | overrides tokenizer settings for that call | | At load, via the tokenizer | `processor_kwargs={"model_max_length": 512}` | `processor_kwargs` replaced `tokenizer_kwargs` in v5.4 | Two cautions: - **Raising the limit is bounded by the model.** A checkpoint built with 512 positions cannot attend over 1,024 tokens; pushing `max_seq_length` past `max_position_embeddings` does not buy context. - **Lowering it is a legitimate speed lever.** Attention cost grows with sequence length, so a reranker over short passages can run with a smaller cap — as long as your passages actually fit. ## Finding the limit a checkpoint actually uses Do not assume a number. After loading, read **`model.max_seq_length`**: - For a text reranker it is the tokenizer's `model_max_length`, already capped at the model's `max_position_embeddings` unless you overrode it. - For a model without a tokenizer, the property falls back to `max_position_embeddings` from the model config. - Two rerankers with similar names can have different limits, so check each checkpoint rather than carrying a value over from another model. Record the value with your evaluation results: a ranking-quality number is only meaningful for the length limit it was measured at. ## Fixing the missed answers Since the window cannot grow arbitrarily, fit the data to the window: - **Score passages, not documents.** Split each long document into overlapping chunks that, together with a typical query, fit inside `max_seq_length`. Rerank the chunks. - **Aggregate per document if you return documents.** A common choice is the maximum chunk score per document; whichever you choose, apply it consistently. - **Measure before and after.** Count how many candidate pairs exceed the limit with the model's own tokenizer (`model.tokenizer`) on a sample, so you know whether truncation is a real problem for your corpus. - **Pick a reranker with a longer window** if your passages are inherently long; compare the checkpoints' position limits rather than assuming. How to choose chunk sizes and overlaps for retrieval is a chunking design question in its own right; the CrossEncoder-specific point is that the **pair** has to fit, so the query's length comes out of the passage's budget. ## Quick diagnostic ```python tok = model.tokenizer limit = model.max_seq_length too_long = sum(len(tok(q, p)["input_ids"]) > limit for q, p in sample_pairs) print(f"{too_long}/{len(sample_pairs)} pairs exceed {limit} tokens") ``` If a meaningful share of pairs exceeds the limit, truncation is changing your rankings.

  • Does a CrossEncoder truncate the query too?
    It can. `longest_first` removes tokens from whichever text is currently longer, so with a long query and a short passage the query is trimmed. In normal reranking the passage is the longer side, which is why the passage tail is what usually disappears.
  • Why might you deliberately lower max_seq_length on a reranker?
    Attention cost rises with sequence length, so a smaller cap makes every pair cheaper to score. It is safe only when your query-plus-passage pairs already fit; measure with the model's tokenizer on real data before lowering it, because the cut is silent.

  • The query and the passage each get their own full token limit
  • The CrossEncoder raises an error when a pair is too long
  • Setting max_seq_length to 4096 lets any checkpoint read 4096 tokens
  • The max_length constructor argument was removed in v5.4
  • Truncation removes the query first

Assumes Sentence Transformers v6; the CrossEncoder max_length property was renamed to max_seq_length and tokenizer_kwargs to processor_kwargs in v5.4.