@@ -1423,6 +1423,9 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1423
1423
static const common_regex user_tool_call_regex (" (?: <\\ |constrain\\ |>([a-zA-Z]+))?<\\ |message\\ |>" );
1424
1424
static const common_regex builtin_tool_call_regex (" (?:browser|python)[\\ s\\ S]*<\\ |message\\ |>" );
1425
1425
1426
+ // Save the channel start so we can roll back to delegate reasoning parsing to builder.
1427
+ size_t channel_start_pos = 0 ;
1428
+
1426
1429
auto consume_until_next = [&](size_t from = std::string::npos) {
1427
1430
if (auto res = builder.try_find_regex (start_regex, from, false )) {
1428
1431
auto begin = res->groups [0 ].begin ;
@@ -1501,25 +1504,11 @@ static void common_chat_parse_gpt_oss(common_chat_msg_parser & builder) {
1501
1504
if (builder.try_consume_regex (to_regex)) {
1502
1505
tool_call (false ); // built-in tools can be called in the analysis channel
1503
1506
} 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);
1513
1508
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 ());
1521
1510
} else {
1522
- builder.add_reasoning_content (reasoning );
1511
+ builder.try_parse_reasoning ( " <|channel|>analysis<|message|> " , " <|end|> " );
1523
1512
}
1524
1513
} else {
1525
1514
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) {
1543
1532
1544
1533
auto message = [&]() {
1545
1534
if (auto res = builder.try_consume_regex (channel_regex)) {
1535
+ channel_start_pos = res->groups [0 ].begin ;
1546
1536
channel (*res);
1547
1537
} else if (builder.try_consume_regex (to_regex)) {
1548
1538
tool_call (true );
0 commit comments