@@ -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 \n Here's my answer." ,
57+ input : "<think> Let me think:\n - Point 1\n - Point 2</think>\n \n Here'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" ,
0 commit comments