Skip to content

[Engine] serve: Qwen4-35B-A3B thinking levels (reasoning_effort "medium" was an HTTP 400) - #437

Closed
Keoian wants to merge 1 commit into
simplexai-labs:mainfrom
Keoian:serve-qwen4-thinking-levels
Closed

Keoian wants to merge 1 commit into
simplexai-labs:mainfrom
Keoian:serve-qwen4-thinking-levels

Conversation

@Keoian

@Keoian Keoian commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Problem and result

Qwen4-35B-A3B's chat template has four thinking levels, xhigh, high (its default), low and off, and stops on any other word. It has no medium. With the server as it is:

  • a request with "reasoning_effort": "medium" (the level many OpenAI-style clients send by default) is an HTTP 400: Unexpected reasoning effort medium. Supported efforts are xhigh, high, low and off.
  • "reasoning_effort": "high" reaches the model as xhigh, because that is Flash-Next's rule.

With this change the server recognises the template and uses a table of its own for it: medium is the model's default level (high), the model's own words keep their meaning, and none / off / minimal / enable_thinking: false turn the thinking off. The texts the server writes into a reply also use this model's spelling of thinking, <think>...</think>answer with no blank lines.

A request asks for Before After
nothing high high
low low low
medium HTTP 400 high
high xhigh high
xhigh, max xhigh xhigh
none, off, minimal no thinking no thinking

Related discussion

#425 (Qwen4-35B-A3B support) and #430 (the draft engine port). This PR is only the server side and does not overlap #430's files. Niko1221/Strata#7782 solves the same problem for the Strata project's server; this one is written for PolyStrata's per-family effort tables and shares no code with it.

