Repository navigation
Conversation
…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
requested review from
CatherineSue,
JustinTong0323,
kpham-sgl and
pyc96
as code owners
September 12, 2026 23:17
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. |
3 of 5 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Gemma 4 wraps string tool-call arguments in
<|"|>...<|"|>, but it sometimes omits the opening delimiter and still emits the closing one: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 gemma4we saw it on 6/20 calls at temperature 0.8, 4/20 at 0.5 and 3/20 at 0.3.Gemma4Detectorreads that lone closing delimiter as the start of a string that never ends:parse_streaming_increment):_find_matching_bracenever finds the closing brace, so the call is emitted with its name and empty arguments.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})._parse_gemma4_arraycan 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_braceand 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, throughdetect_and_parseandparse_streaming_incrementonly. It is a separate file so it does not conflict with #37305, which adds broadergemma4_detectortests. 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