Skip to content

Commit 6fa035e

Browse files
committed
model/parsers: fix gemma4 arg parsing when quoted strings contain "
Fixes: #15241
1 parent 3536ef5 commit 6fa035e

2 files changed

Lines changed: 143 additions & 6 deletions

File tree

‎model/parsers/gemma4.go‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,27 +345,69 @@ func parseGemma4ToolCall(content string) (api.ToolCall, error) {
345345

346346
// gemma4ArgsToJSON converts Gemma 4's custom argument format to valid JSON.
347347
func gemma4ArgsToJSON(s string) string {
348-
s = strings.ReplaceAll(s, `<|"|>`, `"`)
348+
const quoteToken = `<|"|>`
349349

350350
var buf strings.Builder
351351
buf.Grow(len(s) + 32)
352-
inString := false
352+
const (
353+
stringModeNone = iota
354+
stringModeGemmaToken
355+
stringModeRawQuote
356+
)
357+
358+
stringMode := stringModeNone
353359
hex := "0123456789abcdef"
354360
i := 0
355361
for i < len(s) {
362+
if strings.HasPrefix(s[i:], quoteToken) {
363+
if stringMode == stringModeGemmaToken {
364+
stringMode = stringModeNone
365+
} else if stringMode == stringModeNone {
366+
stringMode = stringModeGemmaToken
367+
} else {
368+
// In a raw-quote string, treat the Gemma quote token literally.
369+
buf.WriteString(quoteToken)
370+
i += len(quoteToken)
371+
continue
372+
}
373+
buf.WriteByte('"')
374+
i += len(quoteToken)
375+
continue
376+
}
377+
356378
ch := s[i]
357379

358-
if ch == '"' {
359-
inString = !inString
380+
if stringMode == stringModeNone && ch == '"' {
381+
stringMode = stringModeRawQuote
360382
buf.WriteByte('"')
361383
i++
362384
continue
363385
}
364386

365-
if inString {
387+
if stringMode != stringModeNone {
366388
switch ch {
367389
case '\\':
390+
if i+1 < len(s) {
391+
next := s[i+1]
392+
switch next {
393+
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't', 'u':
394+
// Preserve valid JSON escapes that are already in the source string.
395+
buf.WriteByte('\\')
396+
buf.WriteByte(next)
397+
i += 2
398+
continue
399+
}
400+
}
401+
// Unknown escape sequence: treat backslash as a literal character.
368402
buf.WriteString(`\\`)
403+
case '"':
404+
if stringMode == stringModeRawQuote {
405+
stringMode = stringModeNone
406+
buf.WriteByte('"')
407+
} else {
408+
// In Gemma-token strings, raw double quotes are string content.
409+
buf.WriteString(`\"`)
410+
}
369411
case '\n':
370412
buf.WriteString(`\n`)
371413
case '\r':
@@ -389,7 +431,7 @@ func gemma4ArgsToJSON(s string) string {
389431
continue
390432
}
391433

392-
if !inString && isIdentStart(ch) {
434+
if isIdentStart(ch) {
393435
j := i + 1
394436
for j < len(s) && isIdentPart(s[j]) {
395437
j++

‎model/parsers/gemma4_test.go‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ func TestGemma4Parser(t *testing.T) {
133133
},
134134
},
135135
},
136+
{
137+
name: "tool_call_with_array_of_multiple_gemma_quoted_strings",
138+
input: `<|tool_call>call:process{items:[<|"|>a<|"|>,<|"|>b "quoted"<|"|>,<|"|>c<|"|>]}<tool_call|>`,
139+
expectedToolCalls: []api.ToolCall{
140+
{
141+
Function: api.ToolCallFunction{
142+
Name: "process",
143+
Arguments: testArgs(map[string]any{
144+
"items": []any{"a", `b "quoted"`, "c"},
145+
}),
146+
},
147+
},
148+
},
149+
},
136150
{
137151
name: "tool_call_with_multiline_string_arg",
138152
input: `<|tool_call>call:bash{command:<|"|>date
@@ -148,6 +162,62 @@ func TestGemma4Parser(t *testing.T) {
148162
},
149163
},
150164
},
165+
{
166+
name: "tool_call_with_escaped_double_quotes_in_string_arg",
167+
input: `<|tool_call>call:search{query:<|"|>say \"hello\"<|"|>}<tool_call|>`,
168+
expectedToolCalls: []api.ToolCall{
169+
{
170+
Function: api.ToolCallFunction{
171+
Name: "search",
172+
Arguments: testArgs(map[string]any{
173+
"query": `say "hello"`,
174+
}),
175+
},
176+
},
177+
},
178+
},
179+
{
180+
name: "tool_call_with_unescaped_double_quotes_in_string_arg",
181+
input: `<|tool_call>call:search{query:<|"|>say "hello"<|"|>}<tool_call|>`,
182+
expectedToolCalls: []api.ToolCall{
183+
{
184+
Function: api.ToolCallFunction{
185+
Name: "search",
186+
Arguments: testArgs(map[string]any{
187+
"query": `say "hello"`,
188+
}),
189+
},
190+
},
191+
},
192+
},
193+
{
194+
name: "tool_call_with_multiple_unescaped_double_quote_segments",
195+
input: `<|tool_call>call:search{query:<|"|>say "hello", then "goodbye"<|"|>}<tool_call|>`,
196+
expectedToolCalls: []api.ToolCall{
197+
{
198+
Function: api.ToolCallFunction{
199+
Name: "search",
200+
Arguments: testArgs(map[string]any{
201+
"query": `say "hello", then "goodbye"`,
202+
}),
203+
},
204+
},
205+
},
206+
},
207+
{
208+
name: "tool_call_with_mixed_escaped_and_unescaped_double_quotes",
209+
input: `<|tool_call>call:search{query:<|"|>first \"quoted\" then "raw"<|"|>}<tool_call|>`,
210+
expectedToolCalls: []api.ToolCall{
211+
{
212+
Function: api.ToolCallFunction{
213+
Name: "search",
214+
Arguments: testArgs(map[string]any{
215+
"query": `first "quoted" then "raw"`,
216+
}),
217+
},
218+
},
219+
},
220+
},
151221
{
152222
name: "multiple_tool_calls",
153223
input: `<|tool_call>call:get_weather{location:<|"|>Paris<|"|>}<tool_call|><|tool_call>call:get_weather{location:<|"|>London<|"|>}<tool_call|>`,
@@ -410,6 +480,11 @@ func TestGemma4ArgsToJSON(t *testing.T) {
410480
input: `{items:[<|"|>a<|"|>,<|"|>b<|"|>]}`,
411481
expected: `{"items":["a","b"]}`,
412482
},
483+
{
484+
name: "array_value_with_multiple_gemma_quoted_strings",
485+
input: `{items:[<|"|>a<|"|>,<|"|>b "quoted"<|"|>,<|"|>c<|"|>]}`,
486+
expected: `{"items":["a","b \"quoted\"","c"]}`,
487+
},
413488
{
414489
name: "empty_object",
415490
input: `{}`,
@@ -431,6 +506,26 @@ func TestGemma4ArgsToJSON(t *testing.T) {
431506
<|"|>}`,
432507
expected: `{"command":"date\n"}`,
433508
},
509+
{
510+
name: "string_value_with_escaped_double_quotes",
511+
input: `{query:<|"|>say \"hello\"<|"|>}`,
512+
expected: `{"query":"say \"hello\""}`,
513+
},
514+
{
515+
name: "string_value_with_unescaped_double_quotes",
516+
input: `{query:<|"|>say "hello"<|"|>}`,
517+
expected: `{"query":"say \"hello\""}`,
518+
},
519+
{
520+
name: "string_value_with_multiple_unescaped_double_quote_segments",
521+
input: `{query:<|"|>say "hello", then "goodbye"<|"|>}`,
522+
expected: `{"query":"say \"hello\", then \"goodbye\""}`,
523+
},
524+
{
525+
name: "string_value_with_mixed_escaped_and_unescaped_double_quotes",
526+
input: `{query:<|"|>first \"quoted\" then "raw"<|"|>}`,
527+
expected: `{"query":"first \"quoted\" then \"raw\""}`,
528+
},
434529
}
435530

436531
for _, tt := range tests {

0 commit comments

Comments
 (0)