Skip to content

fix(gemini): 兼容入口 chat 请求转发 response_format 结构化输出 - #8662

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
feeeei:fix/gemini-chat-response-format
Nov 16, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
feeeei:fix/gemini-chat-response-format

Conversation

@feeeei

@feeeei feeeei commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

背景

Gemini 平台的 /v1/chat/completions 兼容入口按 chat → Responses → Anthropic → Gemini 的链路转换请求。response_format 在 chat → Responses 这一步会转成 text.format,但到 Responses → Anthropic(apicompat.ResponsesToAnthropicRequest)时被丢弃,Gemini 请求的 generationConfig 里既没有 responseMimeType,也没有 schema。

结果是客户端即使指定了 response_format: {"type":"json_schema", ...},上游也只能靠 prompt 里的文字约束输出格式。生产上能看到这类请求返回非法 JSON:未转义的引号、尾逗号、套上 ```json 代码块,或者干脆输出 Markdown 文字。

改动

新增 applyGeminiChatResponseFormat。在 forwardClaudeBodyAsChatCompletions 里,紧接思考档位处理之后,根据客户端原始的 response_format 写入 generationConfig:

response_format.type 写入内容
json_object responseMimeType: "application/json"
json_schema responseMimeType,并把 json_schema.schema 原样写入 responseJsonSchema;没有 schema 时只写 responseMimeType
text、缺省、无法解析 请求不变

以下两种情况不写,请求保持原样:

  • Gemini 请求带 tools:Gemini 2.x 不接受工具与 JSON responseMimeType 同用。
  • 上游模型是图片输出模型(isGeminiImageOutputModel,与图片生成门禁同一判定)。

schema 写入 responseJsonSchema(JSON Schema 原样),不转换成 OpenAPI 子集的 responseSchema,避免有损转换。实测官方 Vertex、AI Studio 以及几家第三方中转,对含 $schema、additionalProperties: false、anyOf(含 null)、minLength/maxLength、minimum/maximum、minItems/maxItems、enum 的 schema 都返回 200。官方上游会把 schema 计入 promptTokenCount,输出为紧凑的合法 JSON。

同一个 chat 文件里,Code Assist(OAuth + project_id)分支原来先把 Gemini 请求 Unmarshal 成 any,再与 model、project 一起 Marshal。这样会把 schema 的属性按字母序重排,客户端有意写成"先 reasoning 后 answer"的顺序会丢失;超过 2^53 的整数也会丢精度。现在改为用 sjson.SetRawBytes 原样嵌入 request,顶层 key 的顺序保持不变。

未改动

  • 没有改 apicompat.ResponsesToAnthropicRequest。它也被 Claude 平台的 chat/responses 转换共用,改它会让 Claude 上游收到 output_config.format。
  • /v1/messages 兼容入口(GeminiMessagesCompatService.Forward)同样会丢弃 Anthropic 的 output_config.format / output_format。这是另一种协议形状,本 PR 不处理。
  • ForwardNative(/v1beta)的 Code Assist 分支也是同款 Unmarshal → Marshal 包装,客户端原生发送的 responseJsonSchema 同样会被重排。这个问题在本 PR 之前就存在,本 PR 不处理。

测试

  • 新增用例:
    • TestApplyGeminiChatResponseFormat:覆盖 json_schema、json_object、text、null、非法 JSON、带 tools、图片模型等情况。
    • TestGeminiForwardAsChatCompletions_ForwardsResponseFormat:非流式 json_schema 与流式 json_object 的上游请求体。
    • TestGeminiForwardAsChatCompletions_ResponseFormatSkippedWithTools
    • TestGeminiForwardAsChatCompletions_CodeAssistKeepsResponseSchemaBytes:断言属性顺序与大整数逐字节保留。
  • 去掉接线后,转发用例失败;把 Code Assist 包装改回 Unmarshal → Marshal 后,CodeAssistKeepsResponseSchemaBytes 失败。
  • 以下命令均通过:
    • go test -tags=unit ./internal/service/ ./internal/handler/... ./internal/pkg/apicompat/...
    • go test ./internal/service/ ./internal/handler/... ./internal/pkg/apicompat/...
    • golangci-lint run ./internal/service/... ./internal/handler/...

/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 属性顺序、截断大整数。
@Wei-Shaw
Wei-Shaw merged commit 980ddd9 into Wei-Shaw:main Nov 16, 2026
9 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants