Files
sub2api/backend/internal/service/gemini_compat_response_format_test.go
T
feeeei 5846bfbbfa fix(gemini): 兼容入口 chat 请求转发 response_format 结构化输出
/v1/chat/completions 走 Gemini 账号时,response_format 在 chat→Responses→Anthropic
转换中丢失,上游只能靠 prompt 约束 JSON 格式。现在按客户端原始 response_format 写
generationConfig:json_object 写 responseMimeType=application/json;json_schema 另把
json_schema.schema 原样写入 responseJsonSchema。请求带 tools 或上游模型为图片输出
模型时不写。

Code Assist(OAuth + project_id)包装改为原字节嵌入 request,避免 Unmarshal→Marshal
重排 schema 属性顺序、截断大整数。
2026-11-11 10:17:12 +08:00

169 lines
9.8 KiB
Go

package service
import (
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"strings"
"testing"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)
const geminiResponseFormatTestSchema = `{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"score":{"type":"integer","minimum":1,"maximum":10},"note":{"anyOf":[{"type":"string","maxLength":600},{"type":"null"}]}},"required":["score","note"],"additionalProperties":false}`
func TestApplyGeminiChatResponseFormat(t *testing.T) {
base := []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}],"generationConfig":{"maxOutputTokens":32000,"thinkingConfig":{"thinkingLevel":"high"}}}`)
withTools := []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}],"tools":[{"functionDeclarations":[{"name":"lookup"}]}],"generationConfig":{"maxOutputTokens":100}}`)
noConfig := []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`)
tests := []struct {
name string
req []byte
format string
model string
wantMime bool
wantSchema string
}{
{name: "json_schema writes mime and schema", req: base, format: `{"type":"json_schema","json_schema":{"name":"grade","strict":true,"schema":` + geminiResponseFormatTestSchema + `}}`, wantMime: true, wantSchema: geminiResponseFormatTestSchema},
{name: "json_schema without schema writes mime only", req: base, format: `{"type":"json_schema","json_schema":{"name":"grade"}}`, wantMime: true},
{name: "json_object writes mime only", req: base, format: `{"type":"json_object"}`, wantMime: true},
{name: "json_object creates generationConfig", req: noConfig, format: `{"type":"json_object"}`, wantMime: true},
{name: "text leaves request unchanged", req: base, format: `{"type":"text"}`},
{name: "null leaves request unchanged", req: base, format: `null`},
{name: "empty leaves request unchanged", req: base, format: ``},
{name: "invalid json leaves request unchanged", req: base, format: `{"type":"json_object"`},
{name: "non-object leaves request unchanged", req: base, format: `"json_object"`},
{name: "image model leaves request unchanged", req: base, format: `{"type":"json_object"}`, model: "gemini-2.5-flash-image"},
{name: "unlisted image model leaves request unchanged", req: base, format: `{"type":"json_object"}`, model: "models/gemini-3.5-flash-image-preview"},
{name: "text model writes mime", req: base, format: `{"type":"json_object"}`, model: "gemini-4-argon", wantMime: true},
{name: "tools present leaves request unchanged", req: withTools, format: `{"type":"json_schema","json_schema":{"name":"grade","schema":` + geminiResponseFormatTestSchema + `}}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := applyGeminiChatResponseFormat(tt.req, json.RawMessage(tt.format), tt.model)
require.True(t, json.Valid(got), string(got))
if !tt.wantMime {
require.Equal(t, string(tt.req), string(got))
return
}
require.Equal(t, "application/json", gjson.GetBytes(got, "generationConfig.responseMimeType").String())
schema := gjson.GetBytes(got, "generationConfig.responseJsonSchema")
if tt.wantSchema == "" {
require.False(t, schema.Exists(), string(got))
} else {
require.JSONEq(t, tt.wantSchema, schema.Raw)
}
require.False(t, gjson.GetBytes(got, "generationConfig.responseSchema").Exists(), string(got))
require.Equal(t, gjson.GetBytes(tt.req, "contents").Raw, gjson.GetBytes(got, "contents").Raw)
for _, path := range []string{"generationConfig.maxOutputTokens", "generationConfig.thinkingConfig"} {
require.Equal(t, gjson.GetBytes(tt.req, path).Raw, gjson.GetBytes(got, path).Raw, path)
}
})
}
}
func TestGeminiForwardAsChatCompletions_ForwardsResponseFormat(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name string
stream bool
format string
wantSchema string
}{
{name: "non-stream json_schema", format: `{"type":"json_schema","json_schema":{"name":"grade","strict":true,"schema":` + geminiResponseFormatTestSchema + `}}`, wantSchema: geminiResponseFormatTestSchema},
{name: "stream json_object", stream: true, format: `{"type":"json_object"}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp := &http.Response{StatusCode: http.StatusOK}
if tt.stream {
resp.Header = http.Header{"Content-Type": []string{"text/event-stream"}}
resp.Body = io.NopCloser(strings.NewReader(`data: {"candidates":[{"content":{"role":"model","parts":[{"text":"{\"score\":7,\"note\":null}"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":3,"candidatesTokenCount":5}}` + "\n\n"))
} else {
resp.Header = http.Header{"Content-Type": []string{"application/json"}}
resp.Body = io.NopCloser(strings.NewReader(`{"candidates":[{"content":{"parts":[{"text":"{\"score\":7,\"note\":null}"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":3,"candidatesTokenCount":5}}`))
}
upstream := &queuedHTTPUpstreamStub{responses: []*http.Response{resp}}
svc := &GeminiMessagesCompatService{httpUpstream: upstream, cfg: &config.Config{}}
body := []byte(`{"model":"gemini-4-argon","stream":` + strconv.FormatBool(tt.stream) + `,"reasoning_effort":"high","max_tokens":32000,"response_format":` + tt.format + `,"messages":[{"role":"user","content":"grade this"}]}`)
c, recorder := newAntigravityCompatContext(http.MethodPost, "/v1/chat/completions", body)
_, err := svc.ForwardAsChatCompletions(context.Background(), c, geminiCompatThinkingTestAccount(), body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, recorder.Code)
require.Len(t, upstream.requestBodies, 1)
sent := upstream.requestBodies[0]
require.Equal(t, "application/json", gjson.GetBytes(sent, "generationConfig.responseMimeType").String(), string(sent))
schema := gjson.GetBytes(sent, "generationConfig.responseJsonSchema")
if tt.wantSchema == "" {
require.False(t, schema.Exists(), string(sent))
} else {
require.JSONEq(t, tt.wantSchema, schema.Raw)
}
require.Equal(t, "high", gjson.GetBytes(sent, "generationConfig.thinkingConfig.thinkingLevel").String())
require.Equal(t, int64(32000), gjson.GetBytes(sent, "generationConfig.maxOutputTokens").Int())
if !tt.stream {
require.Equal(t, `{"score":7,"note":null}`, gjson.Get(recorder.Body.String(), "choices.0.message.content").String())
}
})
}
}
func TestGeminiForwardAsChatCompletions_ResponseFormatSkippedWithTools(t *testing.T) {
gin.SetMode(gin.TestMode)
upstream := &queuedHTTPUpstreamStub{responses: []*http.Response{{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(`{"candidates":[{"content":{"parts":[{"text":"ok"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":3,"candidatesTokenCount":1}}`)),
}}}
svc := &GeminiMessagesCompatService{httpUpstream: upstream, cfg: &config.Config{}}
body := []byte(`{"model":"gemini-4-argon","response_format":{"type":"json_object"},"tools":[{"type":"function","function":{"name":"lookup","parameters":{"type":"object","properties":{"q":{"type":"string"}}}}}],"messages":[{"role":"user","content":"hi"}]}`)
c, recorder := newAntigravityCompatContext(http.MethodPost, "/v1/chat/completions", body)
_, err := svc.ForwardAsChatCompletions(context.Background(), c, geminiCompatThinkingTestAccount(), body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, recorder.Code)
require.Len(t, upstream.requestBodies, 1)
sent := upstream.requestBodies[0]
require.True(t, gjson.GetBytes(sent, "tools").IsArray(), string(sent))
require.False(t, gjson.GetBytes(sent, "generationConfig.responseMimeType").Exists(), string(sent))
require.False(t, gjson.GetBytes(sent, "generationConfig.responseJsonSchema").Exists(), string(sent))
}
func TestGeminiForwardAsChatCompletions_CodeAssistKeepsResponseSchemaBytes(t *testing.T) {
gin.SetMode(gin.TestMode)
upstream := &queuedHTTPUpstreamStub{responses: []*http.Response{{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": []string{"text/event-stream"}},
Body: io.NopCloser(strings.NewReader(`data: {"response":{"candidates":[{"content":{"parts":[{"text":"{\"reasoning\":\"r\",\"answer\":\"a\"}"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":3,"candidatesTokenCount":5}}}` + "\n\n")),
}}}
svc := &GeminiMessagesCompatService{tokenProvider: &GeminiTokenProvider{}, httpUpstream: upstream, cfg: &config.Config{}}
account := &Account{
ID: 612,
Platform: PlatformGemini,
Type: AccountTypeOAuth,
Credentials: map[string]any{"access_token": "ya29.test-token", "project_id": "project-1"},
Concurrency: 1,
}
schema := `{"type":"object","properties":{"reasoning":{"type":"string"},"answer":{"type":"string"},"big":{"type":"integer","maximum":9007199254740993}},"required":["reasoning","answer"]}`
body := []byte(`{"model":"gemini-4-argon","response_format":{"type":"json_schema","json_schema":{"name":"r","schema":` + schema + `}},"messages":[{"role":"user","content":"hi"}]}`)
c, recorder := newAntigravityCompatContext(http.MethodPost, "/v1/chat/completions", body)
_, err := svc.ForwardAsChatCompletions(context.Background(), c, account, body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, recorder.Code)
require.Len(t, upstream.requestBodies, 1)
sent := upstream.requestBodies[0]
require.Equal(t, "gemini-4-argon", gjson.GetBytes(sent, "model").String())
require.Equal(t, "project-1", gjson.GetBytes(sent, "project").String())
require.Equal(t, "application/json", gjson.GetBytes(sent, "request.generationConfig.responseMimeType").String(), string(sent))
require.Equal(t, schema, gjson.GetBytes(sent, "request.generationConfig.responseJsonSchema").Raw)
require.Equal(t, `{"reasoning":"r","answer":"a"}`, gjson.Get(recorder.Body.String(), "choices.0.message.content").String())
}