fix(transformers): enforce the launch context_length on multimodal direct chat #5729
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #5729"
general
weight: 0.35
Jackmeson1
What happens
context_lengthis documented as the per-request input-plus-output limit, Transformers included (doc/source/user_guide/launch.rst). Atbdd7651, the MiniCPM-V 4.7, Qwen2-VL, GLM-4.1V and InternVL adapters ignore it on direct chat: a prompt that already fills the limit still reaches generation, and an oversizedmax_tokensis forwarded past the remaining budget. These CPU-mocked MiniCPM-V 4.7 direct-chat cases use synthetic preparedinput_idsand a launch config withcontext_length=8:| request |
bdd7651| with this PR ||---|---|---|
| 3 prepared prompt tokens,
max_tokens=12| generation starts withmax_new_tokens=12|max_new_tokens=5|| 7 prepared prompt tokens,
max_tokens=12|max_new_tokens=12|max_new_tokens=1|| 8 or 9 prepared prompt tokens,
max_tokens=6| attempts to start the generation thread |ContextLengthExceededbefore the generation thread starts || no
context_lengthat launch | requested/default output budget | same budget |The text Transformers batching path uses
get_max_src_lenand truncates prompt IDs inpad_prefill_tokens(transformers/utils.py); these multimodal direct-chat paths build their inputs and callgeneratewithout those checks.Change
PytorchMultiModalModel._cap_context_max_new_tokens(input_length, max_new_tokens)inmultimodal/core.py. With no launchcontext_length, it returns the adapter's resolvedmax_new_tokensunchanged. Otherwise it caps output to the remaining budget, and raises the existingContextLengthExceededwhen the prepared prompt (including media placeholders ininput_ids) leaves no room. MLX uses the same exception class. The existing chat REST handlers return HTTP 400 for non-streaming requests and an SSE error for streaming requests.ContextLengthExceededby exception type (review feedback): before streaming starts they return HTTP 400 withcontext_length_exceededandinvalid_request_errorrespectively; once streaming, Responses emitsresponse.failedwith that code and Anthropic anerrorevent of typeinvalid_request_error. The existing Responses text heuristics, rate-limit and generic-error handling are unchanged. MLX, which raises the same exception, benefits too.No docs change: this aligns the code with what
launch.rstalready says.Tests
xinference/model/llm/tests/test_multimodal_context_length.pyhas 360 cases: four adapters × two response modes × three message shapes (text, image, video) × 15 budget cases. It covers output caps, prompts at/over the limit (no generation thread orgeneratecall), one token left, omitted/Nonemax_tokens, unchanged defaults, absent/Nonelaunch limits, and input/config preservation. All cases supply synthetic prepared IDs: MiniCPM-V 4.7 mocks the processor; the other three mockbuild_inputs_from_messages. The media cases test plumbing, not real media tokenization or video support in every adapter.test_qwen2_vl.py's fixture gains an empty_pytorch_model_config, because it builds the model without__init__.I ran these outside the sandbox at
a011ed1(basebdd7651), on Linux under WSL with CPU torch and Transformers 5.17.0:PYTHONPATH=. python -m pytest xinference/model/llm/tests/test_multimodal_context_length.py xinference/model/llm/tests/test_qwen2_vl.py xinference/model/llm/tests/test_context_length.py -q --timeout=300: 403 passed on Python 3.12.15 (36 s) and Python 3.10.12 (34 s).bdd7651production files with the branch's test files:test_multimodal_context_length.pygave 144 failed and 216 passed. The failures are 96 uncapped output budgets and 48 attempts to start the generation thread for full/oversized prompts; the passing cases are unchanged-behaviour controls.xinference/api/tests/test_openai_responses.pycover both routes in both modes with the real exception, plus generic-error and rate-limit controls.test_openai_responses.py,test_anthropic_messages.pyandtest_multimodal_context_length.pytogether: 421 passed on Python 3.12.15 and 3.10.12; with the production files at the first commit, 6 failed and 415 passed.These tests do not load model weights, run GPU inference, or measure memory use. MiniCPM-V 4.7's loader requires Transformers >=5.18.0; the mocks bypass it.
AI-assisted: I used Claude Code to help write this change and its tests.
🤖 Generated with Claude Code
https://claude.ai/code/session_019TTGAv6fe859ebbCLLfLHP