Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Unsloth Studio: a JSONL reply that quotes <tool_call> no longer stalls the tool parser and fails the turn with RecursionError (#14097)

Leo Borcherdingcfe31dce284e
diff --git a/studio/backend/core/inference/tool_call_parser.py b/studio/backend/core/inference/tool_call_parser.py
index 2e83fe5263d..9c4c59831fe 100644
--- a/studio/backend/core/inference/tool_call_parser.py
+++ b/studio/backend/core/inference/tool_call_parser.py
@@ -2340,7 +2340,7 @@ def _disabled_gemma_call_end_containing_signal(
) -> int | None:
"""End offset (exclusive) of the earliest PROSE (non-promotable) wrapper-less Gemma call whose
balanced body holds the first foreign signal, else None. A prose name makes the quoted literal
- data, so the caller drops the span and recurses on the tail; a promotable call defers to the
+ data, so the caller drops the span and parses the tail; a promotable call defers to the
enabled-call guard."""
first = _first_foreign_tool_signal(content)
trig = content.find(_MISTRAL_TRIGGER)
@@ -2378,57 +2378,55 @@ def parse_tool_calls_from_text(
the markerless Llama-3.2 bare-JSON form (the marker-based forms carry an explicit signal, so
a disabled-tool name there is a real call attempt); ``None`` keeps the name-agnostic
behaviour."""
- # Drop Magistral [THINK]...[/THINK] BEFORE dispatch: a rehearsed call inside it must never be promoted, and the
- # parse path must agree with the display strip.
- content = _strip_mistral_reasoning(content)
-
- # Equal-length mask, so the spans this returns still index the caller's text: a blocked
- # call's arguments are quoted prose, and a wrapped call nested there was promoted.
- content, _blocked_bodies = _mask_blocked_bodies(content, enabled_tool_names, think = True)
-
- # A leading bare-JSON value is decided FIRST: a string argument quoting tool markup (XML or a Mistral trigger)
- # must stay data, so the bare-JSON parser takes the outer call before any other pass. Precedes the Mistral guard,
- # whose preamble tolerance would otherwise claim a trigger quoted inside the leading object.
- if _xml_signal_inside_leading_bare_json(content):
- calls = _parse_llama3_bare_json(
- content, id_offset = id_offset, enabled_tool_names = enabled_tool_names
- )
- if calls:
- return calls
- i = 0
- while i < len(content) and content[i] in " \t\n\r":
- i += 1
- # The guard guarantees a balanced leading value (object or array).
- end = (_balanced_brace_end if content[i] == "{" else _balanced_bracket_end)(content, i)
- return parse_tool_calls_from_text(
- content[end + 1 :],
- id_offset = id_offset,
- allow_incomplete = allow_incomplete,
- enabled_tool_names = enabled_tool_names,
- )
+ # Loop, not recurse (one level per JSONL line hit RecursionError). A skipped run's tail is stripped
+ # and masked again before any dispatch, or reasoning after a quoted opener stays executable.
+ while True:
+ # Drop Magistral [THINK]...[/THINK] BEFORE dispatch: a rehearsed call inside it must never be promoted, and
+ # the parse path must agree with the display strip.
+ content = _strip_mistral_reasoning(content)
+
+ # Equal-length mask, so the spans this returns still index the caller's text: a blocked
+ # call's arguments are quoted prose, and a wrapped call nested there was promoted.
+ content, _blocked_bodies = _mask_blocked_bodies(content, enabled_tool_names, think = True)
+
+ # A leading bare-JSON value is decided FIRST: a string argument quoting tool markup (XML or a Mistral trigger)
+ # must stay data, so the bare-JSON parser takes the outer call before any other pass. Precedes the Mistral
+ # guard, whose preamble tolerance would otherwise claim a trigger quoted inside the leading object.
+ skipped = False
+ while _xml_signal_inside_leading_bare_json(content):
+ calls = _parse_llama3_bare_json(
+ content, id_offset = id_offset, enabled_tool_names = enabled_tool_names
+ )
+ if calls:
+ return calls
+ i = 0
+ while i < len(content) and content[i] in " \t\n\r":
+ i += 1
+ # The guard guarantees a balanced leading value (object or array).
+ end = (_balanced_brace_end if content[i] == "{" else _balanced_bracket_end)(content, i)
+ content = content[end + 1 :]
+ skipped = True
+ if skipped:
+ continue
- # A leading enabled wrapper-less Gemma call is decided BEFORE the Mistral guard: its body reads as prose to the
- # preamble tolerance below, so a quoted [TOOL_CALLS] would otherwise steal the turn.
- if _signal_inside_leading_wrapperless_gemma(content, enabled_tool_names):
- calls = _parse_gemma_tool_calls(
- content,
- id_offset = id_offset,
- allow_incomplete = allow_incomplete,
- enabled_tool_names = enabled_tool_names,
- )
- if calls:
- return calls
+ # A leading enabled wrapper-less Gemma call is decided BEFORE the Mistral guard: its body reads as prose to
+ # the preamble tolerance below, so a quoted [TOOL_CALLS] would otherwise steal the turn.
+ if _signal_inside_leading_wrapperless_gemma(content, enabled_tool_names):
+ calls = _parse_gemma_tool_calls(
+ content,
+ id_offset = id_offset,
+ allow_incomplete = allow_incomplete,
+ enabled_tool_names = enabled_tool_names,
+ )
+ if calls:
+ return calls
- # A DISABLED wrapper-less Gemma call is prose: drop the span and parse the tail BEFORE the Mistral guard, whose
- # preamble tolerance would otherwise parse a quoted trigger.
- _prose_end = _disabled_gemma_call_end_containing_signal(content, enabled_tool_names)
- if _prose_end is not None:
- return parse_tool_calls_from_text(
- content[_prose_end:],
- id_offset = id_offset,
- allow_incomplete = allow_incomplete,
- enabled_tool_names = enabled_tool_names,
- )
+ # A DISABLED wrapper-less Gemma call is prose: drop the span and parse the tail BEFORE the Mistral guard,
+ # whose preamble tolerance would otherwise parse a quoted trigger.
+ _prose_end = _disabled_gemma_call_end_containing_signal(content, enabled_tool_names)
+ if _prose_end is None:
+ break
+ content = content[_prose_end:]
# A [TOOL_CALLS] call that is the first tool emission owns the turn: XML quoted in its arguments or trailing prose
# is not promoted over it, nor does a prose preface forfeit it.
diff --git a/studio/backend/tests/test_tool_parser_leading_json_lines.py b/studio/backend/tests/test_tool_parser_leading_json_lines.py
new file mode 100644
index 00000000000..fb309594d32
--- /dev/null
+++ b/studio/backend/tests/test_tool_parser_leading_json_lines.py
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A reply made of many JSON lines that quote tool markup (a Hermes-format dataset, say) is data:
+the parser steps over each line instead of recursing once per line."""
+
+import sys
+import time
+
+from core.inference.tool_call_parser import parse_tool_calls_from_text
+
+LINE = '{"text": "use <tool_call> tags"}\n'
+
+
+def test_many_json_lines_quoting_tool_markup_parse_to_no_calls():
+ limit = sys.getrecursionlimit()
+ sys.setrecursionlimit(1000)
+ try:
+ start = time.monotonic()
+ calls = parse_tool_calls_from_text(LINE * 1600, enabled_tool_names = {"web_search"})
+ elapsed = time.monotonic() - start
+ finally:
+ sys.setrecursionlimit(limit)
+ assert calls == []
+ assert elapsed < 10, elapsed
+
+
+def test_a_real_call_after_quoting_lines_is_still_found():
+ text = (
+ LINE * 3
+ + '<tool_call>{"name": "web_search", "arguments": {"query": "unsloth"}}</tool_call>'
+ )
+ calls = parse_tool_calls_from_text(text, enabled_tool_names = {"web_search"})
+ assert [c["function"]["name"] for c in calls] == ["web_search"]
+
+
+def test_reasoning_after_a_quoting_line_is_not_executed():
+ real = '<tool_call>{"name": "web_search", "arguments": {"query": "real"}}</tool_call>'
+ for reasoning in (
+ '[THINK]<|python_tag|>terminal.call(cmd="rm")[/THINK]',
+ '<think><|python_tag|>terminal.call(cmd="rm")</think>',
+ "[THINK]call:web_search{q:<|python_tag|>x}[/THINK]",
+ ):
+ calls = parse_tool_calls_from_text(
+ LINE + reasoning + "\n" + real, enabled_tool_names = {"web_search", "terminal"}
+ )
+ assert [c["function"]["name"] for c in calls] == ["web_search"], reasoning