Skip to content

Commit 91c631b

Browse files
newjordanaldehir
andauthored
chat : fix reasoning leak with force-opened bare <think> templates (ggml-org#24674)
* chat : fix reasoning leak with force-opened bare <think> templates The reasoning start tag inferred from prior turns can carry trailing whitespace (e.g. <think>\n) while a force-open template prefills a bare <think>. Trim the tag used for the prefix split so the bare prefill is matched instead of being swallowed into content. * chat : fix Nemotron Nano v2 regression --------- Co-authored-by: Alde Rojas <hello@alde.dev>
1 parent efb3036 commit 91c631b

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

common/chat-auto-parser-generator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ common_peg_arena autoparser::build_parser(const generation_params & inputs, cons
147147
} else {
148148
parser = content.build_parser(ctx);
149149
}
150-
return pure_content ? p.prefix(generation_prompt, reasoning.start) + parser : p.prefix(generation_prompt, reasoning.start) << parser;
150+
const std::string reasoning_start = trim_whitespace(reasoning.start);
151+
return pure_content ? p.prefix(generation_prompt, reasoning_start) + parser : p.prefix(generation_prompt, reasoning_start) << parser;
151152
});
152153
}
153154

common/chat-diff-analyzer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ static std::vector<std::function<void(const common_chat_template & tmpl, autopar
124124
analysis.tools.format.section_end = "";
125125
analysis.tools.format.per_call_start = "<TOOLCALL>";
126126
analysis.tools.format.per_call_end = "</TOOLCALL>";
127+
analysis.tools.format.tools_array_wrapped = true;
127128
analysis.content.mode = content_mode::PLAIN;
128129
analysis.content.start = "";
129130
analysis.content.end = "";
130131
analysis.reasoning.mode = reasoning_mode::TAG_BASED;
131-
analysis.reasoning.start = "<think>\n\n";
132+
analysis.reasoning.start = "<think>\n";
132133
analysis.reasoning.end = "</think>";
133134
analysis.assistant_start = "<SPECIAL_11>Assistant";
134135
analysis.user_start = "<SPECIAL_11>User";
135136
analysis.preserved_tokens.clear();
136-
analysis.preserved_tokens.push_back("<SPECIAL_12>");
137137
analysis.preserved_tokens.push_back("<SPECIAL_11>");
138138
analysis.preserved_tokens.push_back("</think>");
139139
analysis.preserved_tokens.push_back("<TOOLCALL>");

tests/test-chat.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4706,9 +4706,16 @@ static void test_template_output_peg_parsers(bool detailed_debug) {
47064706
// Format: <TOOLCALL>[{"name": "func", "arguments": {...}}]</TOOLCALL>
47074707
{
47084708
auto tst = peg_tester("models/templates/NVIDIA-Nemotron-Nano-v2.jinja", detailed_debug);
4709-
tst.test("<TOOLCALL>[{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}]</TOOLCALL>")
4709+
tst.test("I'm\nthinking\n</think>\n<TOOLCALL>[{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}]</TOOLCALL>")
4710+
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
47104711
.tools({ special_function_tool })
4711-
.expect(message_assist_call)
4712+
.expect(message_assist_call_thoughts)
4713+
.run();
4714+
4715+
tst.test("I'm\nthinking\n</think>\n\n<TOOLCALL>[{\"name\": \"special_function\", \"arguments\": {\"arg1\": 1}}]</TOOLCALL>\n")
4716+
.reasoning_format(COMMON_REASONING_FORMAT_AUTO)
4717+
.tools({ special_function_tool })
4718+
.expect(message_assist_call_thoughts)
47124719
.run();
47134720

47144721
// Continuation tests

0 commit comments

Comments
 (0)