qwen3coder: preserve literal function closing tags in parameters (#19228)

This commit is contained in:
Eva H authored and GitHub committed 2026-10-23 10:13:26 -07:00
1 parent f52b9e742d
commit 64a901e713
2 files changed
+57 -5

No files matched your search

+15 -1
View File
@@ -439,11 +439,25 @@ func transformToXML(raw string) string {
// xml tags we just emitted
var out strings.Builder
lastIdx := 0
parameterDepth := 0
for _, loc := range qwenXMLTagRegex.FindAllStringIndex(transformed, -1) {
if loc[0] > lastIdx {
escapeTextNode(&out, transformed[lastIdx:loc[0]])
}
out.WriteString(transformed[loc[0]:loc[1]])
tag := transformed[loc[0]:loc[1]]
switch {
case strings.HasPrefix(tag, "<parameter"):
parameterDepth++
case tag == "</parameter>" && parameterDepth > 0:
parameterDepth--
}
// Only a function-level close ends the call. A parameter can contain
// this literal text, but still needs its own closing tag.
if tag == "</function>" && parameterDepth > 0 {
escapeTextNode(&out, tag)
} else {
out.WriteString(tag)
}
lastIdx = loc[1]
}
if lastIdx < len(transformed) {
+42 -4
View File
@@ -386,6 +386,17 @@ func TestQwenToolParser(t *testing.T) {
}
steps := []step{
{
name: "literal function close in string parameter",
tools: []api.Tool{tool("write", map[string]api.ToolProperty{
"content": {Type: api.PropertyType{"string"}},
})},
rawToolCall: `<function=write><parameter=content>text = "</function>"</parameter></function>`,
wantToolCall: api.ToolCall{Function: api.ToolCallFunction{
Name: "write",
Arguments: testArgs(map[string]any{"content": `text = "</function>"`}),
}},
},
{
name: "simple tool call",
tools: []api.Tool{},
@@ -1113,25 +1124,30 @@ func TestQwen3CoderParserDoneFlushesBufferedContent(t *testing.T) {
func TestQwen3CoderParserToolCallIndexingStreaming(t *testing.T) {
parser := Qwen3CoderParser{}
parser.Init(nil, nil, nil)
parser.Init([]api.Tool{tool("write", map[string]api.ToolProperty{
"content": {Type: api.PropertyType{"string"}},
})}, nil, nil)
var all []api.ToolCall
_, _, calls, err := parser.Add("<tool_call><function=first><parameter=a>1</parameter></function></tool_call><tool_call><function=second>", false)
content, _, calls, err := parser.Add(`Before<tool_call><function=first><parameter=a>1</parameter></function></tool_call>Between<tool_call><function=write><parameter=content>text = "</fun`, false)
if err != nil {
t.Fatalf("step 1 parse failed: %v", err)
}
all = append(all, calls...)
_, _, calls, err = parser.Add("<parameter=b>2</parameter></function></tool_call><tool_call><function=third><parameter=c>3</parameter></function></tool_call>", true)
c, _, calls, err := parser.Add(`ction>"</parameter></function></tool_call>After<tool_call><function=third><parameter=c>3</parameter></function></tool_call>`, true)
if err != nil {
t.Fatalf("step 2 parse failed: %v", err)
}
all = append(all, calls...)
if content+c != "BeforeBetweenAfter" {
t.Fatalf("unexpected content: %q", content+c)
}
want := []api.ToolCall{
{Function: api.ToolCallFunction{Name: "first", Arguments: testArgs(map[string]any{"a": "1"}), Index: 0}},
{Function: api.ToolCallFunction{Name: "second", Arguments: testArgs(map[string]any{"b": "2"}), Index: 1}},
{Function: api.ToolCallFunction{Name: "write", Arguments: testArgs(map[string]any{"content": `text = "</function>"`}), Index: 1}},
{Function: api.ToolCallFunction{Name: "third", Arguments: testArgs(map[string]any{"c": "3"}), Index: 2}},
}
if len(all) != len(want) {
@@ -1170,6 +1186,28 @@ func TestQwen3CoderParserToolCallIndexResetOnInit(t *testing.T) {
}
}
func TestQwen3CoderParserRejectsMalformedParameters(t *testing.T) {
for _, raw := range []string{
`<function=write><parameter=content>print('ok')</function>`,
`<function=write><parameter=content>print('ok')</parameter=content></function>`,
`<function=write><parameter=content>text = "</function>"</parameter>`,
// Reduced shape from #18563's follow-up: the outer edits parameter is unclosed.
`<function=edit>
<parameter=path>src/orders.py</parameter>
<parameter=edits>
<parameter=oldText>before</parameter>
<parameter=newText>after</parameter>
</function>`,
} {
parser := Qwen3CoderParser{}
parser.Init(nil, nil, nil)
_, _, calls, err := parser.Add(toolOpenTag+raw+toolCloseTag, true)
if err == nil || len(calls) != 0 {
t.Errorf("raw=%q: expected rejection without tool calls, got calls=%#v, err=%v", raw, calls, err)
}
}
}
func TestQwenXMLTransform(t *testing.T) {
cases := []struct {
desc string