#4161·omlx

/v1/rerank silently truncates long documents at 512 tokens; request-level and per-model max_length overrides are not reachable

Author: geraldtscharreCreated Oct 1, 2026Updated Oct 1, 2026

Describe the bug

POST /v1/rerank silently truncates each query-document pair at 512 tokens for encoder-based rerankers (e.g. bge-reranker-v2-m3 MLX ports), even for models whose configs support far longer inputs.

This is a per-call default inside the engine, not a hardcode — models/reranker.py already accepts a max_length argument with model-appropriate defaults (512 for seq-classification, 8192 for CausalLM rerankers). The problem is that no caller surface can reach it:

  1. The RerankRequest schema (api/rerank_models.py) has no max_length field — an explicit max_length in the request body is silently discarded by pydantic.
  2. The route handler (server.py, create_rerank) calls engine.rerank(query=..., documents=..., top_n=...) — max_length is never passed, so the engine always falls back to the 512 default for encoder rerankers.
  3. Model settings (PUT /admin/api/models/{id}/settings) don't help either: both max_context_window and max_tokens are ignored by the rerank path (get_max_context_window has no call sites in the rerank route).

For long documents this produces misranks rather than truncation noise: a rich full-length passage that mentions query terms throughout scores below short 200-char prefixes from other documents, because its middle content is cut away. For retrieval stacks reranking ~1,000–3,000-token chunks this systematically demotes the best documents.

To Reproduce

  1. Load any encoder reranker (e.g. bge-reranker-v2-m3 MLX port).
  2. Send to /v1/rerank:
    • a ~2,500–3,000-token passage that genuinely answers the query
    • two short (~200-char) prefixes of unrelated documents mentioning the query keywords
  3. Observe the full passage ranked below the prefixes. Include an explicit max_length: 8192 in the request body — it is ignored (scores are byte-identical without it). Setting max_context_window: 8192 via the model settings API also has no effect on rerank.

Expected behavior

Either max_length is honored from the request body (preferred), or it is configurable per-model via model settings — the same way the embeddings route was fixed. Silent truncation of long inputs should at minimum be logged once per request (as the embeddings fix did).

Additional context

The embeddings route had the identical defect and was fixed in #1687 / #1694 by exposing and plumbing the limit. The rerank route never received the equivalent fix. The engine-side plumbing (max_length parameter with per-model-type defaults) is already complete in models/reranker.py — only the HTTP surface is missing, which makes this a two-file, ~10-line change.

Tested on 0.7.0 (installed app bundle) and against current main.


Recommended solution (pattern of #1694, verified against main — happy to contribute a PR if a maintainer gives the nod, otherwise this diff is complete as-is):

diff
--- a/omlx/api/rerank_models.py
+++ b/omlx/api/rerank_models.py
@@ -42,6 +42,15 @@ class RerankRequest(BaseModel):
     If not specified, returns all documents.
     """
 
+    max_length: int | None = None
+    """
+    Maximum token length for each query-document pair.
+    If not specified, uses a model-appropriate default (512 for encoder-based
+    rerankers, 8192 for CausalLM rerankers). Longer inputs are silently
+    truncated, so requests reranking long documents should pass an explicit
+    max_length matching the input sizes.
+    """
+
     return_documents: bool = True
     """Whether to include document text in the response."""
 
diff --git a/omlx/server.py
+++ b/omlx/server.py
@@ -3694,6 +3694,8 @@ async def create_rerank(
     - String documents or dict documents with 'text' field
     - Optional top_n to limit results
+    - Optional max_length to control per-pair token truncation
+      (defaults: 512 for encoder rerankers, 8192 for CausalLM rerankers)
     - Optional return_documents to include document text in response
     """
@@ -3726,6 +3728,7 @@ async def create_rerank(
         output = await engine.rerank(
             query=request.query,
             documents=documents_raw,
             top_n=request.top_n,
+            max_length=request.max_length,
         )