[GH-ISSUE #1814] Model writing a literal </think> emits the special token: quoted tag ends the reasoning mid-sentence and the turn stops (finish_reason=stop) #717

Closed
opened 2026-11-04 01:45:18 +07:00 by bsakaguchi · 7 comments
Owner

Originally created by @danirebollo on GitHub (Oct 10, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/1814

Follow-up on the output side of #537. On 0.1.41 (Qwen3.8-Flash-Next IQ3_XXS, 2×3090), a long agent session about parsing had 4 turns in a row cut after 100–200 tokens with finish_reason: stop, right after a backtick in the reasoning.
What I changed in serve/ (patch attached):
Only the model's own end-of-thinking token closes the thinking. Service.run() now tells the parser when it is that token, so a the model writes with ordinary text tokens stays reasoning text.A special / token that can't be a real marker is swapped for the tag's text tokens, and generation continues. That covers the answer, and quoted or mid-line tags in the thinking. It reuses the #123 continuation path.If the model stops right after opening an inline code span in its thinking, the tag is written as text and generation continues.
It's capped at 64 swaps per reply, behind a literal_think_guard flag (on by default), with 12 new tests.
What worked for me: asking the model to write the tag, repeating answer: 42 and a tool result containing the tag now all give full reasoning and a correct answer. A prompt that returned no answer 2/6 times returned 0/3. Decode speed is unchanged (120.3 vs 120.4 tok/s).

Originally created by @danirebollo on GitHub (Oct 10, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/1814 Follow-up on the output side of #537. On 0.1.41 (Qwen3.8-Flash-Next IQ3_XXS, 2×3090), a long agent session about parsing </think> had 4 turns in a row cut after 100–200 tokens with finish_reason: stop, right after a backtick in the reasoning. What I changed in serve/ (patch attached): Only the model's own end-of-thinking token closes the thinking. Service.run() now tells the parser when it is that token, so a </think> the model writes with ordinary text tokens stays reasoning text.A special <think>/</think> token that can't be a real marker is swapped for the tag's text tokens, and generation continues. That covers the answer, and quoted or mid-line tags in the thinking. It reuses the #123 continuation path.If the model stops right after opening an inline code span in its thinking, the tag is written as text and generation continues. It's capped at 64 swaps per reply, behind a literal_think_guard flag (on by default), with 12 new tests. What worked for me: asking the model to write the tag, repeating answer: 42</think> and a tool result containing the tag now all give full reasoning and a correct answer. A prompt that returned no answer 2/6 times returned 0/3. Decode speed is unchanged (120.3 vs 120.4 tok/s).
Author
Owner

@47Hunter47 commented on GitHub (Oct 10, 2026):

I reproduced this on an RTX 3090 with Qwen3.8-Flash-Next IQ3_XXS on 0.1.41 and dumped the raw turns with STRATA_DEBUG=1. The fix is in PR #1840.

What the raw text shows

The tag is not being swapped for text tokens, and it is not being quoted: the model is about to write its closing thinking tag and the turn-end token comes instead.

[strata] raw: 'We need to respond to user: "In a markdown code fence, show exactly how the closing
thinking tag looks, including the character after think. ... inside prose."\n\nNeed likely show
closing thinking tag: <|im_end|>'

finish_reason is stop, the reasoning ends mid-sentence, nothing is produced as content, and the client gets an empty reply. In the other turns I dumped the cut lands on a backtick instead (...Some models output reasoning in \``) or on `` or ```, i.e. wherever the model reaches for the tag. That matches your "right after a backtick" observation.

I kept five raw turns in the PR as serve/fixtures/literal_think_specimens.json (no-answer, answered, length-cut).

How often

runs empty replies
9 trigger runs, streaming and whole, STRATA_PREFILL_CPU_SHARE on and off 6 of 9
same prompts, later 3 of 5
same prompts, later 0 of 6

It is not deterministic on my machine: the same prompt answers normally on one pass and ends with nothing on the next, and the prompt has to make the model reach for the tag (asking it to show, parse or quote the tag). Your 2/6 and 0/3 numbers fit that.

Why the parser cannot catch it on its own

In this tokenizer the tag is not a special token - it is eight ordinary text tokens (<, /, t, h, i, n, k, >), and the token that ends the turn is the ordinary end-of-turn token. So a turn that stops inside its thinking is byte-for-byte indistinguishable from a turn that stops inside its thinking: nothing in the stream says "I meant that". That is also why the #537 work on the input side does not cover it (there the prompt carries a literal tag and can be marked before it is rendered; here it is the model's own output).

What I did

Service.run() continues the reply when it ends with finish_reason == "stop" inside the thinking with no answer: the thinking is closed the way #123's wrap-up closes it (REASONING_CLOSE) and the next pass's prompt is the one that produced the reply plus what it generated plus that close, so the engine continues from the prefix it already holds. parser.state == "content" is the same end from the other side (the tag was written as text, the parser took it as the marker and nothing followed): there the tag goes back into the reasoning text first, so the client still sees everything the model wrote.

It is #1053's continuation, so opt-in the same way: "literal_think_guard": true in strata-<model>.json, or STRATA_LITERAL_THINK_GUARD=1, capped at LITERAL_THINK_RETRIES = 2 continuations per reply, with a literal_think_retries counter in the request status/trace. I left the default off because #1053 is off by default and this appends the close to the prompt, which should stay a deliberate choice.

serve/test_literal_think_guard.py replays the five dumped turns through a mock engine; the full serve suite is at 604 tests with the only failure being the flaky telemetry (_loop) thread count in test_responses.py (it fails the same way on this checkout without the change).

One more symptom, same family

While testing I also saw turns that ended with finish_reason == "length" and up to a few thousand characters of reasoning text in content (the reasoning ran to the token budget after the parser had taken a literal tag as the marker). That is a second, quieter version of the same confusion - the client gets reasoning where the answer belongs - and it is not what this PR addresses. If you want it handled too, I can look at whether the same guard should reopen the thinking on a length end as well, or whether the answer parser should refuse content that is only reasoning.

<!-- gh-comment-id:6096774684 --> @47Hunter47 commented on GitHub (Oct 10, 2026): I reproduced this on an RTX 3090 with Qwen3.8-Flash-Next IQ3_XXS on 0.1.41 and dumped the raw turns with `STRATA_DEBUG=1`. The fix is in PR #1840. ## What the raw text shows The tag is not being swapped for text tokens, and it is not being quoted: the model is about to write its closing thinking tag and the **turn-end token comes instead**. ``` [strata] raw: 'We need to respond to user: "In a markdown code fence, show exactly how the closing thinking tag looks, including the character after think. ... inside prose."\n\nNeed likely show closing thinking tag: <|im_end|>' ``` `finish_reason` is `stop`, the reasoning ends mid-sentence, nothing is produced as content, and the client gets an empty reply. In the other turns I dumped the cut lands on a backtick instead (`...Some models output reasoning in \``) or on `\`</thinking>\` or \`\``, i.e. wherever the model reaches for the tag. That matches your "right after a backtick" observation. I kept five raw turns in the PR as `serve/fixtures/literal_think_specimens.json` (`no-answer`, `answered`, `length-cut`). ## How often | runs | empty replies | | --- | --- | | 9 trigger runs, streaming and whole, `STRATA_PREFILL_CPU_SHARE` on and off | 6 of 9 | | same prompts, later | 3 of 5 | | same prompts, later | 0 of 6 | It is not deterministic on my machine: the same prompt answers normally on one pass and ends with nothing on the next, and the prompt has to make the model reach for the tag (asking it to show, parse or quote the tag). Your 2/6 and 0/3 numbers fit that. ## Why the parser cannot catch it on its own In this tokenizer the tag is not a special token - it is eight ordinary text tokens (`<`, `/`, `t`, `h`, `i`, `n`, `k`, `>`), and the token that ends the turn is the ordinary end-of-turn token. So a turn that stops inside its thinking is byte-for-byte indistinguishable from a turn that stops inside its thinking: nothing in the stream says "I meant that". That is also why the #537 work on the input side does not cover it (there the *prompt* carries a literal tag and can be marked before it is rendered; here it is the model's own output). ## What I did `Service.run()` continues the reply when it ends with `finish_reason == "stop"` inside the thinking with no answer: the thinking is closed the way #123's wrap-up closes it (`REASONING_CLOSE`) and the next pass's prompt is the one that produced the reply plus what it generated plus that close, so the engine continues from the prefix it already holds. `parser.state == "content"` is the same end from the other side (the tag was written as text, the parser took it as the marker and nothing followed): there the tag goes back into the reasoning text first, so the client still sees everything the model wrote. It is #1053's continuation, so opt-in the same way: `"literal_think_guard": true` in `strata-<model>.json`, or `STRATA_LITERAL_THINK_GUARD=1`, capped at `LITERAL_THINK_RETRIES = 2` continuations per reply, with a `literal_think_retries` counter in the request status/trace. I left the default off because #1053 is off by default and this appends the close to the prompt, which should stay a deliberate choice. `serve/test_literal_think_guard.py` replays the five dumped turns through a mock engine; the full `serve` suite is at 604 tests with the only failure being the flaky telemetry `(_loop)` thread count in `test_responses.py` (it fails the same way on this checkout without the change). ## One more symptom, same family While testing I also saw turns that ended with `finish_reason == "length"` and up to a few thousand characters of *reasoning text* in `content` (the reasoning ran to the token budget after the parser had taken a literal tag as the marker). That is a second, quieter version of the same confusion - the client gets reasoning where the answer belongs - and it is not what this PR addresses. If you want it handled too, I can look at whether the same guard should reopen the thinking on a `length` end as well, or whether the answer parser should refuse content that is only reasoning.
Author
Owner

@47Hunter47 commented on GitHub (Oct 10, 2026):

A quick follow-up on my comment above: I split the work into two layers in PR #1840, so the fix is not optional any more.

  • Always on: the end is reported - a log line, totals.ended_inside_thinking (/metrics' totals_ended_inside_thinking_total), and "ended_inside_thinking": true beside choices in the response (and in the stream's last chunk). If you are getting an empty reply, the response itself now says the turn ended inside the thinking, so a client can decide to retry instead of showing nothing. It changes no output, so it needs no switch.
  • Opt-in: the automatic continuation ("literal_think_guard": true, or STRATA_LITERAL_THINK_GUARD=1) closes the thinking and lets the model answer, capped at two continuations per reply.

serve/fixtures/literal_think_specimens.json in that PR holds five raw turns from my machine (no-answer / answered / length-cut), and serve/test_literal_think_guard.py replays them through a mock engine, so the behaviour is pinned to the real text rather than to my summary of it.

<!-- gh-comment-id:6096902535 --> @47Hunter47 commented on GitHub (Oct 10, 2026): A quick follow-up on my comment above: I split the work into two layers in PR #1840, so the fix is not optional any more. * **Always on:** the end is reported - a log line, `totals.ended_inside_thinking` (`/metrics`' `totals_ended_inside_thinking_total`), and `"ended_inside_thinking": true` beside `choices` in the response (and in the stream's last chunk). If you are getting an empty reply, the response itself now says the turn ended inside the thinking, so a client can decide to retry instead of showing nothing. It changes no output, so it needs no switch. * **Opt-in:** the automatic continuation (`"literal_think_guard": true`, or `STRATA_LITERAL_THINK_GUARD=1`) closes the thinking and lets the model answer, capped at two continuations per reply. `serve/fixtures/literal_think_specimens.json` in that PR holds five raw turns from my machine (`no-answer` / `answered` / `length-cut`), and `serve/test_literal_think_guard.py` replays them through a mock engine, so the behaviour is pinned to the real text rather than to my summary of it.
Author
Owner

@47Hunter47 commented on GitHub (Oct 12, 2026):

Fresh numbers on the current build, since my branch in #1840 is now synced with main.

Setup: RTX 3090, Qwen3.8-Flash-Next IQ3_XXS, 0.1.43 (engine rebuilt for it), 250K context, --kv int8, literal_think_guard on.

The trigger prompt asks the model to show exactly how the closing thinking tag looks. Four runs:

  • 2 runs ended inside the thinking with the turn-end token (384 and 63 tokens in). The guard closed the thinking and continued, and both gave an answer (364 and 353 chars). The response carried ended_inside_thinking: true, the journal has the line, and /metrics read totals.ended_inside_thinking = 2 (strata:totals_ended_inside_thinking_total in /metrics/prometheus).
  • 2 runs never hit that end.

So the dead end you reported still happens on 0.1.43, and with the flag off those two runs are exactly the empty reply from your report.

I also found the same family elsewhere while looking: #1178 (a special token written in the thinking), #1526 (a quoted tag, which the maintainer fixed as a rule in 0.1.42) and #2026 (v0.1.43, reached from the string </tool_call>). Same end, different strings, so the guard keys on the end itself rather than on the tag.

<!-- gh-comment-id:6121174188 --> @47Hunter47 commented on GitHub (Oct 12, 2026): Fresh numbers on the current build, since my branch in #1840 is now synced with main. Setup: RTX 3090, Qwen3.8-Flash-Next IQ3_XXS, 0.1.43 (engine rebuilt for it), 250K context, `--kv int8`, `literal_think_guard` on. The trigger prompt asks the model to show exactly how the closing thinking tag looks. Four runs: - 2 runs ended inside the thinking with the turn-end token (384 and 63 tokens in). The guard closed the thinking and continued, and both gave an answer (364 and 353 chars). The response carried `ended_inside_thinking: true`, the journal has the line, and `/metrics` read `totals.ended_inside_thinking` = 2 (`strata:totals_ended_inside_thinking_total` in `/metrics/prometheus`). - 2 runs never hit that end. So the dead end you reported still happens on 0.1.43, and with the flag off those two runs are exactly the empty reply from your report. I also found the same family elsewhere while looking: #1178 (a special token written in the thinking), #1526 (a quoted tag, which the maintainer fixed as a rule in 0.1.42) and #2026 (v0.1.43, reached from the string `</tool_call>`). Same end, different strings, so the guard keys on the end itself rather than on the tag.
Author
Owner

@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.

<!-- gh-comment-id:6156220242 --> @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](https://github.com/Niko1221/Strata/releases/tag/v0.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.
Author
Owner

@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:

  • a checkout of the tag v0.1.44 (ed4773eb) — the tag's own serve/, no local patches; literal_think_guard does 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_tokens 4000, temperature 1.0: 8 of 8 came back with an empty content and finish_reason: stop (86 to 255 tokens generated). One turn, as the engine log prints it:

[strata] req openai: msgs=1 tools=0 max_tokens_raw=4000/None max_new=4000 thinking=True stream=False prompt_tokens=76
[strata] thinking capped at 3000 of max_tokens 4000 to leave room for the answer (budget 16384)
[strata] thinking: 76 of budget 3000 (max 4000) tokens, 86.3 tok/s, 1 s
[strata] done: 89 tokens in 1 s (81.9 tok/s) (stop, cancel=False), expert cache 98.1% hit
[strata] raw: '...使用 ChatML 格式:<|im_start|>user\n内容<|im_end|>'

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 reaches content.

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 /metrics has no ended_inside_thinking, so an empty reply carries no sign of why it is empty. That is what #1840 adds, both layers behind literal_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.

<!-- gh-comment-id:6160396058 --> @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: - a checkout of the tag `v0.1.44` (`ed4773eb`) — the tag's own `serve/`, no local patches; `literal_think_guard` does 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_tokens` 4000, temperature 1.0: **8 of 8 came back with an empty `content` and `finish_reason: stop`** (86 to 255 tokens generated). One turn, as the engine log prints it: ``` [strata] req openai: msgs=1 tools=0 max_tokens_raw=4000/None max_new=4000 thinking=True stream=False prompt_tokens=76 [strata] thinking capped at 3000 of max_tokens 4000 to leave room for the answer (budget 16384) [strata] thinking: 76 of budget 3000 (max 4000) tokens, 86.3 tok/s, 1 s [strata] done: 89 tokens in 1 s (81.9 tok/s) (stop, cancel=False), expert cache 98.1% hit [strata] raw: '...使用 ChatML 格式:<|im_start|>user\n内容<|im_end|>' ``` 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 reaches `content`. 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 `/metrics` has no `ended_inside_thinking`, so an empty reply carries no sign of why it is empty. That is what #1840 adds, both layers behind `literal_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.
Author
Owner

@47Hunter47 commented on GitHub (Oct 16, 2026):

Re-reported with the engine log the close comment asked for: #2731 — the same end still reproduces on a stock checkout of v0.1.44.1 (reasoning_close_retry off, 4 of 4 runs empty for the #1178 input).

<!-- gh-comment-id:6208608987 --> @47Hunter47 commented on GitHub (Oct 16, 2026): Re-reported with the engine log the close comment asked for: #2731 — the same end still reproduces on a stock checkout of `v0.1.44.1` (`reasoning_close_retry` off, 4 of 4 runs empty for the #1178 input).
Author
Owner

@nghia1930mmm-cmd commented on GitHub (Oct 16, 2026):

Là sao?

Trên 16 tháng 10, 2026 12:14:25 GMT+07:00, Mert @.***> đã viết:

47Hunter47 left a comment (Niko1221/Strata#1814)

Re-reported with the engine log the close comment asked for: #2731 — the same end still reproduces on a stock checkout of v0.1.44.1 (reasoning_close_retry off, 4 of 4 runs empty for the #1178 input).

--
Reply to this email directly or view it on GitHub:
https://github.com/Niko1221/Strata/issues/1814#issuecomment-6208608987
You are receiving this because you are subscribed to this thread.

Message ID: @.***>

<!-- gh-comment-id:6208630247 --> @nghia1930mmm-cmd commented on GitHub (Oct 16, 2026): Là sao? Trên 16 tháng 10, 2026 12:14:25 GMT+07:00, Mert ***@***.***> đã viết: >47Hunter47 left a comment (Niko1221/Strata#1814) > >Re-reported with the engine log the close comment asked for: #2731 — the same end still reproduces on a stock checkout of `v0.1.44.1` (`reasoning_close_retry` off, 4 of 4 runs empty for the #1178 input). > >-- >Reply to this email directly or view it on GitHub: >https://github.com/Niko1221/Strata/issues/1814#issuecomment-6208608987 >You are receiving this because you are subscribed to this thread. > >Message ID: ***@***.***>
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#717
No description provided.