mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #5346] [Other] serve: preserve multi-turn reasoning by default and skip empty historical think blocks #5396
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#5396
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/5346
Author: @bsorensen110
Created: 11/5/2026
Status: 🔄 Open
Base:
main← Head:fix/preserve-reasoning-upstream📝 Commits (2)
3e94381serve: preserve multi-turn reasoning by default and skip empty historical think blocksf7ad2f3serve: keep reasoning preservation opt-in and honor all request aliases📊 Changes
7 files changed (+190 additions, -21 deletions)
View changed files
📝
serve/chat_golden.json(+7 -7)📝
serve/chat_template.jinja(+1 -1)📝
serve/frontend.py(+59 -6)📝
serve/responses.py(+5 -2)📝
serve/server.py(+19 -3)➕
serve/test_preserve_reasoning.py(+77 -0)📝
serve/test_server.py(+22 -2)📄 Description
Problem and result
Two things in the multi-turn reasoning path, found while investigating reasoning degradation on long agentic conversations with Qwen3.8-Flash-Next.
1. A prompt-serialization bug: historical assistant turns with empty reasoning are rendered as an empty think block.
serve/chat_template.jinjadecides to keep a historical turn's thinking onpreserve_thinking is undefined or preserve_thinking is true or loop.index0 > ns.last_query_index— it never checks thatreasoning_contentis non-empty. A client that echoes an assistant turn back withreasoning_content: ""— which is what happens whenever the reasoning item is present but its content was dropped (e.g./v1/responseshistory where the reasoning summary is[], or clients that strip reasoning text to save tokens) — gets this into the next prompt:So the conversation history accumulates one empty
<think>\n\n</think>block per reasoning-less assistant turn — a few-shot example of "reason by saying nothing", right in the few-shot region of every subsequent request. After the fix the same history renders<|im_start|>assistant\nfirst answer— the empty block is simply not emitted. Turns with reasoning are rendered exactly as before, and the current turn's think-open is untouched.2.
preserve_reasoning/preserve_thinkinghad no plumbing through the serve front end. llama.cpp's name ispreserve_reasoning, Transformers/Qwen templates testpreserve_thinking, and there was no way to reach either: a request'schat_template_kwargsentries were dropped before rendering,/v1/responseshad no path at all, the shared Chat settings rejected the keys, andChatTemplate._detect_caps()inferred template support from one default render. After the fix:clear_thinking) are accepted on/v1/chat/completions,/v1/anthropicmessages and/v1/responses, at request level or insidechat_template_kwargs, and reach the template normalized to both spellings;.shared-settings.jsonacceptspreserve_reasoning/preserve_thinking/clear_thinking(booleans) as defaults, and a request's own value always wins;supports_preserve_reasoningonly when the reasoning block actually appears on and disappears off;openai_to_messages,chat_template_kwargs.enable_thinking: falseused to reset the whole kwargs dict, discarding whatever else had been computed; it now merges, so the two settings coexist.Related discussion
preserve_thinking/preserve_reasoningfromchat_template_kwargsand the shared settings through the OpenAI/Anthropic converters, without changing a default. This PR does the same job for the same clients plus the/v1/responsespath, theclear_thinkingalias, the differential caps probe and the template guard in point 1, but it also turns the default on for templates that prove support — so if the maintainer prefers the strict opt-in of #4932, the two can be merged by keeping that PR's defaults policy and adding this one's template guard + responses path. Expect conflicts inserve/frontend.py/serve/server.py.Approach
serve/chat_template.jinja— one condition:{%- if reasoning_content and (preserve_thinking is undefined or ...) %}. Empty reasoning never opens a think block.serve/frontend.py—_normalize_preserve_reasoning()(accepts both spellings and their text forms;preserve_thinkingwins when both are given at one level),preserve_reasoning_kwargs()(extracts from a request and/or itschat_template_kwargs, maps the inverseclear_thinking), called fromChatTemplate.render()and both request converters.serve/responses.py—template_kwargs()merges the shared defaults first and request-level values on top.serve/server.py— the three keys joinSHARED_KEYS(validated as booleans),Service.with_shared()fills them into a request that carries none, and_responses_preparenow passessvc.sharedtotemplate_kwargs().serve/test_preserve_reasoning.py— the serialization regression test, plus aPreserveReasoningKwargscase covering the aliases, text forms,clear_thinking, both request converters and the/v1/responsesshared/request merge;serve/test_server.pygains a case for the shared default, request override and boolean validation, and itsLiteralThinkTagsvector drops one open/close token-id pair — the old count was counting the empty-think-block artifact this PR removes.serve/chat_golden.jsonvectors updated where the guard changes rendering (one historical tool-call turn no longer serializes<think>\n\n</think>before its call).Validation
Revision: this branch, one commit (
3e94381a9) on upstream main9f7d977d2(0.2.10 line). Pure Python path —jinja2template rendering withMockEngine/ByteTokenizer, no GPU or model files involved. Environment: Linux, Python 3.14, pytest.Passed
serve/test_preserve_reasoning.pyagainst the base template:AssertionError: '<think>\n\n</think>' unexpectedly found in '<|im_start|>system ... <|im_start|>assistant\n<think>\n\n</think>\n\nfirst answer<|im_end|> ...'. With the guard:1 passed.serve/frontend.py:ImportError: cannot import name '_normalize_preserve_reasoning'— the normalization the tests exercise does not exist on main.python -m pytest serve/test_preserve_reasoning.py serve/test_frontend.py serve/test_responses.py serve/test_effort_paths.py -q→ 81 passed, 1 skipped, 110 subtests passed.python -m pytest serve/ tools/test_strata_tokenizer.py -q→ 1449 passed, 25 skipped, 14178 subtests passed, with 1 failed:serve/test_keepawake.py::KeepAwakeTests::test_service_holds_for_the_whole_request_including_early_close. That failure is pre-existing: it reproduces identically (same assertion,AssertionError: False is not trueon the power-keepalive call list) in a detached worktree of clean upstream9f7d977d2on this machine, and none of the files this PR touches are on the keep-awake path.Failed: none attributable to this change — the single failure above is the pre-existing baseline case, identical on clean main.
Not run
What this does NOT fix
chat_template.jinjashipped inside tokenizer packs) carry the same unguarded line. This PR fixes the serve fallback template; packs need the same one-line guard at pack build time. (Verified on an installed Qwen3.8-Flash-Next IQ3_S pack template: same empty-think render.)strata.r1:) still cannot be replayed on/v1/responses— by design.Test plan
serve/+ tokenizer golden vectors pass (counts above)Reproduction
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.