Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(api): improve multimodal compatibility (snake_case fields, URL-safe base64, GIF to PNG) - #22

Merged
Mag1cFall merged 3 commits into
Mag1cFall:mainfrom
anon0v0:fix/support-urlsafe-base64
Sep 14, 2026
Merged

fix(api): improve multimodal compatibility (snake_case fields, URL-safe base64, GIF to PNG)#22
Mag1cFall merged 3 commits into
Mag1cFall:mainfrom
anon0v0:fix/support-urlsafe-base64

Conversation

@anon0v0

@anon0v0 anon0v0 commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

问题背景 (Background)

在多模态图片/媒体输入场景中,存在以下三个兼容性问题:

  1. 下划线字段兼容性缺失 (snake_case fields)
    部分中转工具(如 CLIProxyAPI / CPA)或基于 Google Python SDK 的客户端在向 Gemini 原生接口 (/v1beta/models/...:streamGenerateContent) 发送附件时,会将字段命名为下划线风格(如 inline_data、 ile_data、mime_type、 ile_uri)。此前 geminiPart 结构体仅声明了驼峰 tag,导致 Go 的 JSON 反序列化后 MIMEType 为空,从而触发 HTTP 400 · inlineData requires mimeType and data 错误。

  2. URL-safe Base64 编码与 Data URL 前缀容错
    部分官方 SDK(如 Python Google GenAI SDK)和第三方客户端在序列化 Base64 时会生成 URL-safe 编码(使用 -、_ 且省略尾部 = 填充),或者包含 data:image/...;base64, 前缀。此前直接调用 �ase64.StdEncoding.DecodeString 会抛出 illegal base64 data 400 错误。

  3. Google AI Studio 对 GIF 格式的不支持
    Google AI Studio 官方网页与底层 API 不支持直接接收 image/gif 输入,上传或调用时会抛出协议错误码 3 (Unable to process input image)。


修复内容 (Changes)

  1. 多模态结构体与字段兼容
    • 在 internal/api/gemini.go 和 internal/api/video.go 中,为 geminiPart、geminiVideoImageInput 等结构体补充 inline_data、 ile_data、mime_type、 ile_uri 别名映射,并统一通过 inline() / ile() 辅助方法安全取值,完美兼容驼峰与下划线两种风格。
  2. 高性能零拷贝 Base64 解码 (decodeBase64Flexible)
    • 自动剥离 data: URI 前缀;
    • 依次尝试标准库解码器(�ase64.StdEncoding -> �ase64.RawStdEncoding -> �ase64.URLEncoding -> �ase64.RawURLEncoding),彻底避免字符串重复拷贝替换产生的大对象堆内存分配,显著降低大图请求下的 GC 压力。
  3. GIF 自动转换为 PNG (
    ormalizeImagePayload)
    • 自动检测并提取 GIF 首帧转换为 PNG 格式,并将 MIME 类型更新为 image/png;
    • 在 Gemini、OpenAI、Anthropic 及 Video 接口中统一调用。
  4. 单元测试与格式化
    • 补充针对标准 Base64、URL-safe Base64、Data URL 前缀、GIF 转换、以及多模态 snake_case/camelCase 兼容性的完整单元测试并通过;
    • 通过 go fmt 保持代码风格整洁规范。

@anon0v0 anon0v0 changed the title fix(api): 支持 URL-safe 及缺少 padding 的 base64 解码 fix(api): 支持 URL-safe Base64 并自动将 GIF 转为 PNG 兼容 Google AI Studio Sep 13, 2026
@anon0v0 anon0v0 changed the title fix(api): 支持 URL-safe Base64 并自动将 GIF 转为 PNG 兼容 Google AI Studio fix(api): support URL-safe base64 and auto-convert GIF to PNG for Google AI Studio Sep 13, 2026
@anon0v0

anon0v0 commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

补充更新:

除 URL-safe Base64 容错外,本 PR 进一步补充了对 的自动转换支持:

  • 背景:Google AI Studio 网页端原生不支持将 作为静态多模态图片输入(接口会返回 )。网页版官方 AI Studio 在用户上传 GIF 时也是自动抽帧转为静态图片发送。
  • 改动:新增 ,当检测到图片为 时,使用 Go 标准库 解码首帧并转为 重新提交,完美对齐网页端体验。

@anon0v0 anon0v0 closed this Sep 13, 2026
@anon0v0 anon0v0 reopened this Sep 13, 2026
@anon0v0 anon0v0 changed the title fix(api): support URL-safe base64 and auto-convert GIF to PNG for Google AI Studio fix(api): improve multimodal compatibility (snake_case fields, URL-safe base64, GIF to PNG) Sep 13, 2026
@Mag1cFall
Mag1cFall merged commit cc078f4 into Mag1cFall:main Sep 14, 2026
@Mag1cFall

Copy link
Copy Markdown
Owner

非常感谢~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants