[Bugfix] Handle parenthesized Gemma4 tool calls (#53657)

Signed-off-by: Taneem Ibrahim <taneem.ibrahim@gmail.com>
This commit is contained in:
Taneem Ibrahim
2026-08-25 08:21:42 -04:00
committed by GitHub
parent 2014e47485
commit 9c8e90eb26
+10 -1
View File
@@ -285,7 +285,7 @@ def _parse_gemma4_array(arr_str: str, *, partial: bool = False) -> list:
def _gemma4_arg_converter(raw_args: str, partial: bool) -> str:
"""Convert Gemma4 custom arg format to a JSON string."""
text = raw_args.strip()
if text.endswith("}"):
if text.endswith("}") or text.endswith(")") and text.count("(") < text.count(")"):
text = text[:-1]
parsed = _parse_gemma4_args(text, partial=partial)
@@ -304,6 +304,7 @@ def gemma4_config() -> ParserEngineConfig:
"TOOL_END": TOOL_CALL_END,
"CALL_PREFIX": "call:",
"OPEN_BRACE": "{",
"OPEN_PAREN": "(",
},
token_id_terminals={
"THINK_START": CHANNEL_START,
@@ -351,6 +352,14 @@ def gemma4_config() -> ParserEngineConfig:
ParserState.TOOL_ARGS,
(),
),
(ParserState.TOOL_NAME, "OPEN_PAREN"): Transition(
ParserState.TOOL_ARGS,
(),
),
(ParserState.TOOL_NAME, "TOOL_END"): Transition(
ParserState.CONTENT,
(EventType.TOOL_CALL_END,),
),
(ParserState.TOOL_ARGS, "TOOL_END"): Transition(
ParserState.CONTENT,
(EventType.TOOL_CALL_END,),