@@ -75,6 +75,21 @@ static common_chat_msg normalize(const common_chat_msg & msg) {
7575 }
7676 return normalized;
7777}
78+
79+
80+ // trim whitespace from the beginning and end of a string
81+ static std::string trim (const std::string & str) {
82+ size_t start = 0 ;
83+ size_t end = str.size ();
84+ while (start < end && isspace (static_cast <unsigned char >(str[start]))) {
85+ start += 1 ;
86+ }
87+ while (end > start && isspace (static_cast <unsigned char >(str[end - 1 ]))) {
88+ end -= 1 ;
89+ }
90+ return str.substr (start, end - start);
91+ }
92+
7893template <>
7994bool equals (const common_chat_msg & expected, const common_chat_msg & actual) {
8095 return normalize (expected) == normalize (actual);
@@ -148,15 +163,15 @@ static std::string renormalize_json(const std::string & json_str) {
148163 return json_str;
149164 }
150165}
151- static void assert_msg_equals (const common_chat_msg & expected, const common_chat_msg & actual) {
166+ static void assert_msg_equals (const common_chat_msg & expected, const common_chat_msg & actual, bool ignore_whitespace_differences = false ) {
152167 assert_equals (expected.role , actual.role );
153- assert_equals (expected.content , actual.content );
168+ assert_equals (expected.content , ignore_whitespace_differences ? trim (actual. content ) : actual.content );
154169 assert_equals (expected.content_parts .size (), actual.content_parts .size ());
155170 for (size_t i = 0 ; i < expected.content_parts .size (); i++) {
156171 const auto & expected_part = expected.content_parts [i];
157172 const auto & actual_part = actual.content_parts [i];
158173 assert_equals (expected_part.type , actual_part.type );
159- assert_equals (expected_part.text , actual_part.text );
174+ assert_equals (expected_part.text , ignore_whitespace_differences ? trim (actual_part. text ) : actual_part.text );
160175 }
161176 assert_equals (expected.reasoning_content , actual.reasoning_content );
162177 assert_equals (expected.tool_calls .size (), actual.tool_calls .size ());
@@ -280,14 +295,19 @@ static void test_templates(const struct common_chat_templates * tmpls, const std
280295 const std::string & expected_delta = " " ,
281296 bool expect_grammar_triggered = true ,
282297 bool test_grammar_if_triggered = true ,
283- common_reasoning_format reasoning_format = COMMON_REASONING_FORMAT_NONE) {
298+ common_reasoning_format reasoning_format = COMMON_REASONING_FORMAT_NONE,
299+ bool ignore_whitespace_differences = false
300+ ) {
284301 common_chat_msg user_message;
285302 user_message.role = " user" ;
286303 user_message.content = " Hello, world!" ;
287304
288305 for (const auto & tool_choice : std::vector<common_chat_tool_choice> {COMMON_CHAT_TOOL_CHOICE_AUTO, COMMON_CHAT_TOOL_CHOICE_REQUIRED}) {
289306 auto data = init_delta (tmpls, end_tokens, user_message, test_message, tools, tool_choice);
290307 if (!expected_delta.empty ()) {
308+ if (ignore_whitespace_differences) {
309+ data.delta = trim (data.delta );
310+ }
291311 assert_equals (expected_delta, data.delta );
292312 }
293313
@@ -296,7 +316,7 @@ static void test_templates(const struct common_chat_templates * tmpls, const std
296316 syntax.format = data.params .format ;
297317 syntax.reasoning_format = reasoning_format;
298318 const auto msg = common_chat_parse (data.delta , /* is_partial= */ false , syntax);
299- assert_msg_equals (test_message, msg);
319+ assert_msg_equals (test_message, msg, ignore_whitespace_differences );
300320 }
301321
302322 if (!test_message.tool_calls .empty ()) {
@@ -2289,7 +2309,7 @@ Hey there!<|im_end|>
22892309 }
22902310
22912311 {
2292- auto tmpls = read_templates (" models/templates/MiniMax-M2.jinja" );
2312+ auto tmpls = read_templates (" models/templates/unsloth- MiniMax-M2.jinja" );
22932313 std::vector<std::string> end_tokens{ " [e~[" };
22942314
22952315 assert_equals (COMMON_CHAT_FORMAT_MINIMAX_M2, common_chat_templates_apply (tmpls.get (), inputs_no_tools).format );
@@ -2355,7 +2375,10 @@ Hey there!<|im_end|>
23552375 // Test template generation for tool calls
23562376 test_templates (tmpls.get (), end_tokens, message_assist_call, tools,
23572377 " <minimax:tool_call>\n <invoke name=\" special_function\" >\n <parameter name=\" arg1\" >1</parameter>\n </invoke>\n </minimax:tool_call>" ,
2358- /* expect_grammar_triggered= */ true
2378+ /* expect_grammar_triggered= */ true ,
2379+ /* test_grammar_if_triggered= */ true ,
2380+ /* common_reasoning_format= */ COMMON_REASONING_FORMAT_NONE,
2381+ /* ignore_whitespace_differences= */ true
23592382 );
23602383
23612384 }
0 commit comments