Skip to content

[Fix] gemma4_detector: Handle string arguments whose opening delimiter is omitted - #39240

Open
divyvasal wants to merge 2 commits into
sgl-project:mainfrom
divyvasal:gemma4-omitted-opening-string-delimiter
Open

divyvasal wants to merge 2 commits into
sgl-project:mainfrom
divyvasal:gemma4-omitted-opening-string-delimiter

Conversation

@divyvasal

@divyvasal divyvasal commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Gemma 4 wraps string tool-call arguments in <|"|>...<|"|>, but it sometimes omits the opening delimiter and still emits the closing one:

<|tool_call>call:web_search{query:weather in Tokyo<|"|>}<tool_call|>

This is the model, not the server: on google/gemma-4-26B-A4B-it, the HF reference implementation (bf16) puts only p≈0.48 on the opening <|"|> at that position. With the model's default sampling (temperature 1.0, top_p 0.95, top_k 64) it shows up on a sizeable share of calls; against a server running --tool-call-parser gemma4 we saw it on 6/20 calls at temperature 0.8, 4/20 at 0.5 and 3/20 at 0.3.

Gemma4Detector reads that lone closing delimiter as the start of a string that never ends:

  • Streaming (parse_streaming_increment): _find_matching_brace never finds the closing brace, so the call is emitted with its name and empty arguments.
  • Non-streaming (detect_and_parse): the delimiter stays in the value ({"query": "weather in Tokyo<|\"|>"}), and a value containing a comma is split into bogus keys (query:Paris, France<|"|>,limit:5 → {"query": "Paris", "France<|\"|>,limit": 5}).
  • Hang: when an array scan ends unbalanced, _parse_gemma4_array can be handed a stray ]. Its bare-value branch stops on ] without consuming it, so the loop never advances and keeps appending empty values. An omitted delimiter in the last element of a string array is enough (query:[<|"|>a<|"|>,b<|"|>]). Fuzzing both entry points with 8,000 random malformed calls found 6 that never return.

Modifications

  • _skip_string(text, pos): a <|"|> that follows :, [, , or { opens a string and is skipped whole, as before; any other one closes a string whose opening delimiter was omitted, and only that delimiter is skipped. _find_matching_brace and the nested object/array scans use it.
  • _parse_gemma4_args: a bare value that reaches a <|"|> before any :, {, [, ] or } is a string that runs to that delimiter, commas included.
  • _parse_gemma4_value: drops a trailing stray delimiter (array elements).
  • _parse_gemma4_array: skips a stray ] like the other separators, so the loop always advances.

Well-formed calls parse exactly as before.

Tests: test/registered/unit/function_call/test_gemma4_detector_string_delimiters.py, through detect_and_parse and parse_streaming_increment only. It is a separate file so it does not conflict with #37305, which adds broader gemma4_detector tests. All 9 pass with this change. Against the current detector, 4 return wrong or missing arguments and the array case never returns. The same fuzz on this change: 0 hangs and 0 exceptions in 20,000 parses.

Accuracy Tests

Parsing only; no model forward changes. End to end on a Gemma 4 26B-A4B server with --tool-call-parser gemma4: before, a streamed call whose opening delimiter was omitted arrived with empty arguments; after, 60/60 calls at temperature 0.3–0.5 (unary and streamed) carried intact arguments.

Speed Tests and Profiling

N/A: string parsing of the tool-call block only.

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #34725030981
Latest PR Test (Extra): ❌ Run #34725030927
Latest PR Test (AMD ROCm 10): ❌ Run #34725031037

divyvasal and others added 2 commits September 12, 2026 16:13
…miter is omitted

Gemma 4 sometimes omits the opening <|"|> of a string argument and still
emits the closing one (query:weather in Tokyo<|"|>). The detector read the
lone closing delimiter as the start of an unterminated string: streamed calls
lost their arguments, non-streamed calls kept the delimiter in the value or
split a value containing a comma into bogus keys, and a string array ending
in such an element never finished parsing.

A <|"|> now opens a string only after ':', '[', ',' or '{'; any other one is
a stray closing delimiter. A bare value that reaches one before any structural
character is the string up to it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
When an array scan ends unbalanced, the content handed to
_parse_gemma4_array can keep the closing ']'. Its bare-value branch stops on
']' without consuming it, so the loop never advances and keeps appending
empty values. Skip a stray ']' like the other separators.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@divyvasal

Copy link
Copy Markdown
Contributor Author

@JustinTong0323 @kpham-sgl Could one of you run /tag-and-rerun-ci when you get a chance? I'm not in CI_PERMISSIONS.json, so CI stops at the run-ci gate; lint passes. It's a parser-only fix for Gemma 4 tool calls that omit the opening string delimiter, with the repro in the description.

@divyvasal divyvasal changed the title [Bug fix] gemma4_detector: handle string arguments whose opening delimiter is omitted [Fix] gemma4_detector: Handle string arguments whose opening delimiter is omitted Oct 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant