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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: decode Gemma quoted values in a tool call without its wrapper (#16009)
Daniel Han36ec3182ea4c
diff --git a/studio/backend/core/inference/tool_call_parser.py b/studio/backend/core/inference/tool_call_parser.pyindex 71a1b644713..57886803bfb 100644--- a/studio/backend/core/inference/tool_call_parser.py+++ b/studio/backend/core/inference/tool_call_parser.py@@ -385,6 +385,8 @@ _GEMMA_STR_END = '<|"|>'_GEMMA_TC_END = "<tool_call|>"# skip_special_tokens strips the wrapper, so streamed Gemma calls arrive bare.+# A value OPENED by Gemma's string delimiter (``key:<|"|>``); a literal one inside another quote is data.+_GEMMA_QUOTED_VALUE_RE = re.compile(r'(?:^|[{,\[])\s*[A-Za-z_][\w.\-]*\s*:\s*<\|"\|>')_GEMMA_BARE_TC_RE = re.compile(r"(?<!\w)call\s*:\s*([\w\.\-]+)\s*\{")# Partial prefix so the streaming buffer holds it instead of leaking visible text._GEMMA_BARE_TC_PREFIX_RE = re.compile(r"(?<!\w)call\s*(?::\s*[\w\.\-]*)?$")@@ -3141,7 +3143,7 @@ def _parse_gemma_tool_calls(code_spans, code_from = None, cursorbody = content[body_start + 1 : end]try:- args = _gemma_parse_stripped_body(body)+ args = _gemma_bare_body_arguments(body)except Exception:args = {}out.append(@@ -3673,6 +3675,77 @@ def _gemma_strip_quoted_leaves(value: Any) -> Any:return value+_GEMMA_SLOT = "\ue000"+_GEMMA_SLOT_RE = re.compile(_GEMMA_SLOT + r"(\d+)" + _GEMMA_SLOT)+++def _slot_gemma_values(body: str) -> "tuple[str, list[str]]":+ """``body`` with each ``<|"|>`` value that opens a value outside an ordinary quoted string replaced by a+ placeholder, and the raw values in order. Quotes open where the quote-less parser opens them."""+ out: list[str] = []+ raw: list[str] = []+ quote = ""+ prev = ":"+ i, n = 0, len(body)+ while i < n:+ ch = body[i]+ if quote:+ if ch == "\\" and i + 1 < n:+ out.append(body[i : i + 2])+ i += 2+ continue+ if ch == quote:+ quote = ""+ elif prev in ":{[," and body.startswith(_GEMMA_STR_END, i):+ end = body.find(_GEMMA_STR_END, i + len(_GEMMA_STR_END))+ if end < 0:+ out.append(body[i:])+ break+ raw.append(body[i + len(_GEMMA_STR_END) : end])+ out.append(f'"{_GEMMA_SLOT}{len(raw) - 1}{_GEMMA_SLOT}"')+ i = end + len(_GEMMA_STR_END)+ prev = '"'+ continue+ elif ch in "\"'" and prev in ":{[(,=":+ quote = ch+ out.append(ch)+ if not ch.isspace():+ prev = ch+ i += 1+ return "".join(out), raw+++def _gemma_bare_body_arguments(body: str) -> dict[str, Any]:+ """Each ``<|"|>``-quoted value is read raw, every other value as the quote-less parser reads it (single+ quotes, nesting). A delimiter that does not end up a whole value (a literal one inside another quoted+ value) keeps the quote-less reading of the whole body."""+ if _GEMMA_QUOTED_VALUE_RE.search(body) is None or _GEMMA_SLOT in body:+ return _gemma_parse_stripped_body(body)+ slotted, raw = _slot_gemma_values(body)+ used: list[int] = []++ def fill(v: Any) -> Any:+ if isinstance(v, dict):+ return {k: fill(x) for k, x in v.items()}+ if isinstance(v, list):+ return [fill(x) for x in v]+ if isinstance(v, str) and _GEMMA_SLOT in v:+ m = _GEMMA_SLOT_RE.fullmatch(v)+ if m is None:+ raise ValueError("delimiter inside a value")+ used.append(int(m.group(1)))+ return raw[used[-1]]+ return v++ try:+ args = fill(_gemma_parse_stripped_body(slotted))+ except ValueError:+ args = None+ if args is None or sorted(used) != list(range(len(raw))):+ return _gemma_parse_stripped_body(body)+ return args++def _gemma_parse_stripped_body(body: str) -> dict[str, Any]:"""Quote-less body: values end at the next top-level ', key:' boundary, so commas in code survive."""out: dict[str, Any] = {}diff --git a/studio/backend/tests/data/refactor_guard/ast_inventory.json b/studio/backend/tests/data/refactor_guard/ast_inventory.jsonindex 0f20337c6a8..be189c6e8b2 100644--- a/studio/backend/tests/data/refactor_guard/ast_inventory.json+++ b/studio/backend/tests/data/refactor_guard/ast_inventory.json@@ -1116,6 +1116,8 @@"_GEMMA_BARE_TC_RE": "'(?<!\\\\w)call\\\\s*:\\\\s*([\\\\w\\\\.\\\\-]+)\\\\s*\\\\{' flags=32","_GEMMA_JSON_NUMBER_RE": "'-?(?:0|[1-9]\\\\d*)(\\\\.\\\\d+)?([eE][+-]?\\\\d+)?' flags=256","_GEMMA_KEY_RE": "'\\\\s*([A-Za-z_][\\\\w.\\\\-]*)\\\\s*:' flags=32",+ "_GEMMA_QUOTED_VALUE_RE": "'(?:^|[{,\\\\[])\\\\s*[A-Za-z_][\\\\w.\\\\-]*\\\\s*:\\\\s*<\\\\|\"\\\\|>' flags=32",+ "_GEMMA_SLOT_RE": "'\\ue000(\\\\d+)\\ue000' flags=32","_GEMMA_TC_RE": "'<\\\\|tool_call>\\\\s*call\\\\s*:\\\\s*([\\\\w\\\\.\\\\-]+)\\\\s*\\\\{' flags=32","_GLM_JSON_NUMERIC_RE": "'-?\\\\d+(?:\\\\.\\\\d+)?(?:[eE][+-]?\\\\d+)?' flags=32","_GLM_TC_OPEN_RE": "'<tool_call>\\\\s*([\\\\w.\\\\-]+)\\\\s*(?=\\\\n|<arg_key>|</tool_call>)' flags=32",@@ -1172,6 +1174,7 @@"re.compile(\"(?im)(\\\\b(i['\\\\u2019](ll|m going to|m gonna)|i am (going to|gonna)|i will|i shall)\\\\b(?!\\\\s+(?:not|never)\\\\b)(?!\\\\s+\" + _HELP_OFFER + ')|\\\\b(?:let me|allow me)\\\\b(?!\\\\s+(?:not|never|know)\\\\b)(?!\\\\s+' + _SIGN_OFF + ')(?!\\\\s+to\\\\s+' + _HELP_OFFER + \")|(?:^|[.!?]\\\\s+)\\\\s*(?:the\\\\s+)?first\\\\s+step\\\\b|(?:^|[.!?]\\\\s+)\\\\s*first\\\\s*[,:–—-]?\\\\s+(?:my|our)\\\\s+(?:plan|approach|step)\\\\b|(?:^|[.!?]\\\\s+)\\\\s*first\\\\s*[,:–—-]?\\\\s+(?:i|we|let['’]?s|let us)\\\\b|(?:^|[.!?]\\\\s+)\\\\s*first\\\\s*[,:–—-]?\\\\s+\" + _ACTION_VERB + \"\\\\b|\\\\bstep \\\\d+[ \\\\t*_]*(?::[ \\\\t*_\\\\r]*$|(?:[:,.)\\\\u2013\\\\u2014-][ \\\\t*_]*)?(?:\\\\r?\\\\n(?:[ \\\\t]*\\\\r?\\\\n)?[ \\\\t*_#>-]*(?:\\\\d+[.)][ \\\\t*_]*)?)?(?:(?:let['’]?s|let us)\\\\b|\" + _ACTION_VERB + \"\\\\b))|\\\\bhere['\\\\u2019]?s (?:my |the |a )?(?:plan|approach)|\\\\b(?:now i|next i)\\\\b)\")": 1,"re.compile('\"[\\\\s}\\\\]]*,\\\\s*(?:\"[A-Za-z_][\\\\w\\\\-]*\"|\\\\\\'[A-Za-z_][\\\\w\\\\-]*\\\\\\'|[A-Za-z_][\\\\w\\\\-]*)\\\\s*:')": 1,"re.compile('\"name\"\\\\s*:\\\\s*\"([^\"]+)\"')": 1,+ "re.compile('(?:^|[{,\\\\[])\\\\s*[A-Za-z_][\\\\w.\\\\-]*\\\\s*:\\\\s*<\\\\|\"\\\\|>')": 1,"re.compile('(?<!\\\\w)call\\\\s*(?::\\\\s*[\\\\w\\\\.\\\\-]*)?$')": 1,"re.compile('(?<!\\\\w)call\\\\s*:\\\\s*([\\\\w\\\\.\\\\-]+)\\\\s*\\\\{')": 1,"re.compile('-?(?:0|[1-9]\\\\d*)(\\\\.\\\\d+)?([eE][+-]?\\\\d+)?', re.ASCII)": 1,@@ -1221,7 +1224,8 @@"re.compile(_DEEPSEEK_OPEN_RE_SRC + '(?=\\\\s*(?:<|tool▁call▁begin|>|function)|\\\\s*$).*$', re.DOTALL)": 1,"re.compile(_DEEPSEEK_OPEN_RE_SRC + '.*?<|tool▁calls▁end|>', re.DOTALL)": 1,"re.compile(_DEEPSEEK_OPEN_RE_SRC + '|' + re.escape(_KIMI_SECTION_BEGIN) + '|' + re.escape(_KIMI_CALL_BEGIN))": 1,- "re.compile(_DEEPSEEK_OPEN_RE_SRC)": 1+ "re.compile(_DEEPSEEK_OPEN_RE_SRC)": 1,+ "re.compile(_GEMMA_SLOT + '(\\\\d+)' + _GEMMA_SLOT)": 1},"symbols": {"BUDGET_EXHAUSTED_NUDGE": {@@ -1412,6 +1416,15 @@"_GEMMA_KEY_RE": {"kind": "assign"},+ "_GEMMA_QUOTED_VALUE_RE": {+ "kind": "assign"+ },+ "_GEMMA_SLOT": {+ "kind": "assign"+ },+ "_GEMMA_SLOT_RE": {+ "kind": "assign"+ },"_GEMMA_STR_BEGIN": {"kind": "assign"},@@ -1671,6 +1684,11 @@"kind": "def","signature": "text: str, brace_pos: int, hard_stop: int"},+ "_gemma_bare_body_arguments": {+ "decorators": [],+ "kind": "def",+ "signature": "body: str"+ },"_gemma_body_brace_end": {"decorators": [],"kind": "def",@@ -1918,6 +1936,11 @@"kind": "def","signature": "text: str, pos: int"},+ "_slot_gemma_values": {+ "decorators": [],+ "kind": "def",+ "signature": "body: str"+ },"_strictly_inside": {"decorators": [],"kind": "def",diff --git a/studio/backend/tests/data/refactor_guard/golden_outputs.json b/studio/backend/tests/data/refactor_guard/golden_outputs.jsonindex 67f84c9950e..659f6b920df 100644--- a/studio/backend/tests/data/refactor_guard/golden_outputs.json+++ b/studio/backend/tests/data/refactor_guard/golden_outputs.json@@ -13,6 +13,7 @@"_first_foreign_tool_signal": "253f37233f0cd47bb9a1f4e2cb04072d1575b1245d5f0a2c0fc7190dfa138354","_first_sentinel": "a5aebe5d9a8bdd81671d6c572517e0e8526d269db1a0fdf92e86c618ce8155e0","_gemma_balanced_brace_end": "a5fbd98554937c109db0e3b848ba997193994c85025b383c4958452b913d7e5d",+ "_gemma_bare_body_arguments": "b9ae7baf34cbff7fdbf0bb883417d77785d8246a703b160029e7297c0ecc0631","_gemma_body_brace_end": "ad13a08b3a789c52bec6e851b53cf4b90c7b794904f3ee54bf12d4042e4b0820","_gemma_call_is_anchored": "c52e950286578ec37883135946183a85ab3f74b03690722408e40ab65e7f7c6a","_gemma_coerce_scalar": "4649c55ae145ed01354b7bc6dc730fe150235e6f197d1d0b50978eed0b23c811",@@ -42,7 +43,7 @@"_parse_closed_call_unescaped_quotes": "f5b2cba82abb209bd409fbe327b458c0177a2d09c18b0fab2cd75b6ea64e12bb","_parse_deepseek_tool_calls": "b16631fa1f86628069da6662060cd78f6c2b1edc87e15011bcec5103a450cabd","_parse_function_xml": "8e47d9a73bea635e90c3a8c56b8be91b3e42d1bbf75c36795fad2bdefeb71696",- "_parse_gemma_tool_calls": "2f1fc545935ef45a31aa2dc32870ee4fc8cfa93d0121f0bcf714c54ac469329f",+ "_parse_gemma_tool_calls": "b2042d58638346a09c9f52674bb0b525a60bd2a442c8d7afb793faa790656654","_parse_glm_tool_calls": "a59501129afac2b10bf29a43097954b5f9cbcebbcf6202982e43f37c7510a190","_parse_kimi_section_body": "5dc402646fc4b4a4fa5c3221f341502911f8904b4248d3108e3c3d028ed85e0f","_parse_kimi_tool_calls": "5dc402646fc4b4a4fa5c3221f341502911f8904b4248d3108e3c3d028ed85e0f",@@ -57,6 +58,7 @@"_safe_cut": "94cc4fd4cac2a7dbdb65fbb2773fce741b1869b7a528fb1f35e416fe680bd858","_signal_inside_leading_wrapperless_gemma": "e86b5a942f855e7228d865031c7a2d03c60837a0c7d219475013295558ba19c1","_skip_mistral_call_id": "df19f4198fa8e5f7472a47ae59a8efe5aadf15737dacf7c8c0f2d8f367b45f34",+ "_slot_gemma_values": "dbe726cc8ab182c5dce85444e996eed1de75a62d753b2ee6201ad5d3775895e7","_string_content_spans": "87ab6652018fa4bd7b4e986eadaa46f29362693f1eca52cb7cae130dd74ff4f1","_strip_function_xml_calls": "88b342ae0b8dad25cfbbaeaf2f77b5f3a1fea9c2553d03f7cad2e19b5230594a","_strip_gemma_wrapperless_calls": "16f797057d8fac12dd00950f3c172ba8a9ab7bad5918e9b905cbf2ffd25382ce",@@ -82,7 +84,7 @@"is_reprompt_restatement": "5a3fe224ba657bf9ea1706ef997b81e5c97535b0e03d6699962f386ecbaf15a0","is_short_intent_without_action": "1ed247c04b2b2bae560ef52224328500d36644b68274015b8efd2c2b9072ac4b","leading_bare_gemma_call_is_promotable": "f97184954908b5ae23160649f0aadf26a8f3f1b65534d9cc350d91504f478a94",- "parse_tool_calls_from_text": "d577ff6ffb72d4a79cde3cac59149ba0a3d923f155ec43c54596410fc8e4d9f4",+ "parse_tool_calls_from_text": "93d746eaac6c90e7910e5c4b310e5218add5d0340919d4ab843e55563f9c9334","promotable_gemma_call_pos": "9439dc754e212c8a7a203c6740a782e4885c2c61ba413fc4ff0948ea7200e966","repeated_result_message": "d6e89e731201e216a32e40ccaadc19eaf0f796cee28f4af73675b731666f7e27","reprompt_to_act_message": "27b5a6e3694684acde1db0e63b2415462f24ab5a5825e37a30f00912ebcac01f",diff --git a/studio/backend/tests/data/refactor_guard/runtime_inventory.json b/studio/backend/tests/data/refactor_guard/runtime_inventory.jsonindex 0e836dfedf8..e96715bd741 100644--- a/studio/backend/tests/data/refactor_guard/runtime_inventory.json+++ b/studio/backend/tests/data/refactor_guard/runtime_inventory.json@@ -47,6 +47,9 @@"_GEMMA_BARE_TC_RE","_GEMMA_JSON_NUMBER_RE","_GEMMA_KEY_RE",+ "_GEMMA_QUOTED_VALUE_RE",+ "_GEMMA_SLOT",+ "_GEMMA_SLOT_RE","_GEMMA_STR_BEGIN","_GEMMA_STR_END","_GEMMA_TC_END",@@ -124,6 +127,7 @@"_first_foreign_tool_signal","_first_sentinel","_gemma_balanced_brace_end",+ "_gemma_bare_body_arguments","_gemma_body_brace_end","_gemma_call_is_anchored","_gemma_coerce_scalar",@@ -175,6 +179,7 @@"_search_from_literal","_signal_inside_leading_wrapperless_gemma","_skip_mistral_call_id",+ "_slot_gemma_values","_strictly_inside","_string_content_spans","_strip_function_xml_calls",@@ -240,6 +245,7 @@"_first_foreign_tool_signal": "core.inference.tool_call_parser._first_foreign_tool_signal(content: 'str') -> 'int | None'","_first_sentinel": "core.inference.tool_call_parser._first_sentinel(text: 'str', start: 'int', enabled_tool_names=None) -> 'int'","_gemma_balanced_brace_end": "core.inference.tool_call_parser._gemma_balanced_brace_end(text: 'str', brace_pos: 'int', hard_stop: 'int') -> 'int | None'",+ "_gemma_bare_body_arguments": "core.inference.tool_call_parser._gemma_bare_body_arguments(body: 'str') -> 'dict[str, Any]'","_gemma_body_brace_end": "core.inference.tool_call_parser._gemma_body_brace_end(text: 'str', brace_pos: 'int') -> 'int | None'","_gemma_call_is_anchored": "core.inference.tool_call_parser._gemma_call_is_anchored(text: 'str', start: 'int', floor: 'int') -> 'bool'","_gemma_coerce_scalar": "core.inference.tool_call_parser._gemma_coerce_scalar(raw: 'str') -> 'Any'",@@ -291,6 +297,7 @@"_search_from_literal": "core.inference.tool_call_parser._search_from_literal(pattern: 're.Pattern', literal: 'str', text: 'str', pos: 'int')","_signal_inside_leading_wrapperless_gemma": "core.inference.tool_call_parser._signal_inside_leading_wrapperless_gemma(content: 'str', enabled_tool_names: 'Optional[set]') -> 'bool'","_skip_mistral_call_id": "core.inference.tool_call_parser._skip_mistral_call_id(text: 'str', pos: 'int') -> 'int'",+ "_slot_gemma_values": "core.inference.tool_call_parser._slot_gemma_values(body: 'str') -> \"'tuple[str, list[str]]'\"","_strictly_inside": "core.inference.tool_call_parser._strictly_inside(spans: 'list', pos: 'int') -> 'bool'","_string_content_spans": "core.inference.tool_call_parser._string_content_spans(text: 'str', start: 'int', end: 'int') -> 'list'","_strip_function_xml_calls": "core.inference.tool_call_parser._strip_function_xml_calls(text: 'str', *, final: 'bool') -> 'str'",diff --git a/studio/backend/tests/test_gemma_wrapperless_quoted_arguments.py b/studio/backend/tests/test_gemma_wrapperless_quoted_arguments.pynew file mode 100644index 00000000000..1e36b6cc1be--- /dev/null+++ b/studio/backend/tests/test_gemma_wrapperless_quoted_arguments.py@@ -0,0 +1,62 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Gemma 4 sometimes drops its ``<|tool_call>`` wrapper. The healer promotes that bare ``call:NAME{...}``,+and its ``<|"|>``-quoted values must decode as they do inside the wrapper, not keep the delimiters."""++from __future__ import annotations++import json++import pytest++from core.inference.tool_call_parser import parse_tool_calls_from_text++ENABLED = {"web_search", "search_docs"}+++def _calls(text):+ return [+ (c["function"]["name"], json.loads(c["function"]["arguments"]))+ for c in parse_tool_calls_from_text(text, enabled_tool_names = ENABLED)+ ]++[email protected]("lead", ["", "Let me look that up.\n"])+def test_bare_call_decodes_gemma_quotes_like_the_wrapped_one(lead):+ body = 'call:search_docs{query:<|"|>a, b {c}<|"|>,limit:5,exact:true}'+ want = [("search_docs", {"query": "a, b {c}", "limit": 5, "exact": True})]+ assert _calls(lead + body) == want+ assert _calls("<|tool_call>" + body + "<tool_call|>") == want++[email protected]("body", ["call:web_search{query:x}", 'call:web_search{query:"x"}'])+def test_unquoted_bare_bodies_are_unchanged(body):+ assert _calls(body) == [("web_search", {"query": "x"})]+++def test_literal_marker_inside_a_json_quoted_value_keeps_its_arguments():+ # ``, b:<|"|>`` sits inside a JSON string, so the Gemma decoder rejects the body: keep the+ # quote-less reading rather than running the tool with no arguments.+ assert _calls('call:web_search{query:"a, b:<|"|>c"}') == [+ ("web_search", {"query": 'a, b:<|"|>c'})+ ]+++ "body, want",+ [+ ("query:<|\"|>mars<|\"|>,unit:'celsius'", {"query": "mars", "unit": "celsius"}),+ (+ 'query:<|"|>mars<|"|>,opts:{note:\'<|"|>kept<|"|>\'}',+ {"query": "mars", "opts": {"note": '<|"|>kept<|"|>'}},+ ),+ (+ "query:<|\"|>a, b:c<|\"|>,opts:{unit:'c'},tags:['x','y']",+ {"query": "a, b:c", "opts": {"unit": "c"}, "tags": ["x", "y"]},+ ),+ ('code:<|"|>print("hi")\nx = \'y\'<|"|>', {"code": "print(\"hi\")\nx = 'y'"}),+ ],+)+def test_other_values_keep_their_quote_less_reading(body, want):+ assert _calls("call:web_search{" + body + "}") == [("web_search", want)]