[GH-ISSUE #4702] [Engine] Tool call parsing: an inner ``` fence closes a four-backtick fence, so a quoted example inside it becomes a call #2151

Closed
opened 2026-11-04 01:57:04 +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/4702

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 (recover off and on).

An answer that hands the user a Markdown file in a four-backtick fence, with an inner three-backtick block that shows a
tool call, gets a real read call out of the example. The inner ``` closes the outer fence in the parser's code
tracking; in CommonMark a fence is only closed by a fence of at least as many backticks.

Notes: tracking the opening fence's length (and character, ``` vs ~~~) and closing it only on a line of at least as many
of the same character would follow CommonMark.

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 = ("Save this as docs/TOOLS.md:\n\n````markdown\n# Tool calls\n\nA call:\n\n```xml\n"
        + qwen("read", path="EXAMPLE.txt") + "\n```\n````\n\nThat is the file.")
print(parse(text))

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

([('read', {'path': 'EXAMPLE.txt'})], 'Save this as docs/TOOLS.md:\n\n````markdown\n# Tool calls\n\nA call:\n\n```xml```\n````\n\nThat is the file.')

Expected: no call, and the whole text as the answer.

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/4702 ### 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 (recover off and on). An answer that hands the user a Markdown file in a four-backtick fence, with an inner three-backtick block that shows a tool call, gets a real `read` call out of the example. The inner ``` closes the outer fence in the parser's code tracking; in CommonMark a fence is only closed by a fence of at least as many backticks. Notes: tracking the opening fence's length (and character, ``` vs ~~~) and closing it only on a line of at least as many of the same character would follow CommonMark. ### 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 = ("Save this as docs/TOOLS.md:\n\n````markdown\n# Tool calls\n\nA call:\n\n```xml\n" + qwen("read", path="EXAMPLE.txt") + "\n```\n````\n\nThat is the file.") print(parse(text)) ``` Observed (the same whole, byte by byte, and in pieces of 7): ``` ([('read', {'path': 'EXAMPLE.txt'})], 'Save this as docs/TOOLS.md:\n\n````markdown\n# Tool calls\n\nA call:\n\n```xml```\n````\n\nThat is the file.') ``` Expected: no call, and the whole text as the answer. ### 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):

Fixed in 0.2.9: a call shown inside a longer code fence is now text and isn't run. A fence closes only with the same length it opened with. The indented case in #4703 is fixed too. Thanks for the exact write-ups.

<!-- gh-comment-id:6541129231 --> @Niko1221 commented on GitHub (Oct 31, 2026): Fixed in [0.2.9](https://github.com/Niko1221/Strata/releases/tag/v0.2.9): a call shown inside a longer code fence is now text and isn't run. A fence closes only with the same length it opened with. The indented case in #4703 is fixed too. Thanks for the exact write-ups.
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#2151
No description provided.