@@ -809,16 +809,26 @@ common_chat_templates_ptr common_chat_templates_init(
809809 " {%- set content = content.split('</think>') -%} {%- set content = content|last -%} {%- set content = content.strip('\\ n') %}" );
810810 if (default_template_src.find (" {%- set last_tool_call.name = message.tool_calls[-1].name -%}" ) != std::string::npos &&
811811 default_template_src.find (" {%- for tool_call in message.tool_calls -%}" ) != std::string::npos) {
812+ LOG_INF (" Detected MiniMax-M2 official template bug: \" last_tool_call.name = message.tool_calls[-1].name\" , applying automatic fix...\n " );
812813 string_replace_all (default_template_src, " {%- set last_tool_call.name = message.tool_calls[-1].name -%}" , " " );
813814 string_replace_all (default_template_src,
814815 " {%- for tool_call in message.tool_calls -%}" ,
815816 " {%- for tool_call in message.tool_calls -%} {%- set last_tool_call.name = tool_call.function.name -%}" );
816817 }
818+ if (default_template_src.find (" {%- set last_tool_call.name = message.tool_calls[-1].function.name -%}" ) != std::string::npos &&
819+ default_template_src.find (" {%- for tool_call in message.tool_calls -%}" ) != std::string::npos) {
820+ LOG_INF (" Detected MiniMax-M2 unsloth template, applying automatic fix...\n " );
821+ string_replace_all (default_template_src, " {%- set last_tool_call.name = message.tool_calls[-1].function.name -%}" , " " );
822+ string_replace_all (default_template_src,
823+ " {%- for tool_call in message.tool_calls -%}" ,
824+ " {%- for tool_call in message.tool_calls -%} {%- set last_tool_call.name = tool_call.function.name -%}" );
825+ }
817826 LOG_INF (" MiniMax-M2 template fixed\n " );
818827 }
819828 if (default_template_src.find (" ]~!b[" ) != std::string::npos
820829 && default_template_src.find (" ]~b]" ) != std::string::npos
821830 && default_template_src.find (" {% set _args = tool_call.arguments %}" ) != std::string::npos) {
831+ LOG_INF (" Detected MiniMax-M2 official template bug: unchecked tool_call.arguments , applying automatic fix...\n " );
822832 string_replace_all (default_template_src, " {% set _args = tool_call.arguments %}" ,
823833 " {%- if tool_call.arguments is defined and tool_call.arguments is mapping -%} {%- set _args = tool_call.arguments -%} {%- else -%} {%- set _args = {} -%} {%- endif -%}" );
824834 }
@@ -870,6 +880,8 @@ const char * common_chat_format_name(common_chat_format format) {
870880 case COMMON_CHAT_FORMAT_GENERIC: return " Generic" ;
871881 case COMMON_CHAT_FORMAT_MISTRAL_NEMO: return " Mistral Nemo" ;
872882 case COMMON_CHAT_FORMAT_MAGISTRAL: return " Magistral" ;
883+ case COMMON_CHAT_FORMAT_MINIMAX_M2: return " MiniMax-M2" ;
884+ case COMMON_CHAT_FORMAT_GLM_4_5: return " GLM 4.5" ;
873885 case COMMON_CHAT_FORMAT_LLAMA_3_X: return " Llama 3.x" ;
874886 case COMMON_CHAT_FORMAT_LLAMA_3_X_WITH_BUILTIN_TOOLS: return " Llama 3.x with builtin tools" ;
875887 case COMMON_CHAT_FORMAT_DEEPSEEK_R1: return " DeepSeek R1" ;
@@ -885,8 +897,6 @@ const char * common_chat_format_name(common_chat_format format) {
885897 case COMMON_CHAT_FORMAT_NEMOTRON_V2: return " Nemotron V2" ;
886898 case COMMON_CHAT_FORMAT_APERTUS: return " Apertus" ;
887899 case COMMON_CHAT_FORMAT_LFM2_WITH_JSON_TOOLS: return " LFM2 with JSON tools" ;
888- case COMMON_CHAT_FORMAT_GLM_4_5: return " GLM 4.5" ;
889- case COMMON_CHAT_FORMAT_MINIMAX_M2: return " MiniMax-M2" ;
890900 default :
891901 throw std::runtime_error (" Unknown chat format" );
892902 }
@@ -1611,7 +1621,7 @@ inline void parse_msg_with_xml_tool_calls(common_chat_msg_parser & builder, cons
16111621 auto tc = builder.try_find_regex (tool_call_start_regex, std::string::npos, false );
16121622 std::string content;
16131623 std::string tool_call_start;
1614-
1624+
16151625 if (tc) {
16161626 content = std::move (tc->prelude );
16171627 tool_call_start = builder.str (tc->groups [0 ]);
@@ -2696,7 +2706,7 @@ static common_chat_params common_chat_params_init_minimax_m2(const common_chat_t
26962706 common_chat_params data;
26972707
26982708 // Disable every Minja polyfill except object_arguments
2699- minja::chat_template_options topts;
2709+ minja::chat_template_options topts {} ;
27002710 topts.apply_polyfills = true ;
27012711 topts.polyfill_tools = false ;
27022712 topts.polyfill_tool_call_examples = false ;
@@ -2745,21 +2755,14 @@ static common_chat_params common_chat_params_init_minimax_m2(const common_chat_t
27452755}
27462756
27472757static void common_chat_parse_minimax_m2 (common_chat_msg_parser & builder) {
2748- if (!builder.syntax ().parse_tool_calls ) {
2749- // MiniMax-M2 uses <think>...</think> tags for reasoning content
2750- builder.try_parse_reasoning (" <think>" , " </think>" );
2751- builder.add_content (builder.consume_rest ());
2752- return ;
2753- }
2754-
27552758 static const xml_tool_call_format form {
2756- /* form.scope_start = */ " <minimax:tool_call>\n " ,
2759+ /* form.scope_start = */ " <minimax:tool_call>" ,
27572760 /* form.tool_start = */ " <invoke name=\" " ,
2758- /* form.tool_sep = */ " \" >\n " ,
2761+ /* form.tool_sep = */ " \" >" ,
27592762 /* form.key_start = */ " <parameter name=\" " ,
27602763 /* form.key_val_sep = */ " \" >" ,
2761- /* form.val_end = */ " </parameter>\n " ,
2762- /* form.tool_end = */ " </invoke>\n " ,
2764+ /* form.val_end = */ " </parameter>" ,
2765+ /* form.tool_end = */ " </invoke>" ,
27632766 /* form.scope_end = */ " </minimax:tool_call>" ,
27642767 };
27652768 parse_msg_with_xml_tool_calls (builder, form, " <think>" , " </think>" );
@@ -2987,7 +2990,7 @@ static common_chat_params common_chat_params_init_glm_4_5(const common_chat_temp
29872990 common_chat_params data;
29882991
29892992 // Disable every Minja polyfill except object_arguments
2990- minja::chat_template_options topts;
2993+ minja::chat_template_options topts {} ;
29912994 topts.apply_polyfills = true ;
29922995 topts.polyfill_tools = false ;
29932996 topts.polyfill_tool_call_examples = false ;
@@ -3075,13 +3078,6 @@ static common_chat_params common_chat_params_init_glm_4_5(const common_chat_temp
30753078}
30763079
30773080static void common_chat_parse_glm_4_5 (common_chat_msg_parser & builder) {
3078- if (!builder.syntax ().parse_tool_calls ) {
3079- builder.consume_spaces ();
3080- builder.try_parse_reasoning (" <think>" , " </think>" );
3081- builder.add_content (builder.consume_rest ());
3082- return ;
3083- }
3084-
30853081 static const xml_tool_call_format form {
30863082 /* form.scope_start = */ " " ,
30873083 /* form.tool_start = */ " <tool_call>" ,
@@ -3759,13 +3755,6 @@ static void common_chat_parse_lfm2(common_chat_msg_parser & builder) {
37593755}
37603756
37613757static void common_chat_parse_seed_oss (common_chat_msg_parser & builder) {
3762- if (!builder.syntax ().parse_tool_calls ) {
3763- // Parse thinking tags first - this handles the main reasoning content
3764- builder.try_parse_reasoning (" <seed:think>" , " </seed:think>" );
3765- builder.add_content (builder.consume_rest ());
3766- return ;
3767- }
3768-
37693758 // static const xml_tool_call_format form {
37703759 // /* form.scope_start = */ "<seed:tool_call>\n",
37713760 // /* form.tool_start = */ "<function=",
@@ -4102,6 +4091,12 @@ static void common_chat_parse(common_chat_msg_parser & builder) {
41024091 case COMMON_CHAT_FORMAT_MAGISTRAL:
41034092 common_chat_parse_magistral (builder);
41044093 break ;
4094+ case COMMON_CHAT_FORMAT_MINIMAX_M2:
4095+ common_chat_parse_minimax_m2 (builder);
4096+ break ;
4097+ case COMMON_CHAT_FORMAT_GLM_4_5:
4098+ common_chat_parse_glm_4_5 (builder);
4099+ break ;
41054100 case COMMON_CHAT_FORMAT_LLAMA_3_X:
41064101 common_chat_parse_llama_3_1 (builder);
41074102 break ;
@@ -4147,12 +4142,6 @@ static void common_chat_parse(common_chat_msg_parser & builder) {
41474142 case COMMON_CHAT_FORMAT_LFM2_WITH_JSON_TOOLS:
41484143 common_chat_parse_lfm2 (builder);
41494144 break ;
4150- case COMMON_CHAT_FORMAT_GLM_4_5:
4151- common_chat_parse_glm_4_5 (builder);
4152- break ;
4153- case COMMON_CHAT_FORMAT_MINIMAX_M2:
4154- common_chat_parse_minimax_m2 (builder);
4155- break ;
41564145 default :
41574146 throw std::runtime_error (std::string (" Unsupported format: " ) + common_chat_format_name (builder.syntax ().format ));
41584147 }
0 commit comments