[GH-ISSUE #4704] [Engine] Reasoning parser: the stray closing-think rule (#4077) removes a </think> that sits inside a code fence the answer opened #2154

Open
opened 2026-11-04 01:57:05 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @KarlGrier on GitHub (Oct 30, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/4704

Area

[Engine] (speed, kernels, models, the server and API)

Desktop app, Terminal or source?

Source (START-HERE.bat, setup.sh or a build)

What happened

In serve/frontend.py's OutputParser at v0.2.8 (thinking on; recover off and on).

After the reasoning has ended, #4077 drops a </think> at the start of a line of the answer as a stray, and on purpose
ignores fences ("the reasoning may have left one open"). When the fence was opened by the answer itself, the tag is part
of what the user asked for - here, an example of a thinking block - and removing it changes the answer.

Notes: the concern behind #4077 (a fence the reasoning left open makes the answer's code tracking wrong) does not apply
to a fence opened after the reasoning ended. Starting the answer's code tracking fresh at the end of the reasoning (as
#4077 already does after a stray) and keeping a </think> that is inside a fence opened in the answer would cover both.
The same rule turns a quoted call into a real one when the fenced example also holds a thinking block - a test file
the user asked for:

text = ("Plan.\n</think>\n\nHere is the test:\n\n```python\nSAMPLE = '''\n<think>\nplan\n</think>\n"
        + qwen("read", path="EXAMPLE.txt") + "\n'''\n```\n\nRun it with pytest.")
print(parse(text, thinking=True)[0])

Observed: [('read', {'path': 'EXAMPLE.txt'})] - with recover off and on, whole and in pieces. Expected: no call.

Smallest steps or command that reproduce it

Parser level - no model, no GPU, no server. From the root of a v0.2.8 checkout, with this setup:

import json
from serve.frontend import OutputParser

def tool(name, **props):
    return {"name": name, "parameters": {"type": "object", "properties": props}}

S = {"type": "string"}
TOOLS = [tool("read", path=S), tool("write_file", path=S, content=S)]

def qwen(name, **params):  # the Qwen3.8-Flash-Next call form, as in serve/test_tool_corpus.py
    body = "".join(f"<parameter={k}>\n{v}\n</parameter>\n" for k, v in params.items())
    return f"<tool_call>\n<function={name}>\n{body}</function>\n</tool_call>"

def parse(text, thinking=False):
    p = OutputParser(thinking=thinking, tools=TOOLS, stream_tools=False, recover=False, declared_only=True)
    evs = p.feed(text) + p.finish("stop")
    calls = [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"]
    answer = "".join(e.text for e in evs if e.kind == "content")
    return calls, answer

then:

text = "Plan the answer.\n</think>\n\nLike this:\n\n```\n<think>\nstep one\n</think>\nThe answer.\n```\n\nThat is all."
print(parse(text, thinking=True))

Observed (the same whole, byte by byte, and in pieces of 7):

([], 'Like this:\n\n```\n<think>\nstep one\nThe answer.\n```\n\nThat is all.')

Expected: the answer unchanged - </think> kept inside the fence.

Reproduction status

Reproduced locally

Strata version

v0.2.8 (the tag, source checkout, no local modifications) · engine 0.1.54

Graphics card, memory and system

Not involved (parser level, no GPU) · Linux · Python 3.12

Model, size and settings

Not involved: the parser is fed the model's text directly (the Qwen3.8-Flash-Next call form from serve/test_tool_corpus.py).

Measured and drafted with Claude Code.

Originally created by @KarlGrier on GitHub (Oct 30, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/4704 ### Area [Engine] (speed, kernels, models, the server and API) ### Desktop app, Terminal or source? Source (START-HERE.bat, setup.sh or a build) ### What happened In `serve/frontend.py`'s `OutputParser` at v0.2.8 (thinking on; recover off and on). After the reasoning has ended, #4077 drops a `</think>` at the start of a line of the answer as a stray, and on purpose ignores fences ("the reasoning may have left one open"). When the fence was opened by the answer itself, the tag is part of what the user asked for - here, an example of a thinking block - and removing it changes the answer. Notes: the concern behind #4077 (a fence the reasoning left open makes the answer's code tracking wrong) does not apply to a fence opened after the reasoning ended. Starting the answer's code tracking fresh at the end of the reasoning (as #4077 already does after a stray) and keeping a `</think>` that is inside a fence opened in the answer would cover both. The same rule turns a quoted call into a real one when the fenced example also holds a thinking block - a test file the user asked for: ```python text = ("Plan.\n</think>\n\nHere is the test:\n\n```python\nSAMPLE = '''\n<think>\nplan\n</think>\n" + qwen("read", path="EXAMPLE.txt") + "\n'''\n```\n\nRun it with pytest.") print(parse(text, thinking=True)[0]) ``` Observed: `[('read', {'path': 'EXAMPLE.txt'})]` - with recover off and on, whole and in pieces. Expected: no call. ### Smallest steps or command that reproduce it Parser level - no model, no GPU, no server. From the root of a v0.2.8 checkout, with this setup: ```python import json from serve.frontend import OutputParser def tool(name, **props): return {"name": name, "parameters": {"type": "object", "properties": props}} S = {"type": "string"} TOOLS = [tool("read", path=S), tool("write_file", path=S, content=S)] def qwen(name, **params): # the Qwen3.8-Flash-Next call form, as in serve/test_tool_corpus.py body = "".join(f"<parameter={k}>\n{v}\n</parameter>\n" for k, v in params.items()) return f"<tool_call>\n<function={name}>\n{body}</function>\n</tool_call>" def parse(text, thinking=False): p = OutputParser(thinking=thinking, tools=TOOLS, stream_tools=False, recover=False, declared_only=True) evs = p.feed(text) + p.finish("stop") calls = [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"] answer = "".join(e.text for e in evs if e.kind == "content") return calls, answer ``` then: ```python text = "Plan the answer.\n</think>\n\nLike this:\n\n```\n<think>\nstep one\n</think>\nThe answer.\n```\n\nThat is all." print(parse(text, thinking=True)) ``` Observed (the same whole, byte by byte, and in pieces of 7): ``` ([], 'Like this:\n\n```\n<think>\nstep one\nThe answer.\n```\n\nThat is all.') ``` Expected: the answer unchanged - `</think>` kept inside the fence. ### Reproduction status Reproduced locally ### Strata version v0.2.8 (the tag, source checkout, no local modifications) · engine 0.1.54 ### Graphics card, memory and system Not involved (parser level, no GPU) · Linux · Python 3.12 ### Model, size and settings Not involved: the parser is fed the model's text directly (the Qwen3.8-Flash-Next call form from serve/test_tool_corpus.py). Measured and drafted with Claude Code.
Author
Owner

@Niko1221 commented on GitHub (Oct 31, 2026):

We're leaving this one as it is for now. It's a rare display case, and changing the rule could break how the end of the model's thinking is detected, which matters more for agent runs (#4077). Keeping it open in case we find a safe way.

<!-- gh-comment-id:6541142457 --> @Niko1221 commented on GitHub (Oct 31, 2026): We're leaving this one as it is for now. It's a rare display case, and changing the rule could break how the end of the model's thinking is detected, which matters more for agent runs (#4077). Keeping it open in case we find a safe way.
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#2154
No description provided.