[PR #4932] [Other] serve: preserve_thinking / preserve_reasoning reach the chat template #5252

Open
opened 2026-11-07 16:33:49 +07:00 by bsakaguchi · 0 comments
Owner

📋 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)

  • 8f8c95c serve: 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_kwargs reaches the template with only enable_thinking and reasoning_effort.
preserve_thinking and preserve_reasoning are dropped in openai_to_messages / anthropic_to_messages before
rendering, 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 rendered
with 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.json Chat
defaults, 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-preserve set preserve_reasoning
(common/arg.cpp) while Qwen's templates test preserve_thinking; the fixed Qwen templates accept either.

Same area as #4374 (opt-in prompt shaping in serve/frontend.py) and the same class as #843 (an earlier
serve/frontend.py rendering 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-settings and
reasoning_effort before 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

false drops the reasoning of the turns before the current one and keeps the current turn's — the tool-call
loop 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):

setting       reasoning of an older turn   reasoning of the current turn
absent        present                      present
true          present                      present
false         absent                       present

So this is not "switch the thinking off": the reasoning the model is in the middle of using is untouched whatever the
setting. What false removes is the sediment of earlier turns.

Approach

serve/frontend.py — preserve_kwargs() reads preserve_reasoning or preserve_thinking from the request's
chat_template_kwargs and returns both names set to the same value, so a request means the same thing whichever
template 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_reasoning are added to clean_shared_defaults' whitelist
(validated as booleans) and Service.with_shared fills the value into a request that does not carry one itself; a
request'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 to 8f8c95ce to drop
seven unreachable duplicate lines in preserve_kwargs; the tests below were not re-run after that edit, which removes
dead code and changes no behaviour.

Passed

  • python -m pytest serve/test_frontend.py serve/test_server.py -q — 393 passed, 3 skipped, 661 subtests passed
    in 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.
  • Tool calls on the same revision with the switch active: finish_reason: tool_calls, and the call came back as a
    real call — {"name": "read", "arguments": {"path": "C:\\Windows\\win.ini"}}.
  • Start-up with the shared default in place logs
    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_tokens from the
response's usage:

turns of history default (preserve) preserve_thinking: false saved
2 429 91 79%
4 825 149 82%
6 1221 207 83%
8 1617 265 84%

The 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

  • Quality. Whether dropping the earlier reasoning helps or hurts answers on a real workload is not measured here.
    That needs a paired A/B on the same session (true vs false) and I have not run one. Nothing in this PR should be
    read as a quality claim in either direction.
  • What the conversation cache gives back. On live 4-turn conversations the arm with false reports 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.
  • The templates themselves are not touched. Whether a pack's template honours the kwarg is the template's business;
    on a template that reads neither name the kwargs are passed and have no effect.
  • No GPU/CUDA/HIP build is involved: Python only.

Limits

  • The first figure I put in this PR was misleading and I am replacing it. It read "2064 -> 52 prompt tokens
    (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.
  • The reduction is bounded by the template's last_query_index rule, not by the server: everything from the last real
    user query onward keeps its reasoning, by design.
  • A client that already sends the kwarg keeps its own value, so the shared default cannot force the behaviour on a
    client that asks otherwise. That is deliberate, and it means this is not a policy knob.
  • preserve_reasoning wins 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_defaults raises ValueError for a key it does not know, which is right for POST /settings (the
client gets a 400 naming the key) but was also used at start-up inside except (OSError, ValueError): svc.shared = {}.
A .shared-settings.json holding one unrecognised key therefore emptied every shared setting, reasoning_effort
included, 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.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4932 **Author:** [@dan64](https://github.com/dan64) **Created:** 11/1/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `serve/preserve-thinking` --- ### 📝 Commits (1) - [`8f8c95c`](https://github.com/Niko1221/Strata/commit/8f8c95ce4f753ca8c57cfc30faca90344c31e99b) serve: preserve_thinking / preserve_reasoning reach the chat template ### 📊 Changes **4 files changed** (+187 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+35 -0) 📝 `serve/server.py` (+29 -1) 📝 `serve/test_frontend.py` (+67 -0) 📝 `serve/test_server.py` (+56 -0) </details> ### 📄 Description ## Problem and result A request's `chat_template_kwargs` reaches the template with only `enable_thinking` and `reasoning_effort`. `preserve_thinking` and `preserve_reasoning` are dropped in `openai_to_messages` / `anthropic_to_messages` before rendering, 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 rendered with 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.json` Chat defaults, 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-preserve` set `preserve_reasoning` (`common/arg.cpp`) while Qwen's templates test `preserve_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 earlier `serve/frontend.py` rendering 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-settings` and `reasoning_effort` before 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 `false` drops the reasoning of the turns **before** the current one and **keeps the current turn's** — the tool-call loop 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): ``` setting reasoning of an older turn reasoning of the current turn absent present present true present present false absent present ``` So this is not "switch the thinking off": the reasoning the model is in the middle of using is untouched whatever the setting. What `false` removes is the sediment of earlier turns. ## Approach **`serve/frontend.py`** — `preserve_kwargs()` reads `preserve_reasoning` or `preserve_thinking` from the request's `chat_template_kwargs` and returns **both** names set to the same value, so a request means the same thing whichever template 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_reasoning` are added to `clean_shared_defaults`' whitelist (validated as booleans) and `Service.with_shared` fills the value into a request that does not carry one itself; a request'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 to **8f8c95ce** to drop seven unreachable duplicate lines in `preserve_kwargs`; the tests below were **not** re-run after that edit, which removes dead code and changes no behaviour. **Passed** - `python -m pytest serve/test_frontend.py serve/test_server.py -q` — **393 passed, 3 skipped, 661 subtests passed** in 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. - Tool calls on the same revision with the switch active: `finish_reason: tool_calls`, and the call came back as a real call — `{"name": "read", "arguments": {"path": "C:\\Windows\\win.ini"}}`. - Start-up with the shared default in place logs `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_tokens` from the response's `usage`: | turns of history | default (preserve) | `preserve_thinking: false` | saved | |---|---:|---:|---:| | 2 | 429 | 91 | 79% | | 4 | 825 | 149 | 82% | | 6 | 1221 | 207 | 83% | | 8 | 1617 | 265 | 84% | The 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** - **Quality.** Whether dropping the earlier reasoning helps or hurts answers on a real workload is not measured here. That needs a paired A/B on the same session (`true` vs `false`) and I have not run one. Nothing in this PR should be read as a quality claim in either direction. - **What the conversation cache gives back.** On live 4-turn conversations the arm with `false` reports 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. - **The templates themselves** are not touched. Whether a pack's template honours the kwarg is the template's business; on a template that reads neither name the kwargs are passed and have no effect. - No GPU/CUDA/HIP build is involved: Python only. ## Limits - **The first figure I put in this PR was misleading and I am replacing it.** It read "2064 -> 52 prompt tokens (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. - The reduction is bounded by the template's `last_query_index` rule, not by the server: everything from the last real user query onward keeps its reasoning, by design. - A client that already sends the kwarg keeps its own value, so the shared default cannot force the behaviour on a client that asks otherwise. That is deliberate, and it means this is not a policy knob. - `preserve_reasoning` wins 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_defaults` raises `ValueError` for a key it does not know, which is right for `POST /settings` (the client gets a 400 naming the key) but was also used at start-up inside `except (OSError, ValueError): svc.shared = {}`. A `.shared-settings.json` holding one unrecognised key therefore emptied **every** shared setting, `reasoning_effort` included, 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5252
No description provided.