fix(gemini): 使用 JSON Schema 传递工具参数

This commit is contained in:
wangbo 2026-07-14 12:55:44 +08:00
parent 82c8dd352c
commit 30ad0e9f2c
2 changed files with 31 additions and 2 deletions

View File

@ -1406,7 +1406,24 @@ func TestGeminiClientChatRestoresToolContext(t *testing.T) {
"function": map[string]any{
"name": "get_weather",
"description": "lookup weather",
"parameters": map[string]any{"type": "object"},
"parameters": map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]any{
"anyOf": []any{
map[string]any{"type": "string"},
map[string]any{
"type": "object",
"properties": map[string]any{
"city": map[string]any{"type": "string"},
},
"required": []any{"city"},
},
},
},
},
"required": []any{"location"},
},
},
}},
},
@ -1450,6 +1467,18 @@ func TestGeminiClientChatRestoresToolContext(t *testing.T) {
if declaration["name"] != "get_weather" || declaration["description"] != "lookup weather" {
t.Fatalf("tool declaration was not converted for Gemini: %+v", captured["tools"])
}
if _, exists := declaration["parameters"]; exists {
t.Fatalf("Gemini tool declaration must not use OpenAPI parameters: %+v", declaration)
}
parameters, _ := declaration["parametersJsonSchema"].(map[string]any)
properties, _ := parameters["properties"].(map[string]any)
location, _ := properties["location"].(map[string]any)
anyOf, _ := location["anyOf"].([]any)
nestedObject, _ := anyOf[1].(map[string]any)
required, _ := nestedObject["required"].([]any)
if len(anyOf) != 2 || len(required) != 1 || required[0] != "city" {
t.Fatalf("Gemini JSON Schema was not preserved: %+v", declaration)
}
}
func TestGeminiClientChatConvertsFunctionCallResponse(t *testing.T) {

View File

@ -523,7 +523,7 @@ func geminiToolsFromOpenAITools(value any) []any {
declaration["description"] = description
}
if parameters, ok := function["parameters"]; ok {
declaration["parameters"] = parameters
declaration["parametersJsonSchema"] = parameters
}
declarations = append(declarations, declaration)
}