mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Merge pull request #8409 from unique01082/fix/antigravity-response-json-schema
fix(antigravity): Gemini 原生请求的 responseJsonSchema 改写为 responseSchema
This commit is contained in:
2 files changed
+115
No files matched your search
@@ -522,6 +522,11 @@ func cleanGeminiRequest(body []byte) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 结构化输出 Schema
|
||||
if normalizeGeminiResponseSchema(payload) {
|
||||
modified = true
|
||||
}
|
||||
|
||||
if !modified {
|
||||
return body, nil
|
||||
}
|
||||
@@ -529,6 +534,37 @@ func cleanGeminiRequest(body []byte) ([]byte, error) {
|
||||
return json.Marshal(payload)
|
||||
}
|
||||
|
||||
// normalizeGeminiResponseSchema 将 generationConfig.responseJsonSchema 改写为 responseSchema。
|
||||
// Antigravity 上游接受 responseJsonSchema 字段但静默忽略(返回普通文本而非 JSON),只有 responseSchema 生效;
|
||||
// 而 @ai-sdk/google >= 4.0.70 等客户端只发送 responseJsonSchema。已有 responseSchema 时以其为准。
|
||||
func normalizeGeminiResponseSchema(payload map[string]any) bool {
|
||||
changed := false
|
||||
for _, configKey := range []string{"generationConfig", "generation_config"} {
|
||||
config, ok := payload[configKey].(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, schemaKey := range []string{"responseJsonSchema", "response_json_schema"} {
|
||||
raw, exists := config[schemaKey]
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
delete(config, schemaKey)
|
||||
changed = true
|
||||
_, hasSchema := config["responseSchema"]
|
||||
_, hasSnakeSchema := config["response_schema"]
|
||||
if hasSchema || hasSnakeSchema {
|
||||
continue
|
||||
}
|
||||
if schema, ok := raw.(map[string]any); ok {
|
||||
antigravity.DeepCleanUndefined(schema)
|
||||
config["responseSchema"] = antigravity.CleanJSONSchema(schema)
|
||||
}
|
||||
}
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
// filterEmptyPartsFromGeminiRequest 过滤掉 parts 为空的消息
|
||||
// Gemini API 不接受空 parts,需要在请求前过滤
|
||||
func filterEmptyPartsFromGeminiRequest(body []byte) ([]byte, error) {
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
//go:build unit
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func cleanGeminiRequestMap(t *testing.T, body string) map[string]any {
|
||||
t.Helper()
|
||||
out, err := cleanGeminiRequest([]byte(body))
|
||||
require.NoError(t, err)
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(out, &payload))
|
||||
return payload
|
||||
}
|
||||
|
||||
func TestCleanGeminiRequest_ResponseJsonSchemaBecomesResponseSchema(t *testing.T) {
|
||||
payload := cleanGeminiRequestMap(t, `{
|
||||
"contents": [{"role": "user", "parts": [{"text": "hi"}]}],
|
||||
"generationConfig": {
|
||||
"responseMimeType": "application/json",
|
||||
"responseJsonSchema": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"title": {"type": "string", "maxLength": 40},
|
||||
"mood": {"type": "string", "enum": ["vui", "buồn"]},
|
||||
"beats": {"type": "array", "minItems": 3, "items": {"type": "string"}}
|
||||
},
|
||||
"required": ["title", "mood", "beats"]
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
config := payload["generationConfig"].(map[string]any)
|
||||
require.NotContains(t, config, "responseJsonSchema")
|
||||
require.Equal(t, "application/json", config["responseMimeType"])
|
||||
|
||||
schema := config["responseSchema"].(map[string]any)
|
||||
require.NotContains(t, schema, "$schema")
|
||||
require.NotContains(t, schema, "additionalProperties")
|
||||
require.Equal(t, "object", schema["type"])
|
||||
require.ElementsMatch(t, []any{"title", "mood", "beats"}, schema["required"])
|
||||
|
||||
props := schema["properties"].(map[string]any)
|
||||
require.Equal(t, []any{"vui", "buồn"}, props["mood"].(map[string]any)["enum"])
|
||||
// Antigravity 不支持的约束迁移到 description,模型仍能看到。
|
||||
require.Contains(t, props["beats"].(map[string]any)["description"], "minItems: 3")
|
||||
require.NotContains(t, props["title"].(map[string]any), "maxLength")
|
||||
}
|
||||
|
||||
func TestCleanGeminiRequest_SnakeCaseResponseJsonSchema(t *testing.T) {
|
||||
payload := cleanGeminiRequestMap(t, `{"generation_config": {"response_json_schema": {"type": "object", "properties": {"a": {"type": "string"}}}}}`)
|
||||
config := payload["generation_config"].(map[string]any)
|
||||
require.NotContains(t, config, "response_json_schema")
|
||||
require.Equal(t, "object", config["responseSchema"].(map[string]any)["type"])
|
||||
}
|
||||
|
||||
func TestCleanGeminiRequest_ExistingResponseSchemaWins(t *testing.T) {
|
||||
payload := cleanGeminiRequestMap(t, `{"generationConfig": {
|
||||
"responseSchema": {"type": "STRING"},
|
||||
"responseJsonSchema": {"type": "object", "properties": {"a": {"type": "string"}}}
|
||||
}}`)
|
||||
config := payload["generationConfig"].(map[string]any)
|
||||
require.NotContains(t, config, "responseJsonSchema")
|
||||
require.Equal(t, map[string]any{"type": "STRING"}, config["responseSchema"])
|
||||
}
|
||||
|
||||
func TestCleanGeminiRequest_NoSchemaLeavesBodyUntouched(t *testing.T) {
|
||||
body := `{"generationConfig": {"temperature": 0.5}}`
|
||||
out, err := cleanGeminiRequest([]byte(body))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, body, string(out))
|
||||
}
|
||||
Reference in new issue
Block a user