Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

parsers: refactor Nemotron parser to reuse Qwen3Coder for tool calls (#13764)

Jeffrey Morgan3d01f2aa341d
diff --git a/model/parsers/nemotron3nano.go b/model/parsers/nemotron3nano.go
index 7fda8cdc7..eaa6cb87a 100644
--- a/model/parsers/nemotron3nano.go
+++ b/model/parsers/nemotron3nano.go
@@ -1,7 +1,6 @@
package parsers
import (
- "regexp"
"strings"
"unicode"
@@ -14,243 +13,114 @@ const (
Nemotron3NanoCollectingThinking Nemotron3NanoParserState = iota
Nemotron3NanoSkipWhitespaceAfterThinking
Nemotron3NanoCollectingContent
- Nemotron3NanoCollectingToolCalls
)
const (
- nemotronThinkClose = "</think>"
- nemotronToolCallOpen = "<tool_call>"
- nemotronToolCallClose = "</tool_call>"
+ nemotronThinkClose = "</think>"
+ nemotronToolCallOpen = "<tool_call>"
)
type Nemotron3NanoParser struct {
- state Nemotron3NanoParserState
- buffer strings.Builder
- tools []api.Tool
+ state Nemotron3NanoParserState
+ buffer strings.Builder
+ toolParser *Qwen3CoderParser
}
func (p *Nemotron3NanoParser) HasToolSupport() bool { return true }
func (p *Nemotron3NanoParser) HasThinkingSupport() bool { return true }
func (p *Nemotron3NanoParser) Init(tools []api.Tool, lastMessage *api.Message, thinkValue *api.ThinkValue) []api.Tool {
- p.tools = tools
+ p.toolParser = &Qwen3CoderParser{}
+ p.toolParser.Init(tools, nil, nil)
- // thinking is enabled if user requests it
thinkingEnabled := thinkValue != nil && thinkValue.Bool()
-
prefill := lastMessage != nil && lastMessage.Role == "assistant"
- if !thinkingEnabled {
- p.state = Nemotron3NanoCollectingContent
- return tools
- }
-
- if prefill && lastMessage.Content != "" {
+ if !thinkingEnabled || (prefill && lastMessage.Content != "") {
p.state = Nemotron3NanoCollectingContent
- return tools
+ } else {
+ p.state = Nemotron3NanoCollectingThinking
}
- p.state = Nemotron3NanoCollectingThinking
return tools
}
-type nemotronEvent interface {
- isNemotronEvent()
-}
-
-type nemotronEventThinkingContent struct {
- content string
-}
-
-type nemotronEventContent struct {
- content string
-}
-
-type nemotronEventToolCall struct {
- toolCall api.ToolCall
-}
-
-func (nemotronEventThinkingContent) isNemotronEvent() {}
-func (nemotronEventContent) isNemotronEvent() {}
-func (nemotronEventToolCall) isNemotronEvent() {}
-
func (p *Nemotron3NanoParser) Add(s string, done bool) (content string, thinking string, calls []api.ToolCall, err error) {
- p.buffer.WriteString(s)
- events := p.parseEvents()
-
- var toolCalls []api.ToolCall
- var contentSb strings.Builder
- var thinkingSb strings.Builder
- for _, event := range events {
- switch event := event.(type) {
- case nemotronEventToolCall:
- toolCalls = append(toolCalls, event.toolCall)
- case nemotronEventThinkingContent:
- thinkingSb.WriteString(event.content)
- case nemotronEventContent:
- contentSb.WriteString(event.content)
- }
+ if p.state == Nemotron3NanoCollectingContent {
+ return p.toolParser.Add(s, done)
}
- return contentSb.String(), thinkingSb.String(), toolCalls, nil
-}
-
-func (p *Nemotron3NanoParser) parseEvents() []nemotronEvent {
- var all []nemotronEvent
-
- keepLooping := true
- for keepLooping {
- var events []nemotronEvent
- events, keepLooping = p.eat()
- if len(events) > 0 {
- all = append(all, events...)
+ if p.state == Nemotron3NanoSkipWhitespaceAfterThinking {
+ s = strings.TrimLeftFunc(s, unicode.IsSpace)
+ if s == "" {
+ return "", "", nil, nil
}
+ p.state = Nemotron3NanoCollectingContent
+ return p.toolParser.Add(s, done)
}
- return all
-}
-
-// emitWithPartialCheck extracts unambiguous content before a potential partial tag
-func (p *Nemotron3NanoParser) emitWithPartialCheck(bufStr, tag string) (unambiguous, ambiguous string) {
- if overlapLen := overlap(bufStr, tag); overlapLen > 0 {
- beforePartialTag := bufStr[:len(bufStr)-overlapLen]
- trailingLen := trailingWhitespaceLen(beforePartialTag)
- return bufStr[:len(beforePartialTag)-trailingLen], bufStr[len(beforePartialTag)-trailingLen:]
- }
- wsLen := trailingWhitespaceLen(bufStr)
- return bufStr[:len(bufStr)-wsLen], bufStr[len(bufStr)-wsLen:]
-}
-
-func (p *Nemotron3NanoParser) eat() ([]nemotronEvent, bool) {
+ // Nemotron3NanoCollectingThinking - buffer and look for end markers
+ p.buffer.WriteString(s)
bufStr := p.buffer.String()
- if bufStr == "" {
- return nil, false
- }
- switch p.state {
- case Nemotron3NanoCollectingThinking:
- if strings.Contains(bufStr, nemotronThinkClose) {
- split := strings.SplitN(bufStr, nemotronThinkClose, 2)
- thinking := strings.TrimRightFunc(split[0], unicode.IsSpace)
- p.buffer.Reset()
- remainder := strings.TrimLeftFunc(split[1], unicode.IsSpace)
- p.buffer.WriteString(remainder)
- // Transition to whitespace-skipping state if buffer is empty,
- // otherwise go directly to content collection
- if remainder == "" {
- p.state = Nemotron3NanoSkipWhitespaceAfterThinking
- } else {
- p.state = Nemotron3NanoCollectingContent
- }
- if thinking != "" {
- return []nemotronEvent{nemotronEventThinkingContent{content: thinking}}, true
- }
- return nil, true
- }
- unambig, ambig := p.emitWithPartialCheck(bufStr, nemotronThinkClose)
- p.buffer.Reset()
- p.buffer.WriteString(ambig)
- if unambig != "" {
- return []nemotronEvent{nemotronEventThinkingContent{content: unambig}}, false
- }
- return nil, false
+ // Look for end of thinking: </think> or <tool_call> (model may skip </think>)
+ thinkIdx := strings.Index(bufStr, nemotronThinkClose)
+ toolIdx := strings.Index(bufStr, nemotronToolCallOpen)
- // We only want to skip whitespace between thinking and content
- case Nemotron3NanoSkipWhitespaceAfterThinking:
- bufStr = strings.TrimLeftFunc(bufStr, unicode.IsSpace)
- p.buffer.Reset()
- p.buffer.WriteString(bufStr)
- if bufStr == "" {
- return nil, false
- }
- p.state = Nemotron3NanoCollectingContent
- return nil, true
-
- case Nemotron3NanoCollectingContent:
- if strings.Contains(bufStr, nemotronToolCallOpen) {
- split := strings.SplitN(bufStr, nemotronToolCallOpen, 2)
- content := strings.TrimRightFunc(split[0], unicode.IsSpace)
- p.buffer.Reset()
- p.buffer.WriteString(split[1])
- p.state = Nemotron3NanoCollectingToolCalls
- if content != "" {
- return []nemotronEvent{nemotronEventContent{content: content}}, true
- }
- return nil, true
- }
- unambig, ambig := p.emitWithPartialCheck(bufStr, nemotronToolCallOpen)
- p.buffer.Reset()
- p.buffer.WriteString(ambig)
- if unambig != "" {
- return []nemotronEvent{nemotronEventContent{content: unambig}}, false
- }
- return nil, false
+ var endIdx int = -1
+ var remainder string
- case Nemotron3NanoCollectingToolCalls:
- if strings.Contains(bufStr, nemotronToolCallClose) {
- split := strings.SplitN(bufStr, nemotronToolCallClose, 2)
- remaining := strings.TrimLeftFunc(split[1], unicode.IsSpace)
- p.buffer.Reset()
- p.buffer.WriteString(remaining)
+ if thinkIdx != -1 && (toolIdx == -1 || thinkIdx < toolIdx) {
+ endIdx = thinkIdx
+ remainder = strings.TrimLeftFunc(bufStr[thinkIdx+len(nemotronThinkClose):], unicode.IsSpace)
+ } else if toolIdx != -1 {
+ endIdx = toolIdx
+ remainder = bufStr[toolIdx:] // Include <tool_call> tag
+ }
- var events []nemotronEvent
- if tc, err := p.parseToolCall(split[0]); err == nil {
- events = append(events, nemotronEventToolCall{toolCall: tc})
- }
+ if endIdx != -1 {
+ thinking = strings.TrimRightFunc(bufStr[:endIdx], unicode.IsSpace)
+ p.buffer.Reset()
- if !strings.Contains(remaining, nemotronToolCallOpen) {
- p.state = Nemotron3NanoCollectingContent
- }
- return events, true
+ if remainder == "" {
+ p.state = Nemotron3NanoSkipWhitespaceAfterThinking
+ } else {
+ p.state = Nemotron3NanoCollectingContent
+ content, _, calls, err = p.toolParser.Add(remainder, done)
}
- return nil, false
+ return content, thinking, calls, err
}
- return nil, false
+ // No end marker - emit unambiguous thinking
+ thinking = p.emitThinking(bufStr)
+ return "", thinking, nil, nil
}
-var (
- nemotronFunctionRegex = regexp.MustCompile(`<function=([^>]+)>`)
- nemotronParameterRegex = regexp.MustCompile(`<parameter=([^>]+)>\n?([\s\S]*?)\n?</parameter>`)
-)
-
-func (p *Nemotron3NanoParser) parseToolCall(content string) (api.ToolCall, error) {
- toolCall := api.ToolCall{}
+// emitThinking returns unambiguous thinking content, keeping potential partial tags in buffer
+func (p *Nemotron3NanoParser) emitThinking(bufStr string) string {
+ // Check for partial </think> or <tool_call> at end
+ thinkOverlap := overlap(bufStr, nemotronThinkClose)
+ toolOverlap := overlap(bufStr, nemotronToolCallOpen)
+ maxOverlap := max(thinkOverlap, toolOverlap)
- // Extract function name
- fnMatch := nemotronFunctionRegex.FindStringSubmatch(content)
- if len(fnMatch) < 2 {
- return toolCall, nil
- }
- toolCall.Function.Name = fnMatch[1]
-
- // Extract parameters
- toolCall.Function.Arguments = api.NewToolCallFunctionArguments()
- paramMatches := nemotronParameterRegex.FindAllStringSubmatch(content, -1)
- for _, match := range paramMatches {
- if len(match) >= 3 {
- paramName := match[1]
- paramValue := strings.TrimSpace(match[2])
-
- // Try to parse as typed value based on tool definition
- toolCall.Function.Arguments.Set(paramName, p.parseParamValue(paramName, paramValue))
- }
+ if maxOverlap > 0 {
+ unambiguous := bufStr[:len(bufStr)-maxOverlap]
+ unambiguous = strings.TrimRightFunc(unambiguous, unicode.IsSpace)
+ p.buffer.Reset()
+ p.buffer.WriteString(bufStr[len(bufStr)-maxOverlap:])
+ return unambiguous
}
- return toolCall, nil
-}
-
-func (p *Nemotron3NanoParser) parseParamValue(paramName string, raw string) any {
- // Find the matching tool to get parameter type
- var paramType api.PropertyType
- for _, tool := range p.tools {
- if tool.Function.Parameters.Properties != nil {
- if prop, ok := tool.Function.Parameters.Properties.Get(paramName); ok {
- paramType = prop.Type
- break
- }
- }
+ // No partial tags - emit all but trailing whitespace
+ wsLen := trailingWhitespaceLen(bufStr)
+ if wsLen > 0 {
+ unambiguous := bufStr[:len(bufStr)-wsLen]
+ p.buffer.Reset()
+ p.buffer.WriteString(bufStr[len(bufStr)-wsLen:])
+ return unambiguous
}
- return parseValue(raw, paramType)
+ // Nothing to hold back
+ p.buffer.Reset()
+ return bufStr
}
diff --git a/model/parsers/nemotron3nano_test.go b/model/parsers/nemotron3nano_test.go
index 408a31e85..2ba36a156 100644
--- a/model/parsers/nemotron3nano_test.go
+++ b/model/parsers/nemotron3nano_test.go
@@ -8,6 +8,8 @@ import (
"github.com/ollama/ollama/api"
)
+// TestNemotron3NanoParser tests Nemotron-specific behavior (thinking support).
+// Tool call parsing is tested in qwen3coder_test.go since Nemotron delegates to Qwen3CoderParser.
func TestNemotron3NanoParser(t *testing.T) {
tests := []struct {
name string
@@ -17,18 +19,6 @@ func TestNemotron3NanoParser(t *testing.T) {
expectedThinking string
expectedCalls []api.ToolCall
}{
- {
- name: "simple content - no thinking",
- input: "Hello, how can I help you?",
- thinkValue: nil,
- expectedContent: "Hello, how can I help you?",
- },
- {
- name: "simple content - thinking disabled",
- input: "Hello, how can I help you?",
- thinkValue: &api.ThinkValue{Value: false},
- expectedContent: "Hello, how can I help you?",
- },
{
name: "thinking then content",
input: "Let me think about this...</think>\nHere is my answer.",
@@ -43,69 +33,6 @@ func TestNemotron3NanoParser(t *testing.T) {
expectedThinking: "Step 1: Analyze\nStep 2: Process\nStep 3: Conclude",
expectedContent: "The answer is 42.",
},
- {
- name: "simple tool call",
- input: "<tool_call>\n<function=get_weather>\n<parameter=city>\nParis\n</parameter>\n</function>\n</tool_call>",
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "Paris"}),
- },
- },
- },
- },
- {
- name: "content then tool call",
- input: "Let me check the weather.\n<tool_call>\n<function=get_weather>\n<parameter=city>\nNYC\n</parameter>\n</function>\n</tool_call>",
- thinkValue: nil,
- expectedContent: "Let me check the weather.",
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "NYC"}),
- },
- },
- },
- },
- {
- name: "tool call with multiple parameters",
- input: "<tool_call>\n<function=book_flight>\n<parameter=from>\nSFO\n</parameter>\n<parameter=to>\nNYC\n</parameter>\n</function>\n</tool_call>",
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "book_flight",
- Arguments: testArgs(map[string]any{
- "from": "SFO",
- "to": "NYC",
- }),
- },
- },
- },
- },
- {
- name: "multiple tool calls",
- input: "<tool_call>\n<function=get_weather>\n<parameter=city>\nSan Francisco\n</parameter>\n</function>\n</tool_call>\n" +
- "<tool_call>\n<function=get_weather>\n<parameter=city>\nNew York\n</parameter>\n</function>\n</tool_call>",
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "San Francisco"}),
- },
- },
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "New York"}),
- },
- },
- },
- },
{
name: "thinking then tool call",
input: "I should check the weather...</think>\n<tool_call>\n<function=get_weather>\n<parameter=city>\nParis\n</parameter>\n</function>\n</tool_call>",
@@ -135,19 +62,6 @@ func TestNemotron3NanoParser(t *testing.T) {
},
},
},
- {
- name: "tool call with multiline parameter value",
- input: "<tool_call>\n<function=create_note>\n<parameter=content>\nLine 1\nLine 2\nLine 3\n</parameter>\n</function>\n</tool_call>",
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "create_note",
- Arguments: testArgs(map[string]any{"content": "Line 1\nLine 2\nLine 3"}),
- },
- },
- },
- },
{
name: "empty thinking block - immediate close",
input: "</think>\nHere is my answer.",
@@ -161,18 +75,6 @@ func TestNemotron3NanoParser(t *testing.T) {
thinkValue: &api.ThinkValue{Value: false},
expectedContent: "</think>\nSome content after spurious tag.",
},
- {
- name: "tool call with no function name - returns empty tool call",
- input: "<tool_call>\n<function=>\n</function>\n</tool_call>",
- thinkValue: nil,
- expectedCalls: []api.ToolCall{{Function: api.ToolCallFunction{Name: "", Arguments: api.NewToolCallFunctionArguments()}}},
- },
- {
- name: "content with newlines preserved",
- input: "Line 1\n\nLine 2\n\n\nLine 3",
- thinkValue: nil,
- expectedContent: "Line 1\n\nLine 2\n\n\nLine 3",
- },
{
name: "thinking with only whitespace after close tag",
input: "My thoughts...</think> \n\t\n Content here.",
@@ -180,25 +82,6 @@ func TestNemotron3NanoParser(t *testing.T) {
expectedThinking: "My thoughts...",
expectedContent: "Content here.",
},
- {
- name: "unicode content",
- input: "Hello 世界! 🌍 Ñoño",
- thinkValue: nil,
- expectedContent: "Hello 世界! 🌍 Ñoño",
- },
- {
- name: "tool call with numeric parameter",
- input: "<tool_call>\n<function=set_temp>\n<parameter=value>\n42\n</parameter>\n</function>\n</tool_call>",
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "set_temp",
- Arguments: testArgs(map[string]any{"value": "42"}),
- },
- },
- },
- },
}
for _, tt := range tests {
@@ -233,6 +116,8 @@ func TestNemotron3NanoParser(t *testing.T) {
}
}
+// TestNemotron3NanoParser_Streaming tests streaming behavior for thinking support.
+// Tool call streaming is tested in qwen3coder_test.go.
func TestNemotron3NanoParser_Streaming(t *testing.T) {
tests := []struct {
name string
@@ -242,18 +127,6 @@ func TestNemotron3NanoParser_Streaming(t *testing.T) {
expectedThinking string
expectedCalls []api.ToolCall
}{
- {
- name: "streaming content character by character",
- chunks: []string{"H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"},
- thinkValue: nil,
- expectedContent: "Hello, world!",
- },
- {
- name: "streaming content small tokens",
- chunks: []string{"Hel", "lo", ", ", "how ", "can", " I", " help", " you", " today", "?"},
- thinkValue: nil,
- expectedContent: "Hello, how can I help you today?",
- },
{
name: "streaming thinking then content - granular",
chunks: []string{"Let", " me", " th", "ink", " about", " this", "...", "<", "/", "think", ">", "\n", "Here", " is", " my", " answer", "."},
@@ -268,45 +141,6 @@ func TestNemotron3NanoParser_Streaming(t *testing.T) {
expectedThinking: "Step 1: Analyze\nStep 2: Process",
expectedContent: "The answer.",
},
- {
- name: "streaming tool call - highly granular",
- chunks: []string{"<", "tool", "_", "call", ">", "\n", "<", "func", "tion", "=", "get", "_", "weather", ">", "\n", "<", "param", "eter", "=", "city", ">", "\n", "Par", "is", "\n", "</", "param", "eter", ">", "\n", "</", "func", "tion", ">", "\n", "</", "tool", "_", "call", ">"},
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "Paris"}),
- },
- },
- },
- },
- {
- name: "streaming content then tool call - granular",
- chunks: []string{"Let", " me", " check", " the", " weather", ".", "\n<", "tool_call", ">", "\n", "<function=", "get_weather", ">", "\n", "<parameter=", "city", ">", "\n", "NYC", "\n", "</parameter>", "\n", "</function>", "\n", "</tool_call>"},
- thinkValue: nil,
- expectedContent: "Let me check the weather.",
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "NYC"}),
- },
- },
- },
- },
- {
- name: "tool call tag split character by character",
- chunks: []string{"<", "t", "o", "o", "l", "_", "c", "a", "l", "l", ">", "\n", "<", "f", "u", "n", "c", "t", "i", "o", "n", "=", "t", "e", "s", "t", ">", "\n", "<", "/", "f", "u", "n", "c", "t", "i", "o", "n", ">", "\n", "<", "/", "t", "o", "o", "l", "_", "c", "a", "l", "l", ">"},
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "test",
- Arguments: api.NewToolCallFunctionArguments(),
- },
- },
- },
- },
{
name: "thinking close tag split character by character",
chunks: []string{"I", "'", "m", " ", "t", "h", "i", "n", "k", "i", "n", "g", ".", ".", ".", "<", "/", "t", "h", "i", "n", "k", ">", "\n", "D", "o", "n", "e", "!"},
@@ -321,22 +155,6 @@ func TestNemotron3NanoParser_Streaming(t *testing.T) {
expectedThinking: "Thinking...",
expectedContent: "Content here.",
},
- {
- name: "tool call with multiple parameters - streaming",
- chunks: []string{"<tool_", "call>\n", "<function", "=book_", "flight>", "\n<para", "meter=", "from>\n", "SFO\n", "</param", "eter>", "\n<param", "eter=to", ">\nNYC", "\n</para", "meter>", "\n</func", "tion>\n", "</tool_", "call>"},
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "book_flight",
- Arguments: testArgs(map[string]any{
- "from": "SFO",
- "to": "NYC",
- }),
- },
- },
- },
- },
{
name: "thinking then content then tool call - streaming",
chunks: []string{"Ana", "lyzing", " your", " request", "...", "</", "think", ">\n", "I'll", " check", " that", " for", " you", ".", "\n", "<tool", "_call", ">\n", "<function", "=search", ">\n", "<parameter", "=query", ">\n", "test", " query", "\n</", "parameter", ">\n", "</function", ">\n", "</tool", "_call", ">"},
@@ -352,45 +170,6 @@ func TestNemotron3NanoParser_Streaming(t *testing.T) {
},
},
},
- {
- name: "multiple tool calls - streaming",
- chunks: []string{
- "<tool_call>", "\n", "<function=", "get_weather>", "\n",
- "<parameter=", "city>\n", "San Fran", "cisco\n", "</parameter>", "\n",
- "</function>", "\n", "</tool_call>", "\n",
- "<tool_", "call>\n", "<function", "=get_weather", ">\n",
- "<param", "eter=city", ">\nNew", " York\n", "</parameter>\n",
- "</function>\n", "</tool_call>",
- },
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "San Francisco"}),
- },
- },
- {
- Function: api.ToolCallFunction{
- Name: "get_weather",
- Arguments: testArgs(map[string]any{"city": "New York"}),
- },
- },
- },
- },
- {
- name: "tool call with multiline parameter - streaming",
- chunks: []string{"<tool_call>\n", "<function=", "create_note>\n", "<parameter=", "content>\n", "Line 1", "\nLine", " 2\n", "Line 3", "\n</parameter>\n", "</function>\n", "</tool_call>"},
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "create_note",
- Arguments: testArgs(map[string]any{"content": "Line 1\nLine 2\nLine 3"}),
- },
- },
- },
- },
{
name: "empty thinking block",
chunks: []string{"</think>", "\n", "Just content."},
@@ -398,12 +177,6 @@ func TestNemotron3NanoParser_Streaming(t *testing.T) {
expectedThinking: "",
expectedContent: "Just content.",
},
- {
- name: "empty input chunks interspersed",
- chunks: []string{"Hello", "", " ", "", "world", "", "!"},
- thinkValue: nil,
- expectedContent: "Hello world!",
- },
{
name: "tool call immediately after think close - no content",
chunks: []string{"Analyzing...", "</think>", "\n", "<tool_call>", "\n<function=test>\n</function>\n", "</tool_call>"},
@@ -418,25 +191,6 @@ func TestNemotron3NanoParser_Streaming(t *testing.T) {
},
},
},
- {
- name: "tool call with empty parameter value",
- chunks: []string{"<tool_call>\n<function=test>\n<parameter=name>\n", "\n</parameter>\n</function>\n</tool_call>"},
- thinkValue: nil,
- expectedCalls: []api.ToolCall{
- {
- Function: api.ToolCallFunction{
- Name: "test",
- Arguments: testArgs(map[string]any{"name": ""}),
- },
- },
- },
- },
- {
- name: "partial tool call tag at end - buffered",
- chunks: []string{"Here's some content", "<tool"},
- thinkValue: nil,
- expectedContent: "Here's some content",
- },
}
for _, tt := range tests {
@@ -572,3 +326,65 @@ func TestNemotron3NanoParser_WithTools(t *testing.T) {
t.Errorf("calls mismatch (-got +want):\n%s", diff)
}
}
+
+// TestNemotron3NanoParser_ToolCallWithoutThinkClose tests the case where thinking is enabled
+// but the model outputs content + tool call WITHOUT the </think> tag.
+// The parser should still parse the tool call (content before is treated as thinking).
+func TestNemotron3NanoParser_ToolCallWithoutThinkClose(t *testing.T) {
+ chunks := []string{
+ "Let", " me", " analyze", " this", ".", "\n",
+ "<tool_call>", "\n",
+ "<function=get_weather>", "\n",
+ "<parameter=city>", "Paris", "</parameter>", "\n",
+ "</function>", "\n",
+ "</tool_call>",
+ }
+
+ p := &Nemotron3NanoParser{}
+ p.Init(nil, nil, &api.ThinkValue{Value: true}) // thinking ENABLED but model doesn't output </think>
+
+ var allContent string
+ var allThinking string
+ var allCalls []api.ToolCall
+
+ for _, chunk := range chunks {
+ content, thinking, calls, err := p.Add(chunk, false)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ allContent += content
+ allThinking += thinking
+ allCalls = append(allCalls, calls...)
+ }
+
+ // Drain
+ content, thinking, calls, err := p.Add("", true)
+ if err != nil {
+ t.Fatalf("unexpected error on done: %v", err)
+ }
+ allContent += content
+ allThinking += thinking
+ allCalls = append(allCalls, calls...)
+
+ // The parser was in thinking mode, so text before <tool_call> is emitted as thinking.
+ expectedThinking := "Let me analyze this."
+
+ expectedCalls := []api.ToolCall{
+ {
+ Function: api.ToolCallFunction{
+ Name: "get_weather",
+ Arguments: testArgs(map[string]any{"city": "Paris"}),
+ },
+ },
+ }
+
+ if allContent != "" {
+ t.Errorf("expected no content (text was streamed as thinking), got: %q", allContent)
+ }
+ if diff := cmp.Diff(allThinking, expectedThinking); diff != "" {
+ t.Errorf("thinking mismatch (-got +want):\n%s", diff)
+ }
+ if diff := cmp.Diff(allCalls, expectedCalls, argsComparer); diff != "" {
+ t.Errorf("calls mismatch (-got +want):\n%s", diff)
+ }
+}
diff --git a/model/parsers/qwen3coder_test.go b/model/parsers/qwen3coder_test.go
index 01c39924b..24ccc5e9e 100644
--- a/model/parsers/qwen3coder_test.go
+++ b/model/parsers/qwen3coder_test.go
@@ -91,6 +91,37 @@ func TestQwenParserStreaming(t *testing.T) {
},
},
},
+ {
+ desc: "tool call tags split character by character",
+ steps: []step{
+ {input: "<", wantEvents: []qwenEvent{}},
+ {input: "t", wantEvents: []qwenEvent{}},
+ {input: "o", wantEvents: []qwenEvent{}},
+ {input: "o", wantEvents: []qwenEvent{}},
+ {input: "l", wantEvents: []qwenEvent{}},
+ {input: "_", wantEvents: []qwenEvent{}},
+ {input: "c", wantEvents: []qwenEvent{}},
+ {input: "a", wantEvents: []qwenEvent{}},
+ {input: "l", wantEvents: []qwenEvent{}},
+ {input: "l", wantEvents: []qwenEvent{}},
+ {input: ">", wantEvents: []qwenEvent{}},
+ {input: "a", wantEvents: []qwenEvent{}},
+ {input: "b", wantEvents: []qwenEvent{}},
+ {input: "c", wantEvents: []qwenEvent{}},
+ {input: "<", wantEvents: []qwenEvent{}},
+ {input: "/", wantEvents: []qwenEvent{}},
+ {input: "t", wantEvents: []qwenEvent{}},
+ {input: "o", wantEvents: []qwenEvent{}},
+ {input: "o", wantEvents: []qwenEvent{}},
+ {input: "l", wantEvents: []qwenEvent{}},
+ {input: "_", wantEvents: []qwenEvent{}},
+ {input: "c", wantEvents: []qwenEvent{}},
+ {input: "a", wantEvents: []qwenEvent{}},
+ {input: "l", wantEvents: []qwenEvent{}},
+ {input: "l", wantEvents: []qwenEvent{}},
+ {input: ">", wantEvents: []qwenEvent{qwenEventRawToolCall{raw: "abc"}}},
+ },
+ },
{
desc: "trailing whitespace between content and tool call",
steps: []step{