From c7fa6773145cbb0698db6a51d8c3e34940208d0c Mon Sep 17 00:00:00 2001 From: Francesco Menegoni Date: Sat, 31 Jan 2026 19:52:28 +0100 Subject: [PATCH] chat: add thinking/reasoning support for Kimi K2 Pass enable_thinking to the Kimi K2 chat template via additional_context and handle tags at the end of the generated prompt: - When thinking is disabled, immediately close the tag with - When thinking is enabled, set thinking_forced_open = true This allows proper reasoning mode support for Kimi 2.5 / K2 models. Co-Authored-By: Claude Opus 4.5 --- common/chat.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/common/chat.cpp b/common/chat.cpp index 2bf4632669..61ec4ee3cb 100644 --- a/common/chat.cpp +++ b/common/chat.cpp @@ -1882,9 +1882,22 @@ static common_chat_params common_chat_params_init_kimi_k2(const common_chat_temp common_chat_params data; data.grammar_lazy = params.tools.is_array() && !params.tools.empty() && params.tool_choice != COMMON_CHAT_TOOL_CHOICE_REQUIRED; - data.prompt = apply(tmpl, params); + json additional_context = { + {"thinking", params.enable_thinking}, + }; + + data.prompt = apply(tmpl, params, /* messages_override= */ std::nullopt, /* tools_override= */ std::nullopt, additional_context); data.format = COMMON_CHAT_FORMAT_KIMI_K2; + // Handle thinking tags based on prompt ending + if (string_ends_with(data.prompt, "\n") || string_ends_with(data.prompt, "")) { + if (!params.enable_thinking) { + data.prompt += ""; + } else { + data.thinking_forced_open = true; + } + } + data.preserved_tokens = { "", "",