Approach

  • ChatTemplate._detect_qwen4 renders the template once with reasoning_effort="off" and looks for the generation prompt <|im_start|>assistant\n<think></think>. The check is on the template and not on the tokenizer's name, so a fine-tune that keeps the template is served the same way.
  • frontend.use_family("qwen4") selects _EFFORT_QWEN4. Every API already reads the level through effort_kwargs, so OpenAI chat, Responses, Anthropic (effort and thinking budgets), chat_template_kwargs and the config's reasoning_effort follow without further changes. Service.family stays None (the Qwen form), so tool-call retries and the rest behave as for Flash-Next.
  • closed_with(..., compact=True): the closing of a thinking budget and of reasoning_close_retry, the empty thinking block of a tool-call retry, and a forced call are written without line breaks for this model.
  • "effort_position": "end" takes high as the default level and writes the template's own sentence in the trailing turn.
  • medium → high is a choice: the model has no such level, and high is what it does when nothing is asked. Refusing it (today's behaviour) breaks clients that always send it.

No other model is affected: a template is only treated this way when the probe matches.

Validation

Revision 0ec28e69b on main 4106d1a7c. Ubuntu Server, Python 3.14.

Passed:

  • python -m pytest terminal/serve/test_qwen4_effort.py: 15 passed, 38 subtests. It uses the published template (fixtures/qwen4_chat_template.jinja, from the header of unsloth/Qwen4-35B-A3B-GGUF UD-Q4_K_XL, SHA-256 50383841da44…) behind the mock engine, and checks that Flash-Next's template, the fourteen templates of fixtures/family_templates.json.gz and three made-up ones are not taken for Qwen4's.
  • python tools/check --only py-serve,py-tools,docs: py-tools 2,260 passed, 0 failed; docs passed; py-serve 2,364 passed, 1 failed (see Failed).
  • On the model itself: 2x Radeon RX 5700 XT (gfx1010, HIP), Unsloth UD-Q4_K_XL, the strata engine built with -DSTRATA_GEOMETRY=q4_lite from main plus the engine commits of [Engine] qwen4exp: Qwen4-35B-A3B (q4_lite) support Niko1221/Strata#7774 (89561aaaf to 8edd10483), this change on top.
    • Before the change: medium gave the HTTP 400 quoted above; low, xhigh, none, minimal and enable_thinking: false already worked.
    • After: medium, high, low, xhigh, none, chat_template_kwargs: {"reasoning_effort": "off"} and {"enable_thinking": false, "reasoning_effort": "low"} all answer 200, thinking on or off as asked; bogus is a 400 with the server's own message.
    • One tool call and its answer through /v1/chat/completions and through /v1/messages.
    • reasoning_budget_tokens: 40: the server closed the thinking with its wrap-up sentence and the answer followed.
    • tool_choice forcing a function, with thinking high and none: the call came back both times.
    • A second turn that sends the first turn's reasoning_content back reused 101 of 120 prompt tokens.

Failed:

  • serve.test_telemetry_adl.OnlyTheMonitor::test_reader_choice fails on this PC (AMD cards) and fails the same way on unmodified main 4106d1a7c.
  • serve.test_resource_warnings.Closed::test_the_cpu_and_ram_sampler_closes_its_files failed in 1 of 2 full py-serve runs of this branch (3 jobs) and passed in the other. It passes alone (3 of 3) and in one process together with the new test file (2 of 2), and the new file raises no ResourceWarning under python -X dev -W always::ResourceWarning. One full run of unmodified main did not show it. I could not tie it to this change; I am listing it in case it depends on test order.

Not run:

  • Windows and macOS. The page tests that need node were skipped (node is not installed here).
  • "effort_position": "end", reasoning_close_retry and the tool-call markup retry on the model itself: these three are covered by the unit tests only.
  • Any Qwen4 fine-tune, and any size other than UD-Q4_K_XL.

Limits

  • main does not load this model yet ([Engine] support Qwen4-35B-A3B (qwen4exp) with MTP and 128K serving #430 is the port), so this change has nothing to act on until an engine for the q4_lite shape is there. It is independent of which port lands.
  • The mapping follows the model card. If the maintainers prefer medium → low, or a 400 with a clearer message, it is one line of _EFFORT_QWEN4.

🤖 Generated with Claude Code

…k lines

Qwen4-35B-A3B's chat template names its own thinking levels (xhigh, high, low, off) and
stops on any other word.  It has no medium, so a request with reasoning_effort "medium" -
the level many OpenAI clients send by default - was a 400 ("Unexpected reasoning effort
medium"), and "high" was sent on as xhigh, which is Flash-Next's rule.

The server now asks the template what it is (ChatTemplate._detect_qwen4 renders it once
with reasoning_effort "off") and uses a table of its own for it: medium is the model's
default level (high), the model's own words keep their meaning, and none / off / minimal
turn the thinking off.  Every API reads the level through effort_kwargs, so OpenAI chat,
Responses, Anthropic (effort and thinking budgets), chat_template_kwargs and the config's
"reasoning_effort" all follow.

The template writes <think>...</think>answer with nothing between.  The texts the server
writes into a reply use that spelling for this model: the closed thinking of a thinking
budget and of reasoning_close_retry, the empty thinking block of a tool-call retry, and a
forced call.  "effort_position": "end" takes high as the default level and writes the
template's own sentence in its trailing turn.

No other model is touched: the template is recognised by rendering it, and
serve/test_qwen4_effort.py checks that Flash-Next's, the fourteen strata-poly families'
(fixtures/family_templates.json.gz) and three made-up ones are not taken for it.  The
published template is in fixtures/qwen4_chat_template.jinja (unsloth/Qwen4-35B-A3B-GGUF,
Apache 2.0).

Niko1221/Strata PR 7782 solves the same problem for the Strata project's server; this is
written for PolyStrata's per-family effort tables.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
skygunner added a commit to skygunner/PolyStrata that referenced this pull request Dec 4, 2026
…nd make the conversion chain crash-safe

Serving: Qwen4's template knows xhigh/high/low/off and no medium, so select that
dialect from the tokenizer's pre="qwen4" - qwen4exp is shared with Flash-Next,
and Service.family keeps its structural None. Anthropic budgets map 2048/8192 to
high/xhigh, config/shared/settings use the same table, and the tokenizer is read
before the config's effort is validated. With MTP, a Qwen4 request that names no
sampling now uses the model card (thinking 1.0/0.95/20) instead of greedy;
explicit request fields, shared settings and temperature=0 still win, and /props
reports the same defaults. This is the effort bug reported in simplexai-labs#437.

Tools: Tokenizer.from_dir reads an empty merge set as [] (splitlines, not
split("\n")), extract() records the qwen4 provenance (QWEN35 + ignore_merges),
and intake points the Qwen4 drafter at mtp_qwen4_rt.py while Flash-Next keeps
mtp_pack.py. iq_pack publishes native_experts.txt last, after every selected
expert artifact; the splitter and the MTP runtime write beside the target and
rename only on success; the MTP set carries a size-checked complete.json;
make_profile writes atomically. The split tests ignore Windows mmap cleanup
errors, as reported on the PR (assertions unchanged).

Runner: preflight GPU 0's free VRAM, a bindable port, RAM and per-filesystem disk
before any download or build; --check changes nothing. Install the full
requirements.txt, detect the CUDA architecture, reuse artifacts only when
complete, write the config atomically, merge shared settings without overwriting
user keys, and carry the models.json aliases into the config.

Docs: QWEN4.md and both model pages describe the effort dialect, the MTP sampling
exception, the drafter's dense-attention approximation (indexer tensors are not
converted), the two meanings of preserve_thinking, and the deliberately unchanged
--mtp-hnorm/--spec-min-p defaults; add the English model page.

Every new regression failed on the old code first. Final runs: 110 tool tests
(4 skipped) and 826 serving tests (3 skipped), all passing. No engine default that
shapes the draft distribution was changed, and no new real-inference measurement
was taken; the quoted speeds remain the earlier RTX 3080 Ti runs.
@VecSzn

VecSzn commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the PR! Qwen4 thinking-level handling (_EFFORT_QWEN4 mapping medium -> high, _detect_qwen4_efforts(), and compact handling) has already landed on main in commit 1a196d9, so we are closing this as superseded.

@VecSzn VecSzn closed this Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants