Skip to content

Commit 993acc7

Browse files
authored
model: update lfm2 parser/renderer for optional thinking (#16359)
1 parent 7ea692c commit 993acc7

4 files changed

Lines changed: 201 additions & 24 deletions

File tree

‎model/parsers/lfm2.go‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ import (
1414
type LFM2ParserState int
1515

1616
const (
17-
LFM2CollectingThinking LFM2ParserState = iota
17+
// LFM2LookingForThinking is the initial state when thinking is enabled.
18+
// LFM2 models emit an explicit <think> tag only when they reason; a direct
19+
// answer has no tag at all. This state waits to see which one the output
20+
// begins with before committing to thinking or content.
21+
LFM2LookingForThinking LFM2ParserState = iota
22+
LFM2CollectingThinking
1823
LFM2CollectingContent
1924
LFM2CollectingToolCalls
2025
)
@@ -70,8 +75,10 @@ func (p *LFM2Parser) setInitialState(lastMessage *api.Message, thinkValue *api.T
7075
return
7176
}
7277

73-
p.state = LFM2CollectingThinking
74-
p.needsThinkingLeadingTrim = true
78+
// Thinking is enabled, but the model decides per-turn whether to reason. Wait
79+
// for a leading <think> tag before treating output as thinking; otherwise it's
80+
// a direct answer.
81+
p.state = LFM2LookingForThinking
7582
}
7683

7784
func (p *LFM2Parser) Init(tools []api.Tool, lastMessage *api.Message, thinkValue *api.ThinkValue) []api.Tool {
@@ -109,6 +116,18 @@ func (lfm2EventToolCall) isLFM2Event() {}
109116

110117
func (p *LFM2Parser) Add(s string, done bool) (content string, thinking string, calls []api.ToolCall, err error) {
111118
p.buffer.WriteString(s)
119+
120+
// On the final chunk a partial "<think>" prefix can never complete, so commit
121+
// the buffered output as a direct answer rather than withholding it.
122+
if done && p.state == LFM2LookingForThinking {
123+
trimmed := strings.TrimLeftFunc(p.buffer.String(), unicode.IsSpace)
124+
if !strings.HasPrefix(trimmed, lfm2ThinkingOpenTag) {
125+
p.buffer.Reset()
126+
p.buffer.WriteString(trimmed)
127+
p.state = LFM2CollectingContent
128+
}
129+
}
130+
112131
events := p.parseEvents()
113132

114133
var toolCalls []api.ToolCall
@@ -180,6 +199,28 @@ func (p *LFM2Parser) eat() ([]lfm2Event, bool) {
180199
}
181200

182201
switch p.state {
202+
case LFM2LookingForThinking:
203+
// Decide whether this turn is a reasoning turn (begins with <think>) or a
204+
// direct answer (no tag). Leading whitespace is ignored either way.
205+
trimmed := strings.TrimLeftFunc(bufStr, unicode.IsSpace)
206+
if strings.HasPrefix(trimmed, lfm2ThinkingOpenTag) {
207+
after := trimmed[len(lfm2ThinkingOpenTag):]
208+
p.buffer.Reset()
209+
p.buffer.WriteString(after)
210+
p.state = LFM2CollectingThinking
211+
p.needsThinkingLeadingTrim = true
212+
return events, true
213+
}
214+
if trimmed == "" || strings.HasPrefix(lfm2ThinkingOpenTag, trimmed) {
215+
// Only whitespace so far, or a partial "<think>" prefix; wait for more.
216+
return events, false
217+
}
218+
// Direct answer: no thinking block.
219+
p.buffer.Reset()
220+
p.buffer.WriteString(trimmed)
221+
p.state = LFM2CollectingContent
222+
return events, true
223+
183224
case LFM2CollectingThinking:
184225
// Strip opening <think> tag if present
185226
if strings.HasPrefix(bufStr, lfm2ThinkingOpenTag) {

‎model/parsers/lfm2_test.go‎

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,36 @@ func TestLFM2Parser(t *testing.T) {
2525
},
2626
{
2727
name: "thinking_content",
28-
input: "I need to think about this...</think>The answer is 42.",
28+
input: "<think>I need to think about this...</think>The answer is 42.",
2929
expectedThinking: "I need to think about this...",
3030
expectedContent: "The answer is 42.",
3131
hasThinking: true,
3232
},
33+
{
34+
name: "direct_answer_with_thinking_enabled",
35+
input: "The answer is 42.",
36+
expectedContent: "The answer is 42.",
37+
hasThinking: true,
38+
},
39+
{
40+
name: "direct_answer_with_tool_call",
41+
input: "I'll check the weather.<|tool_call_start|>[get_weather(location=\"Paris\")]<|tool_call_end|>",
42+
expectedContent: "I'll check the weather.",
43+
expectedCalls: []api.ToolCall{
44+
{
45+
Function: api.ToolCallFunction{
46+
Name: "get_weather",
47+
Arguments: testArgs(map[string]any{
48+
"location": "Paris",
49+
}),
50+
},
51+
},
52+
},
53+
hasThinking: true,
54+
},
3355
{
3456
name: "thinking_with_newlines",
35-
input: "Let me think:\n- Point 1\n- Point 2</think>\n\nHere's my answer.",
57+
input: "<think>Let me think:\n- Point 1\n- Point 2</think>\n\nHere's my answer.",
3658
expectedThinking: "Let me think:\n- Point 1\n- Point 2",
3759
expectedContent: "Here's my answer.",
3860
hasThinking: true,
@@ -98,7 +120,7 @@ func TestLFM2Parser(t *testing.T) {
98120
},
99121
{
100122
name: "thinking_with_tool_call",
101-
input: "Let me check the weather...</think>I'll get that for you.<|tool_call_start|>[get_weather(location=\"Paris\")]<|tool_call_end|>",
123+
input: "<think>Let me check the weather...</think>I'll get that for you.<|tool_call_start|>[get_weather(location=\"Paris\")]<|tool_call_end|>",
102124
expectedThinking: "Let me check the weather...",
103125
expectedContent: "I'll get that for you.",
104126
expectedCalls: []api.ToolCall{
@@ -121,7 +143,7 @@ func TestLFM2Parser(t *testing.T) {
121143
},
122144
{
123145
name: "only_thinking",
124-
input: "Just thinking content</think>",
146+
input: "<think>Just thinking content</think>",
125147
expectedThinking: "Just thinking content",
126148
expectedContent: "",
127149
hasThinking: true,
@@ -140,7 +162,7 @@ func TestLFM2Parser(t *testing.T) {
140162
},
141163
{
142164
name: "thinking_with_unicode",
143-
input: "我在思考这个问题...</think>答案是42。",
165+
input: "<think>我在思考这个问题...</think>答案是42。",
144166
expectedThinking: "我在思考这个问题...",
145167
expectedContent: "答案是42。",
146168
hasThinking: true,
@@ -164,7 +186,7 @@ func TestLFM2Parser(t *testing.T) {
164186
},
165187
{
166188
name: "thinking_with_special_chars",
167-
input: "Let me calculate: 2+2=4 & 3*3=9...</think>The results are correct!",
189+
input: "<think>Let me calculate: 2+2=4 & 3*3=9...</think>The results are correct!",
168190
expectedThinking: "Let me calculate: 2+2=4 & 3*3=9...",
169191
expectedContent: "The results are correct!",
170192
hasThinking: true,
@@ -228,7 +250,7 @@ func TestLFM2Parser(t *testing.T) {
228250
},
229251
{
230252
name: "thinking_then_python_tool_call",
231-
input: "I should check the status...</think>Let me look that up.<|tool_call_start|>[get_status(id=\"123\")]<|tool_call_end|>",
253+
input: "<think>I should check the status...</think>Let me look that up.<|tool_call_start|>[get_status(id=\"123\")]<|tool_call_end|>",
232254
expectedThinking: "I should check the status...",
233255
expectedContent: "Let me look that up.",
234256
expectedCalls: []api.ToolCall{
@@ -291,7 +313,7 @@ func TestLFM2Parser(t *testing.T) {
291313
},
292314
{
293315
name: "thinking_directly_to_tool_call",
294-
input: "Let me run this command...</think><|tool_call_start|>[bash(command='ls')]<|tool_call_end|>",
316+
input: "<think>Let me run this command...</think><|tool_call_start|>[bash(command='ls')]<|tool_call_end|>",
295317
expectedThinking: "Let me run this command...",
296318
expectedContent: "",
297319
expectedCalls: []api.ToolCall{
@@ -350,11 +372,17 @@ func TestLFM2Parser_Streaming(t *testing.T) {
350372
},
351373
{
352374
name: "streaming_thinking",
353-
chunks: []string{"I need to ", "think about this", "...</think>", "The answer is 42."},
375+
chunks: []string{"<think>", "I need to ", "think about this", "...</think>", "The answer is 42."},
354376
expectedThinking: "I need to think about this...",
355377
expectedContent: "The answer is 42.",
356378
hasThinking: true,
357379
},
380+
{
381+
name: "streaming_direct_answer",
382+
chunks: []string{"The answer ", "is ", "42."},
383+
expectedContent: "The answer is 42.",
384+
hasThinking: true,
385+
},
358386
{
359387
name: "streaming_tool_call",
360388
chunks: []string{"I'll check weather.", "<|tool_call_start|>", "[get_weather(", "location=\"Paris\")]", "<|tool_call_end|>"},
@@ -373,7 +401,7 @@ func TestLFM2Parser_Streaming(t *testing.T) {
373401
},
374402
{
375403
name: "streaming_thinking_with_partial_tag",
376-
chunks: []string{"Thinking about this", "...</", "think>", "Done thinking."},
404+
chunks: []string{"<think>", "Thinking about this", "...</", "think>", "Done thinking."},
377405
expectedThinking: "Thinking about this...",
378406
expectedContent: "Done thinking.",
379407
hasThinking: true,
@@ -401,6 +429,22 @@ func TestLFM2Parser_Streaming(t *testing.T) {
401429
},
402430
hasThinking: false,
403431
},
432+
{
433+
// Opening <think> tag split across chunks must still be recognized.
434+
name: "streaming_thinking_split_open_tag",
435+
chunks: []string{"<th", "ink>", "reasoning", "</think>", "answer"},
436+
expectedThinking: "reasoning",
437+
expectedContent: "answer",
438+
hasThinking: true,
439+
},
440+
{
441+
// A direct answer starting with '<' is ambiguous until enough arrives
442+
// to rule out an opening <think> tag.
443+
name: "streaming_direct_answer_starting_with_angle",
444+
chunks: []string{"<", "html> is a tag"},
445+
expectedContent: "<html> is a tag",
446+
hasThinking: true,
447+
},
404448
{
405449
// Test that leading whitespace after <think> is trimmed even when in separate chunks
406450
name: "streaming_thinking_whitespace_after_tag",
@@ -506,9 +550,9 @@ func TestLFM2Parser_Init(t *testing.T) {
506550
t.Errorf("Init() returned tools mismatch (-want +got):\n%s", diff)
507551
}
508552

509-
// Test initial state is set to thinking when enabled
510-
if parser.state != LFM2CollectingThinking {
511-
t.Errorf("Expected initial state to be LFM2CollectingThinking, got %v", parser.state)
553+
// Test initial state looks for a leading <think> tag when thinking is enabled
554+
if parser.state != LFM2LookingForThinking {
555+
t.Errorf("Expected initial state to be LFM2LookingForThinking, got %v", parser.state)
512556
}
513557
}
514558

@@ -1071,18 +1115,24 @@ func TestLFM2Parser_EdgeCases(t *testing.T) {
10711115
}{
10721116
{
10731117
name: "multiple_think_close_tags",
1074-
input: "First thought</think>Second thought</think>Final content",
1118+
input: "<think>First thought</think>Second thought</think>Final content",
10751119
expectedThinking: "First thought",
10761120
expectedContent: "Second thought</think>Final content",
10771121
hasThinking: true,
10781122
},
10791123
{
1080-
name: "empty_thinking_content",
1081-
input: "</think>Just content",
1124+
name: "empty_thinking_block",
1125+
input: "<think></think>Just content",
10821126
expectedThinking: "",
10831127
expectedContent: "Just content",
10841128
hasThinking: true,
10851129
},
1130+
{
1131+
name: "direct_answer_with_leading_whitespace",
1132+
input: " \n Hello there",
1133+
expectedContent: "Hello there",
1134+
hasThinking: true,
1135+
},
10861136
{
10871137
name: "thinking_disabled_with_think_tags",
10881138
input: "Some content</think>More content",

‎model/renderers/lfm2.go‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type LFM2Renderer struct {
1717
const lfm2BOSToken = "<|startoftext|>"
1818

1919
const (
20+
lfm2ThinkingOpenTag = "<think>"
21+
lfm2ThinkingCloseTag = "</think>"
2022
lfm2ToolListStartTag = "<|tool_list_start|>"
2123
lfm2ToolListEndTag = "<|tool_list_end|>"
2224
lfm2ToolCallStartTag = "<|tool_call_start|>"
@@ -285,18 +287,30 @@ func (r *LFM2Renderer) Render(messages []api.Message, tools []api.Tool, thinkVal
285287

286288
content := r.renderMessageContent(message, imageOffset)
287289
imageOffset += len(message.Images)
288-
if message.Role == "assistant" && !keepPastThinking && i != lastAssistantIndex {
289-
if idx := strings.LastIndex(content, "</think>"); idx >= 0 {
290-
content = strings.TrimSpace(content[idx+len("</think>"):])
291-
}
292-
}
293290
if message.Role == "assistant" && len(message.ToolCalls) > 0 && !strings.Contains(content, lfm2ToolCallStartTag) {
294291
if strings.TrimSpace(content) == "" {
295292
content = lfm2RenderToolCalls(message.ToolCalls) + content
296293
} else {
297294
content = lfm2RenderToolCalls(message.ToolCalls) + "\n" + content
298295
}
299296
}
297+
// Reconstruct the inline <think>...</think> block from the separate
298+
// Thinking field so reasoning turns round-trip in the model's own format:
299+
// thinking precedes any tool calls and content. A direct answer carries no
300+
// Thinking, so nothing is added. Only the thinking variant emits these tags;
301+
// the non-thinking renderer must never send them, including for a trailing
302+
// assistant prefill (which is exempt from the stripping below).
303+
if r.IsThinking && message.Role == "assistant" && message.Thinking != "" && !strings.Contains(content, lfm2ThinkingCloseTag) {
304+
content = lfm2ThinkingOpenTag + message.Thinking + lfm2ThinkingCloseTag + content
305+
}
306+
// Drop reasoning from earlier assistant turns unless thinking is kept; the
307+
// <think>...</think> block is a clean prefix, so everything after the close
308+
// tag (tool calls and content) is preserved.
309+
if message.Role == "assistant" && !keepPastThinking && i != lastAssistantIndex {
310+
if idx := strings.LastIndex(content, lfm2ThinkingCloseTag); idx >= 0 {
311+
content = strings.TrimSpace(content[idx+len(lfm2ThinkingCloseTag):])
312+
}
313+
}
300314
if message.Role == "tool" && !strings.Contains(content, lfm2ToolResponseStartTag) {
301315
content = lfm2ToolResponseStartTag + content + lfm2ToolResponseEndTag
302316
}

‎model/renderers/lfm2_test.go‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,78 @@ func TestLFM2Renderer_ChatTemplateParity(t *testing.T) {
159159
thinkValue: &api.ThinkValue{Value: true},
160160
expected: "<|startoftext|><|im_start|>user\nQ1<|im_end|>\n<|im_start|>assistant\n<think>reason1</think>A1<|im_end|>\n<|im_start|>user\nQ2<|im_end|>\n<|im_start|>assistant\n<think>reason2</think>A2",
161161
},
162+
{
163+
name: "thinking_field_reconstructed_when_enabled",
164+
renderer: &LFM2Renderer{IsThinking: true},
165+
messages: []api.Message{
166+
{Role: "user", Content: "Q1"},
167+
{Role: "assistant", Content: "A1", Thinking: "reason1"},
168+
{Role: "user", Content: "Q2"},
169+
{Role: "assistant", Content: "A2", Thinking: "reason2"},
170+
},
171+
thinkValue: &api.ThinkValue{Value: true},
172+
expected: "<|startoftext|><|im_start|>user\nQ1<|im_end|>\n<|im_start|>assistant\n<think>reason1</think>A1<|im_end|>\n<|im_start|>user\nQ2<|im_end|>\n<|im_start|>assistant\n<think>reason2</think>A2",
173+
},
174+
{
175+
name: "thinking_field_stripped_for_non_last_when_disabled",
176+
renderer: &LFM2Renderer{IsThinking: true},
177+
messages: []api.Message{
178+
{Role: "user", Content: "Q1"},
179+
{Role: "assistant", Content: "A1", Thinking: "reason1"},
180+
{Role: "user", Content: "Q2"},
181+
{Role: "assistant", Content: "A2", Thinking: "reason2"},
182+
},
183+
thinkValue: &api.ThinkValue{Value: false},
184+
expected: "<|startoftext|><|im_start|>user\nQ1<|im_end|>\n<|im_start|>assistant\nA1<|im_end|>\n<|im_start|>user\nQ2<|im_end|>\n<|im_start|>assistant\n<think>reason2</think>A2",
185+
},
186+
{
187+
name: "thinking_precedes_tool_calls",
188+
renderer: &LFM2Renderer{IsThinking: true},
189+
messages: []api.Message{
190+
{Role: "user", Content: "Weather?"},
191+
{
192+
Role: "assistant",
193+
Content: "",
194+
Thinking: "Let me check the weather.",
195+
ToolCalls: []api.ToolCall{
196+
{
197+
Function: api.ToolCallFunction{
198+
Name: "get_weather",
199+
Arguments: testArgs(map[string]any{
200+
"location": "Paris",
201+
}),
202+
},
203+
},
204+
},
205+
},
206+
{Role: "tool", Content: "22C"},
207+
},
208+
thinkValue: &api.ThinkValue{Value: true},
209+
expected: "<|startoftext|><|im_start|>user\nWeather?<|im_end|>\n<|im_start|>assistant\n<think>Let me check the weather.</think><|tool_call_start|>[get_weather(location=\"Paris\")]<|tool_call_end|><|im_end|>\n<|im_start|>tool\n<|tool_response_start|>22C<|tool_response_end|><|im_end|>\n<|im_start|>assistant\n",
210+
},
211+
{
212+
name: "direct_answer_history_has_no_think_tags",
213+
renderer: &LFM2Renderer{IsThinking: true},
214+
messages: []api.Message{
215+
{Role: "user", Content: "Q1"},
216+
{Role: "assistant", Content: "A1"},
217+
{Role: "user", Content: "Q2"},
218+
},
219+
thinkValue: &api.ThinkValue{Value: true},
220+
expected: "<|startoftext|><|im_start|>user\nQ1<|im_end|>\n<|im_start|>assistant\nA1<|im_end|>\n<|im_start|>user\nQ2<|im_end|>\n<|im_start|>assistant\n",
221+
},
222+
{
223+
// The non-thinking variant must never emit <think> tags, even for a
224+
// trailing assistant prefill (which is exempt from thinking stripping).
225+
name: "non_thinking_renderer_drops_thinking_metadata_on_prefill",
226+
renderer: &LFM2Renderer{IsThinking: false},
227+
messages: []api.Message{
228+
{Role: "user", Content: "Hi"},
229+
{Role: "assistant", Content: "Hello", Thinking: "some reasoning"},
230+
},
231+
thinkValue: &api.ThinkValue{Value: false},
232+
expected: "<|startoftext|><|im_start|>user\nHi<|im_end|>\n<|im_start|>assistant\nHello",
233+
},
162234
{
163235
name: "arbitrary_roles_are_rendered_verbatim",
164236
renderer: &LFM2Renderer{IsThinking: false},

0 commit comments

Comments
 (0)