fix: use Responses-compatible function tool_choice format

This commit is contained in:
ivanvolt
2026-04-28 16:26:09 +08:00
parent b0a2252ed1
commit 04b2866f65
7 changed files with 150 additions and 16 deletions
@@ -991,9 +991,40 @@ func TestAnthropicToResponses_ToolChoiceSpecific(t *testing.T) {
var tc map[string]any
require.NoError(t, json.Unmarshal(resp.ToolChoice, &tc))
assert.Equal(t, "function", tc["type"])
fn, ok := tc["function"].(map[string]any)
require.True(t, ok)
assert.Equal(t, "get_weather", fn["name"])
assert.Equal(t, "get_weather", tc["name"])
assert.NotContains(t, tc, "function")
}
func TestResponsesToAnthropicRequest_ToolChoiceFunctionName(t *testing.T) {
req := &ResponsesRequest{
Model: "gpt-5.2",
Input: json.RawMessage(`[{"role":"user","content":"Hello"}]`),
ToolChoice: json.RawMessage(`{"type":"function","name":"get_weather"}`),
}
resp, err := ResponsesToAnthropicRequest(req)
require.NoError(t, err)
var tc map[string]string
require.NoError(t, json.Unmarshal(resp.ToolChoice, &tc))
assert.Equal(t, "tool", tc["type"])
assert.Equal(t, "get_weather", tc["name"])
}
func TestResponsesToAnthropicRequest_ToolChoiceLegacyFunctionName(t *testing.T) {
req := &ResponsesRequest{
Model: "gpt-5.2",
Input: json.RawMessage(`[{"role":"user","content":"Hello"}]`),
ToolChoice: json.RawMessage(`{"type":"function","function":{"name":"get_weather"}}`),
}
resp, err := ResponsesToAnthropicRequest(req)
require.NoError(t, err)
var tc map[string]string
require.NoError(t, json.Unmarshal(resp.ToolChoice, &tc))
assert.Equal(t, "tool", tc["type"])
assert.Equal(t, "get_weather", tc["name"])
}
// ---------------------------------------------------------------------------