[GH-ISSUE #2037] serve: tool_call_recovery misses fenced JSON inside an explicit tool-call wrapper #811

Closed
opened 2026-11-04 01:46:08 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @bettercallcaleb on GitHub (Oct 12, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/2037

Summary

With tool_call_recovery enabled, Strata recovers JSON for a declared tool inside an explicit <tool_call> wrapper, but fails to recognize the same JSON if the model wraps it in a Markdown ```json fence. The response is emitted as regular content, with no tool_call.

This is a code-fence handling issue, not a JSON-string escaping issue: literal backticks inside a JSON string work in the unfenced case.

Reproduction (parser-only; no model required)

import json
from serve.frontend import OutputParser

tools = [
    {
        "name": "write_file",
        "parameters": {
            "type": "object",
            "properties": {"content": {"type": "string"}},
        },
    }
]
payload = json.dumps({
    "name": "write_file",
    "arguments": {"content": "hello `world`"},
})
cases = {
    "bare": f"<tool_call>\n{payload}\n</tool_call>",
    "fenced": f"<tool_call>\n```json\n{payload}\n```\n</tool_call>",
}

for name, raw in cases.items():
    parser = OutputParser(thinking=False, tools=tools, recover=True)
    events = parser.feed(raw) + parser.finish()
    calls = [event.call.arguments for event in events if event.kind == "tool_call"]
    print(name, calls)

Actual

bare [{'content': 'hello `world`'}]
fenced []

The fenced case instead emits the wrapper and fenced JSON as content, without an error. Reproduced against the current serve/frontend.py on main with whole-input, 1-character, and 7-character chunks, both stream_tools=False and True.

Expected

When recovery is explicitly enabled, an otherwise valid JSON call for a declared tool inside an explicit <tool_call> wrapper should either be recovered despite an optional Markdown JSON fence, or produce a clear diagnostic. It should not silently become a normal assistant reply.

Fenced JSON in ordinary prose/reasoning must remain text, not execute as a tool call. Any recovery should be narrowly scoped to the explicit wrapper and a declared tool.

Where

  • serve/frontend.py: OutputParser._follower() recognizes JSON only when the text following <tool_call> begins with {.
  • Related to the opt-in recovery behavior in #1172; the quote/fence safeguards from #1058 should remain intact.
Originally created by @bettercallcaleb on GitHub (Oct 12, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/2037 ## Summary With `tool_call_recovery` enabled, Strata recovers JSON for a declared tool inside an explicit `<tool_call>` wrapper, but fails to recognize the same JSON if the model wraps it in a Markdown ```json fence. The response is emitted as regular content, with no `tool_call`. This is a code-fence handling issue, **not** a JSON-string escaping issue: literal backticks inside a JSON string work in the unfenced case. ## Reproduction (parser-only; no model required) ````python import json from serve.frontend import OutputParser tools = [ { "name": "write_file", "parameters": { "type": "object", "properties": {"content": {"type": "string"}}, }, } ] payload = json.dumps({ "name": "write_file", "arguments": {"content": "hello `world`"}, }) cases = { "bare": f"<tool_call>\n{payload}\n</tool_call>", "fenced": f"<tool_call>\n```json\n{payload}\n```\n</tool_call>", } for name, raw in cases.items(): parser = OutputParser(thinking=False, tools=tools, recover=True) events = parser.feed(raw) + parser.finish() calls = [event.call.arguments for event in events if event.kind == "tool_call"] print(name, calls) ```` ## Actual ```text bare [{'content': 'hello `world`'}] fenced [] ``` The fenced case instead emits the wrapper and fenced JSON as `content`, without an error. Reproduced against the current `serve/frontend.py` on `main` with whole-input, 1-character, and 7-character chunks, both `stream_tools=False` and `True`. ## Expected When recovery is **explicitly enabled**, an otherwise valid JSON call for a declared tool inside an explicit `<tool_call>` wrapper should either be recovered despite an optional Markdown JSON fence, or produce a clear diagnostic. It should not silently become a normal assistant reply. Fenced JSON in ordinary prose/reasoning must **remain text**, not execute as a tool call. Any recovery should be narrowly scoped to the explicit wrapper and a declared tool. ## Where - `serve/frontend.py`: `OutputParser._follower()` recognizes JSON only when the text following `<tool_call>` begins with `{`. - Related to the opt-in recovery behavior in #1172; the quote/fence safeguards from #1058 should remain intact.
Author
Owner

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

Confirmed, and your reproduction was exact. With recovery on, <tool_call> followed by a Markdown fence (json or a bare ) was read as prose, so the call came out as content. Fixed: inside an explicit <tool_call> wrapper, a fence that opens with an optional json and holds a declared tool's {"name": ..., "arguments": ...} is now the call, whole input or streamed in 1- or 7-character chunks, with stream_tools on or off. Narrow on purpose: it needs the wrapper, tool_call_recovery on and a declared tool; a fence in prose or reasoning, another language's fence (```python), and an undeclared tool name all stay text, and the #1058 quote and fence safeguards are untouched. Tests cover each of those. Closing when the fix ships in 0.1.43.1 (out now).

<!-- gh-comment-id:6129332739 --> @Niko1221 commented on GitHub (Oct 12, 2026): Confirmed, and your reproduction was exact. With recovery on, `<tool_call>` followed by a Markdown fence (```json or a bare ```) was read as prose, so the call came out as content. Fixed: inside an explicit `<tool_call>` wrapper, a fence that opens with an optional `json` and holds a declared tool's `{"name": ..., "arguments": ...}` is now the call, whole input or streamed in 1- or 7-character chunks, with `stream_tools` on or off. Narrow on purpose: it needs the wrapper, `tool_call_recovery` on and a declared tool; a fence in prose or reasoning, another language's fence (```python), and an undeclared tool name all stay text, and the #1058 quote and fence safeguards are untouched. Tests cover each of those. Closing when the fix ships in 0.1.43.1 (out now).
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#811
No description provided.