fix(provider): skip empty anthropic tool names (#1772)
Co-authored-by: Alix-007 <267018309+Alix-007@users.noreply.github.com>
This commit is contained in:
parent
bb59518958
commit
05c65d2fe7
2 changed files with 49 additions and 0 deletions
|
|
@ -221,6 +221,10 @@ func buildRequestBody(
|
||||||
|
|
||||||
// Add tool_use blocks
|
// Add tool_use blocks
|
||||||
for _, tc := range msg.ToolCalls {
|
for _, tc := range msg.ToolCalls {
|
||||||
|
if strings.TrimSpace(tc.Name) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Handle nil Arguments (GLM-4 may return null input)
|
// Handle nil Arguments (GLM-4 may return null input)
|
||||||
input := tc.Arguments
|
input := tc.Arguments
|
||||||
if input == nil {
|
if input == nil {
|
||||||
|
|
|
||||||
|
|
@ -492,6 +492,20 @@ func TestBuildRequestBodyEdgeCases(t *testing.T) {
|
||||||
},
|
},
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "skip tool calls with empty names",
|
||||||
|
messages: []Message{
|
||||||
|
{Role: "assistant", Content: "Calling tool", ToolCalls: []ToolCall{
|
||||||
|
{ID: "tool-empty", Name: "", Arguments: map[string]any{"ignored": true}},
|
||||||
|
{ID: "tool-valid", Name: "test_tool", Arguments: map[string]any{"arg": "value"}},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
model: "test-model",
|
||||||
|
options: map[string]any{
|
||||||
|
"max_tokens": 8192,
|
||||||
|
},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
@ -513,6 +527,37 @@ func TestBuildRequestBodyEdgeCases(t *testing.T) {
|
||||||
if got["model"] != tt.model {
|
if got["model"] != tt.model {
|
||||||
t.Errorf("model = %v, want %v", got["model"], tt.model)
|
t.Errorf("model = %v, want %v", got["model"], tt.model)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tt.name == "skip tool calls with empty names" {
|
||||||
|
messages, ok := got["messages"].([]any)
|
||||||
|
if !ok || len(messages) != 1 {
|
||||||
|
t.Fatalf("messages = %#v, want single assistant message", got["messages"])
|
||||||
|
}
|
||||||
|
|
||||||
|
assistantMsg, ok := messages[0].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("assistant message = %#v, want map", messages[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
content, ok := assistantMsg["content"].([]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("assistant content = %#v, want []any", assistantMsg["content"])
|
||||||
|
}
|
||||||
|
if len(content) != 2 {
|
||||||
|
t.Fatalf("assistant content length = %d, want 2", len(content))
|
||||||
|
}
|
||||||
|
|
||||||
|
toolUse, ok := content[1].(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("tool_use block = %#v, want map", content[1])
|
||||||
|
}
|
||||||
|
if gotName := toolUse["name"]; gotName != "test_tool" {
|
||||||
|
t.Fatalf("tool_use name = %v, want %q", gotName, "test_tool")
|
||||||
|
}
|
||||||
|
if gotID := toolUse["id"]; gotID != "tool-valid" {
|
||||||
|
t.Fatalf("tool_use id = %v, want %q", gotID, "tool-valid")
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue