feat: support gemini imagen 4 generate model#4890
Conversation
WalkthroughAdds end-to-end handling for Gemini native Imagen predict requests: DTO/validation for image requests, relay orchestration for predict flows, adaptor size→aspect mapping and routing, a native response handler counting non-RAI images and returning usage, and a pricing entry for imagen-4.0-generate-001. ChangesGemini Native Imagen Prediction
Sequence DiagramsequenceDiagram
participant Client
participant RequestValidator
participant RelayHandler as geminiImagePredictHelper
participant ChannelAdaptor as Gemini Adaptor
participant UpstreamAPI as GoogleCloudImagen
participant ResponseHandler as GeminiNativeImagePredictHandler
Client->>RequestValidator: POST /models/imagen-4.0-generate-001:predict (GeminiImageRequest)
RequestValidator->>RequestValidator: isGeminiImagePredictPath() & GetAndValidateGeminiImageRequest()
RequestValidator->>RelayHandler: validated *GeminiImageRequest
RelayHandler->>ChannelAdaptor: ConvertImageRequest (size → aspect ratio)
ChannelAdaptor->>UpstreamAPI: POST upstream predict request
UpstreamAPI-->>ChannelAdaptor: 200 + GeminiImageResponse
ChannelAdaptor->>ResponseHandler: DoResponse routes to native handler
ResponseHandler->>ResponseHandler: unmarshal, count non-RAI BytesBase64Encoded
ResponseHandler-->>Client: raw body + Usage header
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Important
📝 变更描述 / Description
本 PR 为 Gemini Imagen 4 模型接入支持,目标模型为
imagen-4.0-generate-001。主要变更:
imagen-4.0-generate-001补充默认模型价格。:predict图片请求的解析、转发、响应处理与用量统计。GeminiImageRequest满足现有 relay 请求接口,保证模型映射、参数覆盖和配额消费流程可以复用原有逻辑。:predict图片响应复用同一处理逻辑。这样可以同时支持:
/v1/images/generations/v1/models/imagen-4.0-generate-001:predict🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
验证 curl

curl http://localhost:3000/v1/images/generations \ --request POST \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "model": "imagen-4.0-generate-001", "prompt": "A clean product photo of a ceramic coffee mug on a walnut desk, soft daylight, realistic", "n": 1, "size": "1024x1024", "quality": "standard", "response_format": "b64_json" }'curl http://localhost:3000/v1/models/imagen-4.0-generate-001:predict \ --request POST \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "instances": [ { "prompt": "A clean product photo of a ceramic coffee mug on a walnut desk, soft daylight, realistic" } ], "parameters": { "sampleCount": 1, "aspectRatio": "1:1", "personGeneration": "allow_adult" } }'本地验证通过:
Summary by CodeRabbit
New Features
Bug Fixes / Validation