chat : fix gemma4 required tool grammar (#29115)

This commit is contained in:
Aldehir Rojas
2026-09-19 18:59:43 -05:00
committed by GitHub
parent 59657a613a
commit f072b10371
2 changed files with 24 additions and 0 deletions
+4
View File
@@ -272,6 +272,10 @@ common_chat_params common_chat_params_init_gemma4(const common_chat_template &
/* max = */ inputs.parallel_tool_calls ? -1 : 1
));
if (inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED) {
return start + thought + tool_call;
}
auto scan_to_toolcall = p.rule("scan-to-toolcall", p.until("<|tool_call>"));
auto content = p.rule("content", p.content(p.until_one_of({"<|channel>", "<channel|>", "<|tool_call>"})));
auto message = p.rule("message", thought + content);
+20
View File
@@ -3032,6 +3032,26 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
.expect(message_with_content_and_tool_call("Hello, world!\nWhat's up?", "get_time", R"({"city": "Paris"})"))
.run();
// Required tool call
tst.test(
"<|tool_call>call:get_time{city:<|\"|>Paris<|\"|>}<tool_call|>")
.tools({ get_time_tool })
.tool_choice(COMMON_CHAT_TOOL_CHOICE_REQUIRED)
.expect(message_with_tool_calls("get_time", R"({"city": "Paris"})"))
.run();
// Required tool call after reasoning
tst.test(
"<|channel>thought\nI'm\nthinking<channel|><|tool_call>call:get_time{city:<|\"|>Paris<|\"|>}<tool_call|>")
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
.tools({ get_time_tool })
.tool_choice(COMMON_CHAT_TOOL_CHOICE_REQUIRED)
.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<|\"|>}<tool_call|>"