Skip to content

Commit eca4d65

Browse files
committed
gpt-oss : undo think tags when reasoning_format is none
1 parent c7bd058 commit eca4d65

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

common/chat.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

tests/test-chat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ static void test_template_output_parsers() {
17101710
// Test reasoning formats
17111711
assert_msg_equals(
17121712
simple_assist_msg(
1713-
"<think>I'm\nthinking</think>Hello, world!\nWhat's up?"),
1713+
"<|channel|>analysis<|message|>I'm\nthinking<|end|>Hello, world!\nWhat's up?"),
17141714
common_chat_parse(
17151715
"<|channel|>analysis<|message|>I'm\nthinking<|end|>"
17161716
"<|start|>assistant<|channel|>final<|message|>Hello, world!\nWhat's up?",
@@ -1722,7 +1722,7 @@ static void test_template_output_parsers() {
17221722

17231723
assert_msg_equals(
17241724
simple_assist_msg(
1725-
"<think>I'm\nthinking</think>Hello, world!\nWhat's up?"),
1725+
"<|channel|>analysis<|message|>I'm\nthinking<|end|>Hello, world!\nWhat's up?"),
17261726
common_chat_parse(
17271727
"<|channel|>analysis<|message|>I'm\nthinking<|end|>"
17281728
"<|start|>assistant<|channel|>final<|message|>Hello, world!\nWhat's up?",

0 commit comments

Comments
 (0)