Skip to content

Commit e6f2ec0

Browse files
authored
common : add reasoning_format = none support to gpt-oss (#21094)
1 parent edfb440 commit e6f2ec0

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

common/chat.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
971971
auto has_tools = inputs.tools.is_array() && !inputs.tools.empty();
972972
auto has_response_format = !inputs.json_schema.is_null() && inputs.json_schema.is_object();
973973
auto include_grammar = has_response_format || (has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE);
974+
auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE;
974975

975976
auto parser = build_chat_peg_parser([&](common_chat_peg_builder & p) {
976977
auto start = p.rule("start", p.literal("<|start|>assistant"));
@@ -979,7 +980,13 @@ static common_chat_params common_chat_params_init_gpt_oss(const common_chat_temp
979980
auto channel = p.literal("<|channel|>") + (p.literal("commentary") | p.literal("analysis"));
980981
auto constrain_type = p.chars("[A-Za-z0-9_-]", 1, -1);
981982

982-
auto analysis = p.rule("analysis", p.literal("<|channel|>analysis<|message|>") + p.reasoning(content) + end);
983+
if (extract_reasoning) {
984+
p.rule("analysis", p.literal("<|channel|>analysis<|message|>") + p.reasoning(content) + end);
985+
} else {
986+
p.rule("analysis", p.content(p.literal("<|channel|>analysis<|message|>") + content + end));
987+
}
988+
989+
auto analysis = p.ref("analysis");
983990
auto preamble = p.rule("preamble", p.literal("<|channel|>commentary<|message|>") + p.content(content) + end);
984991
auto final_msg = p.rule("final", p.literal("<|channel|>final<|message|>") + p.content(content));
985992
auto any = p.rule("any", preamble | analysis);

tests/test-chat.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,14 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
27962796
.expect(message_assist_thoughts)
27972797
.run();
27982798

2799+
// Analysis channel (reasoning) with final channel (content) with reasoning_format = none
2800+
tst.test(
2801+
"<|channel|>analysis<|message|>I'm\nthinking<|end|><|start|>assistant<|channel|>final<|message|>Hello, world!\nWhat's "
2802+
"up?")
2803+
.reasoning_format(COMMON_REASONING_FORMAT_NONE)
2804+
.expect_content("<|channel|>analysis<|message|>I'm\nthinking<|end|>Hello, world!\nWhat's up?")
2805+
.run();
2806+
27992807
// Analysis channel only (partial) - still works when reasoning format is set
28002808
tst.test("<|channel|>analysis<|message|>I'm\nthinking")
28012809
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
@@ -2805,24 +2813,28 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
28052813

28062814
// Tool call with recipient in role header: " to=functions.NAME<|channel|>analysis<|message|>JSON"
28072815
tst.test(" to=functions.special_function<|channel|>analysis<|message|>{\"arg1\": 1}")
2816+
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
28082817
.tools({ special_function_tool })
28092818
.expect(message_assist_call)
28102819
.run();
28112820

28122821
// Tool call with recipient in channel header: "<|channel|>analysis to=functions.NAME<|message|>JSON"
28132822
tst.test("<|channel|>analysis to=functions.special_function<|message|>{\"arg1\": 1}")
2823+
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
28142824
.tools({ special_function_tool })
28152825
.expect(message_assist_call)
28162826
.run();
28172827

28182828
// Tool call with constraint: " to=functions.NAME<|channel|>analysis <|constrain|>json<|message|>JSON"
28192829
tst.test(" to=functions.special_function<|channel|>analysis <|constrain|>json<|message|>{\"arg1\": 1}")
2830+
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
28202831
.tools({ special_function_tool })
28212832
.expect(message_assist_call)
28222833
.run();
28232834

28242835
// Tool call in commentary channel (channel header variant)
28252836
tst.test("<|channel|>commentary to=functions.special_function<|message|>{\"arg1\": 1}")
2837+
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
28262838
.tools({ special_function_tool })
28272839
.expect(message_assist_call)
28282840
.run();

0 commit comments

Comments
 (0)