mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4932] [Other] serve: preserve_thinking / preserve_reasoning reach the chat template #5252
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5252
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/4932
Author: @dan64
Created: 11/1/2026
Status: 🔄 Open
Base:
main← Head:serve/preserve-thinking📝 Commits (1)
8f8c95cserve: preserve_thinking / preserve_reasoning reach the chat template📊 Changes
4 files changed (+187 additions, -1 deletions)
View changed files
📝
serve/frontend.py(+35 -0)📝
serve/server.py(+29 -1)📝
serve/test_frontend.py(+67 -0)📝
serve/test_server.py(+56 -0)📄 Description
Problem and result
A request's
chat_template_kwargsreaches the template with onlyenable_thinkingandreasoning_effort.preserve_thinkingandpreserve_reasoningare dropped inopenai_to_messages/anthropic_to_messagesbeforerendering, so no client can ask for the reasoning of the earlier turns to be left out of the prompt. On the Qwen
templates the branch that decides this is
preserve_thinking is undefined or preserve_thinking is true or loop.index0 > ns.last_query_index: with the kwarg dropped the first test is always true, so every earlier assistant turn is renderedwith its full
<think>block, for the whole conversation.This PR makes both names reach the template, and accepts the same two keys in the
.shared-settings.jsonChatdefaults, for clients that never send the kwarg themselves.
It does not change a default. A request that says nothing renders exactly as it does today. What it adds is the
ability to ask for the other behaviour, which did not exist before.
The two names exist because llama.cpp's
--reasoning-preserve/--no-reasoning-preservesetpreserve_reasoning(
common/arg.cpp) while Qwen's templates testpreserve_thinking; the fixed Qwen templates accept either.Related discussion
Same area as #4374 (opt-in prompt shaping in
serve/frontend.py) and the same class as #843 (an earlierserve/frontend.pyrendering fix, accepted): what the server puts into the history decides what the model sees.I searched the open and closed issues and PRs for
preserve_thinking,preserve_reasoning,shared-settingsandreasoning_effortbefore writing this; nothing covers it. If a maintainer would rather have this as an issue first,say so and I will move it there.
What it is for, and what it is not
falsedrops the reasoning of the turns before the current one and keeps the current turn's — the tool-callloop the model is in the middle of, which is the part that carries the plan across tool results. Measured on the pack's
template (a 2-turn history with a tool call in progress):
So this is not "switch the thinking off": the reasoning the model is in the middle of using is untouched whatever the
setting. What
falseremoves is the sediment of earlier turns.Approach
serve/frontend.py—preserve_kwargs()readspreserve_reasoningorpreserve_thinkingfrom the request'schat_template_kwargsand returns both names set to the same value, so a request means the same thing whichevertemplate the pack carries. Called from both converters, so the OpenAI and Anthropic dialects behave alike.
_flag()accepts a JSON boolean or the text form a hand-built body uses (
"false",0); anything unrecognised reads as off.serve/server.py—preserve_thinking/preserve_reasoningare added toclean_shared_defaults' whitelist(validated as booleans) and
Service.with_sharedfills the value into a request that does not carry one itself; arequest's own value always wins. (Adding the keys to the whitelist also matters for a second reason: see the note at
the end — an unknown key there currently clears all the shared settings, which is reported separately as #4933.)
Compatibility: opt-in and narrowing only; no wire-format change; tool calls unchanged (checked below).
Validation
Environment: Windows 11 Pro 26300, i7-12700KF, RTX 5070 Ti 16 GB (sm_120), 111.8 GiB RAM; engine 0.1.51 (CUDA,
compiled from this tree); Qwen3.8-Flash-Next Uncensored Q4_K_S,
--ple-io direct,--kv q4_0, 524288 context.Server:
serve/server.py --engine strata --config <cfg>.json --port 998x.Revision tested: a2129a95, on top of upstream
c62d5e3a. The commit was then amended to8f8c95ceto dropseven unreachable duplicate lines in
preserve_kwargs; the tests below were not re-run after that edit, which removesdead code and changes no behaviour.
Passed
python -m pytest serve/test_frontend.py serve/test_server.py -q— 393 passed, 3 skipped, 661 subtests passedin 116.80 s, including the 11 new tests (
PreserveReasoningSwitch,SharedPreserveReasoning).python -m pytest serve/test_frontend.py serve/test_effort_paths.py serve/test_logprobs.py serve/test_structured.py serve/test_tool_routes.py -q— 93 passed, 175 subtests passed.finish_reason: tool_calls, and the call came back as areal call —
{"name": "read", "arguments": {"path": "C:\\Windows\\win.ini"}}.other apps now use the Chat settings: reasoning_effort=medium, preserve_thinking=False.Prompt size, fixed transcript (the honest number; see Limits for why the first figure I published was wrong).
Same conversation text in both arms, identical assistant turns, only the setting differs;
prompt_tokensfrom theresponse's
usage:preserve_thinking: falseThe saving grows with the number of turns, as it should: it is one reasoning block per earlier turn, and that is a
fixed part of each turn's prompt.
Failed: none observed.
Not run
That needs a paired A/B on the same session (
truevsfalse) and I have not run one. Nothing in this PR should beread as a quality claim in either direction.
falsereports smaller prompts(56/341/429/499 vs 56/480/664/790), but those runs also produced different assistant text, so the difference is not
attributable to the setting alone. How much of the saving the prefix cache absorbs in a long session is unmeasured.
on a template that reads neither name the kwargs are passed and have no effect.
Limits
(97.5%)" on a two-turn fixture whose history was almost entirely reasoning and whose last message was a user turn —
a shape where the current turn has no reasoning to keep, so the whole reasoning left the prompt at once. Real
conversations end with the assistant's turn in progress, so part of the reasoning stays by design. The table above
replaces it.
last_query_indexrule, not by the server: everything from the last realuser query onward keeps its reasoning, by design.
client that asks otherwise. That is deliberate, and it means this is not a policy knob.
preserve_reasoningwins when both are present in the shared settings; the templates use the same precedence.A separate defect this run into (reported as #4933, fixed in #4934)
clean_shared_defaultsraisesValueErrorfor a key it does not know, which is right forPOST /settings(theclient gets a 400 naming the key) but was also used at start-up inside
except (OSError, ValueError): svc.shared = {}.A
.shared-settings.jsonholding one unrecognised key therefore emptied every shared setting,reasoning_effortincluded, with nothing in the log. Adding these two keys to the whitelist hides that symptom for them; the behaviour
for any other unknown key is #4933, with the fix in #4934. The two PRs touch different lines and do not depend on each
other.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.