Skip to content

Commit 3106c25

Browse files
committed
common : consume trailing channel markers in Gemma 4 tool grammar (#25072)
1 parent ebd048f commit 3106c25

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

common/chat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ static common_chat_params common_chat_params_init_gemma4(const common_chat_templ
13921392
auto scan_to_toolcall = p.rule("scan-to-toolcall", p.until("<|tool_call>"));
13931393
auto content = p.rule("content", p.content(p.until_one_of({"<|channel>", "<channel|>", "<|tool_call>"})));
13941394
auto message = p.rule("message", thought + content);
1395-
return start + p.zero_or_more(message) + scan_to_toolcall + tool_call;
1395+
return start + p.zero_or_more(message) + scan_to_toolcall + tool_call + consume_empty_channels;
13961396
}
13971397

13981398
// Gemma 4 may emit an extra <|channel>thought\n<channel|> at the end of the content. It may

tests/test-chat.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,6 +3070,14 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
30703070
.expect(message_with_tool_calls("amount", R"({"orig": 1.5e10})"))
30713071
.run();
30723072

3073+
// Tool call with trailing <|channel> fragment (#25072)
3074+
tst.test(
3075+
"<|tool_call>call:get_time{city:<|\"|>London<|\"|>}<tool_call|><|channel> ")
3076+
.tools({ get_time_tool })
3077+
.is_partial(true)
3078+
.expect(message_with_tool_calls("get_time", R"({"city": "London"})"))
3079+
.run();
3080+
30733081
// Edge cases
30743082
tst.test(
30753083
"<|channel>thought\n<channel|>Hello, world!\nWhat's up?<channel|>")

0 commit comments

Comments
 (0)