diff --git a/common/parsers/gemma4.cpp b/common/parsers/gemma4.cpp index 5902e290b..2a2e929e1 100644 --- a/common/parsers/gemma4.cpp +++ b/common/parsers/gemma4.cpp @@ -303,7 +303,8 @@ common_chat_params common_chat_params_init_gemma4(const common_chat_template & data.format = COMMON_CHAT_FORMAT_PEG_GEMMA4; data.supports_thinking = true; data.thinking_start_tag = "<|channel>thought"; - data.thinking_end_tags = {""}; + // a tool call may start before the thought channel is closed; stays first, the budget sampler forces it + data.thinking_end_tags = {"", "<|tool_call>"}; data.preserved_tokens = { "<|channel>", @@ -334,9 +335,10 @@ common_chat_params common_chat_params_init_gemma4(const common_chat_template & auto start = p.rule("start", p.optional(p.literal("<|turn>model\n"))); if (extract_reasoning) { - p.rule("thought", p.literal("<|channel>thought") + p.space() + p.reasoning(p.until("")) + p.literal("")); + // the model may start a tool call before closing the thought channel + p.rule("thought", p.literal("<|channel>thought") + p.space() + p.reasoning(p.until_one_of({"", "<|tool_call>"})) + (p.literal("") | p.peek(p.literal("<|tool_call>")))); } else { - p.rule("thought", p.content(p.literal("<|channel>thought") + p.space() + p.until("") + p.literal(""))); + p.rule("thought", p.content(p.literal("<|channel>thought") + p.space() + p.until_one_of({"", "<|tool_call>"}) + (p.literal("") | p.peek(p.literal("<|tool_call>"))))); } auto consume_empty_channels = p.gbnf(p.zero_or_more(p.literal("<|channel>") + p.negate(p.literal("thought"))), ""); diff --git a/tests/test-chat.cpp b/tests/test-chat.cpp index 7752015c8..556cf1b2a 100644 --- a/tests/test-chat.cpp +++ b/tests/test-chat.cpp @@ -3178,6 +3178,17 @@ static void test_template_output_peg_parsers(bool detailed_debug) { }) .run(); + // Tool call after reasoning, the model did not close the thought channel (#32231) + tst.test( + "<|channel>thought\nI'm\nthinking<|tool_call>call:get_time{city:<|\"|>Paris<|\"|>}") + .reasoning_format(COMMON_REASONING_FORMAT_AUTO) + .tools({ get_time_tool }) + .expect_reasoning("I'm\nthinking") + .expect_tool_calls({ + { "get_time", R"({"city": "Paris"})", {} }, + }) + .run(); + // Parallel tool calls tst.test( "<|tool_call>call:get_time{city:<|\"|>London<|\"|>}"