mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[GH-ISSUE #1178] serve: Special tokens (<|im_end|>) emitted during model thinking cause tasks to terminate early #491
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#491
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?
Originally created by @TwoAnts on GitHub (Oct 6, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/1178
Recently, I’ve been using a model (Qwen3.8 Flash Next IQ3_S) to fix Strata’s code related to special tokens. The model tends to output special tokens in its thinking, which causes the task to terminate immediately.
Strata may need to add some fallback handling to mitigate this issue.
Minimal test input:
Please check whether it terminates early and whether it can still produce output normally.
I can reproduce this locally multiple times: it either terminates during thinking, or fails to output the answer normally.
@ladno64 commented on GitHub (Oct 6, 2026):
у меня такая же проблема.
@TwoAnts commented on GitHub (Oct 6, 2026):
I made some small patches using DeepSeek: e5437d2
The main approach is to replace specific end tokens with their corresponding text and feed them back in to continue generation. This change can mitigate the issue, but it does not guarantee a complete fix. It may serve as a reference.
@ladno64 commented on GitHub (Oct 6, 2026):
у меня очередь команд на отправку в диалоге. пишу "1" и ллм понимает что надо продолжать работать.))
@TwoAnts commented on GitHub (Oct 6, 2026):
I still hope it can be handled automatically. I feel that tools like llama.cpp have more complete handling for output, though that might be a big undertaking.
@ladno64 commented on GitHub (Oct 6, 2026):
Strata-main\serve\chat_template.jinja отправляйте его в крупную нейросеть вроде дипсика и пусть поищет ошибки.
@47Hunter47 commented on GitHub (Oct 12, 2026):
This is the shape #1814 describes and my PR #1840 targets: a turn that ends inside its thinking with no answer, so the client gets an empty reply with
finish_reason: stop.What I measured today on 0.1.43 (RTX 3090, IQ3_XXS, 250K context,
--kv int8, guard on):totals.ended_inside_thinking= 2 in/metrics.</tool_call>verbatim: 4 runs, 2 ended inside the thinking, one rescued by the continuation, one still empty after the cap of 2 continuations.Your approach in e5437d2 (swap the ending token for its text and feed it back to continue) is the same idea as the continuation side of #1840. The differences: mine keeps the repair inside the same reply and caps it per reply so a model that keeps doing it cannot loop, and it reports the end even when the continuation is not enough, so the empty reply is at least identifiable. It is opt-in (
literal_think_guard); with the flag off a server behaves as before.The Chinese prompt in your report is a good third case for my corpus. If you run it against your build, the journal line
the reply ended inside its thinking with no answerandtotals.ended_inside_thinkingin/metricswill show whether the dead end you hit is the same one, rather than a different early stop.@TwoAnts commented on GitHub (Oct 12, 2026):
Currently I mainly run into two situations:
When the context contains
<|im_end|>, the model may output an end token early during the thinking phase, causing the task to be interrupted abnormally. -> This severely affects usage in long conversations.When hindsight does reflection/consolidation, it requires the answer to be in JSON format, and Strata directly throws an error for answers that do not conform to JSON syntax. -> This causes the hindsight task to fail.
In fact, these are both shortcomings of the model. Of course, now that models are becoming more and more human-like, it is normal for them to occasionally make mistakes. They cannot be treated as precision machines.
My original intention with these workarounds was to make minor fixes as much as possible. Strata's serve layer is implemented in Python, so it can indeed be patched more flexibly.
Both of the above problems are currently solved by intercepting and retrying at the serve layer.
For the premature-ending issue, I mainly special-case three patterns: `<|im_end|>`, the triple-backtick pattern
...<|im_end|>''', and<think>...<|im_end|></think>, replace them back with text, feed them back, and continue generation (with some retry caps, of course). Of course, for those in the main body, there is nothing I can do; the code still cannot make semantic-level distinctions (though adding a jev might help a bit, lol). But this has already greatly reduced the frequency and mitigated the problem. f99e638For the JSON format issue, I also added a small loop at the serve layer that points out the error and corrects it, and the problem is basically solved. f839590
However, I mostly had an agent help me with the implementation, so it is somewhat crude.
I think the overall direction of changes is to strengthen Strata's detection of unreasonable outputs at the serve layer, and to allow some iterative retry dialogue between the serve layer and the model before giving the final answer. (this could be an extensible and reasonably well-structured implementation)
This is very similar to how humans check before delivering work results.
@47Hunter47 commented on GitHub (Oct 12, 2026):
Your first point is the shape I have been chasing: the turn ends inside the thinking with no answer, so the client gets an empty reply with
finish_reason: stop. Measured today on 0.1.43 (RTX 3090, Qwen3.8-Flash-Next IQ3_XXS, 250K context,--kv int8):/metricsshowedtotals.ended_inside_thinking= 2.</tool_call>verbatim: 4 runs, 2 ended inside the thinking. One was continued (the answer came back as<|), the other kept ending inside its thinking until the guard hit its cap of 2 continuations and that reply stayed empty.So the guard I opened in #1840 (opt-in
literal_think_guard) is not a complete fix, but it does two things that may help your harness: it continues the reply once or twice, and it says why a reply is empty when it cannot (log line,totals.ended_inside_thinkingin/metrics, andended_inside_thinking: truebesidechoices), so a client can retry instead of treating the turn as a normal empty answer.If you can run your
<|im_end|>-in-context prompt with the flag on, that counter will tell you whether it is the same end or a different one. And if it fires, the raw turn would be a useful addition to the five real turns inserve/fixtures/literal_think_specimens.json.Your second point (the hindsight JSON error) is a separate issue, so I have nothing to add there.
@47Hunter47 commented on GitHub (Oct 12, 2026):
I ran your minimal input here, guard on (
0.1.43, same build as above: RTX 3090, Qwen3.8-Flash-Next IQ3_XXS, 250K context,--kv int8).Your prompt, 4 runs: 4/4 ended inside the thinking (116, 68, 139 and 63 tokens generated), each one closed and continued on the first attempt — journal:
the reply ended inside its thinking with no answer ... closing the thinking so the model answers (1 of 2, literal_think_guard)— and 0 empty replies.totals.ended_inside_thinking6 → 10.The trigger you describe for long conversations, a transcript that already carries
<|im_end|>followed by an ordinary question: 4 runs, 4/4 the same shape (125, 396, 65, 111 tokens), 0 empty.totals.ended_inside_thinking10 → 14.So on this build your case is not occasional, it is every run — 8/8 — and the guard rescued all eight. But the replies it rescued were mostly stubs:
<|im_start|>,","marker"(that one is at least the answer), one<|im_end|>inside a fence, and one that resumed as planning prose instead of an answer. That is your diagnosis inf99e638: the literal re-tokenizes to the token that stops the turn, so the model cannot write it, and the guard only decides what happens after the stop. Write-back repairs the content, continuation repairs the empty reply — neither is the whole fix, and a turn like this needs both.On the shape of the work:
f99e638's "a turn that ends mid-structure is continued" is the same decision #1840 makes, covering the thinking case plus the quoted-marker and unclosed-fence cases #1840 does not. Mine is opt-in, capped per reply, and says why when it cannot continue; #1886 takes a third read, tying the parser transition to the real control-token identity. If you openf99e638as a PR, the maintainer can run the three against one corpus, which is the comparison worth having before any of them lands. I would rather lose most of #1840 to your broader shape than have two half-fixes in the tree.Your second point (the hindsight JSON error) is a different class from this one — as its own issue or PR it would get a proper review, and
f839590is already opt-in, which is the shape #1886 asked of #1840.One offer: a specimen of this turn would fit the corpus in #1840 (
serve/fixtures/literal_think_specimens.json, five realno-answer/answered/length-cutturns). Cutting one needs a serve started withSTRATA_DEBUG=1, which the running server was not, so I could not cut it today; I can add one on the next restart with the flag on.@47Hunter47 commented on GitHub (Oct 12, 2026):
Follow-up, with the write-back in and the credit where it belongs: I ported the thinking case of your shape 3 into the guard from #1840 (
06dfdc8c), so the text the stop token stood for is written back as ordinary text — the tokenizer'splainspans, so it can travel in the next pass's prompt without stopping that pass at once — and the thought goes on from there. Your backtick/fence shapes for the answer and your own caps I did not port; those are still yours if you openf99e638as a PR.It is what fixed the answer, not only the emptiness. Your minimal input, 4 runs on 0.1.43 here (RTX 3090, Qwen3.8-Flash-Next IQ3_XXS, 250K context,
--kv int8), before and after:All 8 runs ended inside the thinking, so the guard fired on every one of them; with the close alone they were non-empty but 3 of the 4 were stubs, and your diagnosis is the reason. The context-carries-
<|im_end|>variant answered 4/4 with the write-back too (its longer answers still come back in the template's own vocabulary —<|im_start|>user\n...— which is the model, not the guard).Detection and reporting are unchanged: one journal line per end,
totals.ended_inside_thinking, andended_inside_thinking: truebesidechoices. Two write-backs per reply, then the close, then the cap reports an empty reply; a thought that repeats whole passages is #728's loop, not a marker it mentions, and goes straight to the close.Your second point (the hindsight JSON error) is still a different class from this one — if you open
f839590as its own issue or PR it can be reviewed on its own terms.@TwoAnts commented on GitHub (Oct 13, 2026):
Hi, really love your positive attitude. 👍👍👍
Just to share my perspective:
These fixes are actually all addressing long-tail issues. It feels more like cleaning up after the model. This is actually unrelated to the core of the inference engine, so I feel it should really be called a post-fix.
For these complex edge cases, the code may never be able to fix them all or fix them cleanly, and it may overcorrect. A lot of fix code combined could eventually become a giant pile of shit.
I suspect this may also be something the author wants to avoid as much as possible.
I think we may need to see what the author thinks. Whether he would allow a post-fix-like module to identify, correct, and retry various special cases, or whether he would rather integrate these fixes himself so they can be maintained going forward.
@Niko1221
@47Hunter47 commented on GitHub (Oct 13, 2026):
@TwoAnts I share the worry, and I think the repo has answered most of it already.
Recovery at the serve layer, behind a config key with a counter next to it, is not a new idea here.
reasoning_loop_recoveryworks that way: it stops a generation, drains it, carries on from the same tokens, and reportsreasoning_recoveries.docs/DETAILS.mdcalls it "a policy, not a numerical engine fix or a guarantee of an answer," and it says the recovery can mistake real work for a loop, so measure the answer quality next to the completion rate.tool_call_recoveryhas the same shape for calls written inside the reasoning, and it ships off by default. One key per class, with its own corpus and its own counter, is the pattern Strata already uses. I readliteral_think_guardas the fourth member of that family rather than a new kind of patch, and that pattern is my answer to your worry about the fixes piling up.I would still split two things the thread keeps together. When a quoted or mid-line
</think>ends up in the answer (#1886), the parser is simply wrong, and that should be fixed every time with no flag at all. When the turn really does end inside its thinking and there is no answer to parse, an optional repair is the only thing left to do. Those are different problems. One flag over both would make things worse instead of simpler.There is one place where the repo has already decided against retrying.
structured.pysays "Failed generations are errors, never silently retried or returned as successful structured output," so I would keep JSON repair out of the layer and leave it to the prompt or the client.@Niko1221 what is genuinely your call here is scope, not shape. If you want this class handled, it can reuse the two recoveries that are already in the tree. If you would rather not, the half I would keep either way is the one that says why a reply came back empty, because that changes no output a client gets.
@Niko1221 commented on GitHub (Oct 13, 2026):
Thanks for the report. To keep the tracker current we are closing issues older than three days: 0.1.44 is out with a long list of fixes, and many older reports are covered by it or by 0.1.43.1.
If this still happens on 0.1.44, please leave a comment here with your engine log (and the version line it prints), and we will reopen it. A new issue that links this one works too.
@47Hunter47 commented on GitHub (Oct 14, 2026):
I re-ran this on a stock 0.1.44, so this shape is not covered by the release:
v0.1.44(ed4773eb) — the tag's ownserve/, no local patches;literal_think_guarddoes not exist there, so none of my code is in the path.build_info=Strata 0.1.44, engine 0.1.44, RTX 3090, Qwen3.8-Flash-Next IQ3_XXS, 250K context,--kv int8, sampling 1.0/0.95/20, thinking budget 16384.The minimal input from #1178 (
Qwen 聊天模板里,助手回合的结束标记是什么?请原样写出,例如 "标记"。), 8 runs,max_tokens4000, temperature 1.0: 8 of 8 came back with an emptycontentandfinish_reason: stop(86 to 255 tokens generated). One turn, as the engine log prints it:89 tokens out of a 3000-token thinking budget, ended with
stop: no budget cut and no length cut — the raw text stops where the model was about to write<|im_end|>and nothing reachescontent.The prompt from #1814 ("In a markdown code fence, show exactly how the closing thinking tag looks..."), 10 runs: 3 empty, same shape (the reasoning ends on
Could be something like ",the closing tag for thought: `,they likely mean a tag like <|im_end|>).On that tree
/metricshas noended_inside_thinking, so an empty reply carries no sign of why it is empty. That is what #1840 adds, both layers behindliteral_think_guard: with it on, the same two prompts came back 0 empty of 8 on the same box, and each end is reported (the response field, the journal line,/metrics).So the class is still there on 0.1.44 as it ships, and the fix for it is the review of #1840.