@@ -1423,6 +1423,9 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
14231423    static  const  common_regex user_tool_call_regex (" (?: <\\ |constrain\\ |>([a-zA-Z]+))?<\\ |message\\ |>"  );
14241424    static  const  common_regex builtin_tool_call_regex (" (?:browser|python)[\\ s\\ S]*<\\ |message\\ |>"  );
14251425
1426+     //  Save the channel start so we can roll back to delegate reasoning parsing to builder.
1427+     size_t  channel_start_pos = 0 ;
1428+ 
14261429    auto  consume_until_next = [&](size_t  from = std::string::npos) {
14271430        if  (auto  res = builder.try_find_regex (start_regex, from, false )) {
14281431            auto  begin = res->groups [0 ].begin ;
@@ -1501,25 +1504,11 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
15011504        if  (builder.try_consume_regex (to_regex)) {
15021505            tool_call (false ); //  built-in tools can be called in the analysis channel
15031506        } else  if  (builder.try_consume_regex (message_regex)) {
1504-             std::string reasoning;
1505-             bool  has_end = false ;
1506-             if  (auto  res = builder.try_find_regex (end_regex, std::string::npos, false )) {
1507-                 reasoning = res->prelude ;
1508-                 has_end = true ;
1509-             } else  {
1510-                 reasoning = builder.consume_rest ();
1511-             }
1512- 
1507+             builder.move_to (channel_start_pos);
15131508            if  (builder.syntax ().reasoning_format  == COMMON_REASONING_FORMAT_NONE || builder.syntax ().reasoning_in_content ) {
1514-                 //  the templates raise an exception if <|channel|> is present
1515-                 //  an assistant's content, so wrap it in think tags
1516-                 builder.add_content (" <think>"  );
1517-                 builder.add_content (reasoning);
1518-                 if  (has_end) {
1519-                     builder.add_content (" </think>"  );
1520-                 }
1509+                 builder.add_content (consume_until_next ());
15211510            } else  {
1522-                 builder.add_reasoning_content (reasoning );
1511+                 builder.try_parse_reasoning ( " <|channel|>analysis<|message|> " ,  " <|end|> "  );
15231512            }
15241513        } else  {
15251514            throw  common_chat_msg_parse_exception (" expected: <|message|>, got: "   + consume_until_next ());
@@ -1543,6 +1532,7 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
15431532
15441533    auto  message = [&]() {
15451534        if  (auto  res = builder.try_consume_regex (channel_regex)) {
1535+             channel_start_pos = res->groups [0 ].begin ;
15461536            channel (*res);
15471537        } else  if  (builder.try_consume_regex (to_regex)) {
15481538            tool_call (true );
0 commit comments