Commit f16e79da8 for llama.cpp
commit f16e79da8921a6ad8e6499a68b27d109c5716a0a
Author: jaredmeaux <132780082+jaredmeaux@users.noreply.github.com>
Date: Sat Nov 28 05:22:49 2026 -0600
chat : Allow K2 Horizon tool call during thought (#32993)
Allow tool calls that would otherwise appear inside reasoning/thinking blocks for K2 Horizon.
K2 Horizon models can emit tool calls without closing the thinking block. Without this
fix the parser would only stop on explicit end tag, and the tool call was lost without
executing. With this PR we now end reasoning before the tool call starts.
In short, when reasoning mentions `<ifm|tool_calls>`, we end reasoning there.
My tests cover this case at medium and high effort.
This fixes #32972, and is the same pattern as #32232 (Gemma4) and #32271 (Ministral-3).
- I have read and agree with the [contributing guidelines](https://github.com/ggml-org/llama.cpp/blob/master/CONTRIBUTING.md)
I have used Opus to run and score benchmarks, help find the K2 parser, draft
the fix and test, and see how others have resolved this for other models. I have carefully
examined all changes and tested with my local benchmarks.
I have used Sol to code review twice.
Assisted-by: Claude Opus 5.5
Assisted-by: GPT 6.2 Sol
diff --git a/common/parsers/k2-horizon.cpp b/common/parsers/k2-horizon.cpp
index b02330d2d..164fee757 100644
--- a/common/parsers/k2-horizon.cpp
+++ b/common/parsers/k2-horizon.cpp
@@ -71,6 +71,10 @@ common_chat_params common_chat_params_init_k2_horizon(const common_chat_template
auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE;
auto include_grammar = has_response_format || (has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE);
+ // A tool call may start before the thought is closed, the real closers stay first, the budget sampler forces them
+ const auto think_closers = data.thinking_end_tags;
+ data.thinking_end_tags.push_back(SECTION_START);
+
if (inputs.has_continuation()) {
const auto & msg = inputs.continue_msg;
@@ -86,12 +90,13 @@ common_chat_params common_chat_params_init_k2_horizon(const common_chat_template
auto generation_prompt = p.literal(GEN_PREFIX);
auto think_end = p.choice();
- for (const auto & tag : data.thinking_end_tags) {
+ for (const auto & tag : think_closers) {
think_end |= p.literal(tag);
}
auto think_body = p.until_one_of(data.thinking_end_tags);
auto think_block = [&](const common_peg_parser & body) {
- return p.optional(THINK_START + p.space() + p.through(body, think_end));
+ // Peek only, because the tool call rule will consume <ifm|tool_calls>
+ return p.optional(THINK_START + p.space() + body + (think_end | p.peek(p.literal(SECTION_START))));
};
auto reasoning = extract_reasoning ? think_block(p.reasoning(think_body)) : p.eps();
diff --git a/tests/test-chat.cpp b/tests/test-chat.cpp
index 83c11cc2d..cb075a75e 100644
--- a/tests/test-chat.cpp
+++ b/tests/test-chat.cpp
@@ -5601,6 +5601,22 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
.expect(message_assist_call_thoughts)
.run();
+ // The models sometimes open the tool call section without closing the reasoning at all
+ for (const char * effort : { R"("medium")", R"("high")" }) {
+ tst.test(
+ "I'm\nthinking<ifm|tool_calls>\n"
+ "<ifm|tool_call>special_function\n"
+ "<ifm|arg_key>arg1</ifm|arg_key>\n"
+ "<ifm|arg_value>1</ifm|arg_value>\n"
+ "</ifm|tool_call>\n"
+ "</ifm|tool_calls>")
+ .reasoning_format(COMMON_REASONING_FORMAT_AUTO)
+ .tools({ special_function_tool })
+ .chat_template_kwargs({ { "reasoning_effort", effort } })
+ .expect(message_assist_call_thoughts)
+ .run();
+ }
+
tst.test(
"I'm\nthinking</ifm|think><ifm|tool_calls>\n"
"<ifm|tool_call>{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}</ifm|tool_call>\n"