fix(aistudio): 修复图像模型 GenerateContent 403 Code 7(补全官网扩展协议字段) - #31
Merged
Merged
Conversation
官网对图像生成模型的 GenerateContent 有一组隐藏扩展约束,经官网页面
XHR 抓包与逐字段消融回放实测,以下每一项缺失/错误都会独立触发
403 Code 7(The caller does not have permission):
- responseModalities 必须为 [IMAGE,TEXT],纯 IMAGE 被拒;
含 /v1/images/generations 显式传 [IMAGE] 的路径,统一归一化补 TEXT。
- wire[2] safetySettings:图像模型官网发 null,代理注入的配置会被拒。
- wire[6]:output_resolution 能力的模型必须下发固定值
[[null,null,null,[null,[[]]]]]。
- generationConfig imageConfig:output_resolution 模型缺省需带
[null,"1K"];无该能力的模型(如 2.5-flash-image)发了反而被拒。
- X-Goog-Ext-519733851-Bin:该头按 bootstrap 页面(gemini-flash-latest)
采样,其 proto field15 与图像模型页面取值不同(1 vs 0),错值被拒、
缺省可通过;图像请求改为置空(头合并时被删除)。
能力分闸依据模型目录 capabilities(image_route / output_resolution),
NB1 类只要求 modalities,多发扩展字段同样被拒。
已实测:gemini-3-pro-image 文生图、gemini-3.1-flash-image /
gemini-2.5-flash-image 图生图、原生 generateContent +
imageConfig{1:1,2K} + 参考图均通过。
Owner
|
非常感谢!复现后进行了更多修复,已合入。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
所有图像生成模型(
gemini-3-pro-image、gemini-3.1-flash-image、gemini-2.5-flash-image)经GenerateContent调用全部返回:文本模型不受影响。
/v1/chat/completions、/v1/images/generations、原生/v1beta/models/...:generateContent三个入口同样失败。根因
官网对图像模型的
GenerateContent请求有一组隐藏的扩展约束。通过在官网页面挂 XHR 探针抓取 UI 原生请求并逐字段消融回放,实测以下每一项缺失或错误都会独立触发 403 Code 7:generationConfig.responseModalities[IMAGE,TEXT]([2,1])[IMAGE]([2]),纯图像被拒null(图像模型禁发)[[null,null,null,[null,[[]]]]](output_resolution能力模型)generationConfigthinking[1](thinking能力模型)generationConfigimageConfig[null,"1K"](output_resolution模型缺省)X-Goog-Ext-519733851-Bin能力分闸(实测):
gemini-2.5-flash-image只要求 modalities,多发 wire[6]/imageConfig 反而 403;gemini-3-pro-image/gemini-3.1-flash-image(具output_resolution能力)需要全套。因此按模型目录 capabilities 分闸,不可统一补发。修复
GenerationDefaults新增ImageRoute/OutputResolution,由decodeGenerationDefaults从模型 capabilities 填充;applyModelMediaDefaults:image_route 模型强制ResponseModalities=[IMAGE,TEXT],并归一化显式传入的[IMAGE](覆盖/v1/images/generations路径);EncodeGenerateContentRequest:image_route 不发 wire[2] safety;output_resolution模型补发 wire[6] 固定槽位;encodeGenerationConfig:output_resolution模型缺省补imageConfig=[nil,"1K"],无该能力的模型丢弃 imageConfig;doProtected:image_route 请求置空X-Goog-Ext-519733851-Bin(经头合并后删除,上游视为缺省)。验证
gemini-3-pro-image文生图:200 出图 ✓gemini-3.1-flash-image/gemini-2.5-flash-image带参考图:200 ✓generateContent+imageConfig{aspectRatio:"1:1", imageSize:"2K"}+ 参考图:200,输出 1:1 图像 ✓gemini-3.1-flash-lite文本回归:正常 ✓go vet ./internal/aistudio/无警告;go test ./internal/aistudio/通过影响面
responseModalities:["IMAGE"]会被归一化为[IMAGE,TEXT](上游本就拒绝纯 IMAGE,此行为是放宽而非收紧)。