Google Gemini 系列同步图像生成 (T2I / I2I / 多图合成 / 多轮编辑, 原生 Interactions API 格式)
POST /v1/images/generations
Auth: {'type': 'bearer', 'description': '在 HTTP header `Authorization: Bearer sk-xxx` 中传入您的 API Key'}
**Gemini Banana (Nano Banana)** 是 Google Gemini 系列的多模态图像生成模型, 支持: - **文生图** (Text-to-Image) - **图像编辑** (Image-to-Image, 传入 1 张参考图 + 文本指令) - **多图合成** (Multi-Image Composition) — `gemini-3.1-flash-image-preview` 支持最多 **14 张参考图** (10 张物体 + 4 张人物) - **多轮编辑** (服务端持有对话历史, 通过 `previous_interaction_id` 链接迭代修改) ## 调用方式 按 **Google Gemini Interactions API** (2026-05 新协议, 字段全 snake_case) 传 body, 同步返回。 客户端继续使用我方 OpenAI 兼容路径 `https://api.router.ai/v1/images/generations`, 不需要直连 Google。 ## 支持的模型 | Model | 说明 | |---|---| | `gemini-3.1-flash-image-preview` | Nano Banana 主力推荐, 4 档分辨率 (512px/1K/2K/4K), 含 thinking 模式, 14 种比例 | | `gemini-3-pro-image-preview` | Nano Banana 专业版, 含 Thinking 推理 + 高保真文本渲染 | | `gemini-2.5-flash-image` | Nano Banana 经典 (GA), 高吞吐低延迟, 1K 单档 | | `gemini-2.5-flash-image-preview` | Nano Banana 经典预览版 (兼容老 SDK 别名) | ## 输出分辨率配置 通过 `response_format.image_size` 指定: | image_size | 实际尺寸 (含 aspect_ratio=1:1) | 适用模型 | |---|---|---| | `"512px"` | 512×512 (注意 **px 后缀**) | 仅 `gemini-3.1-flash-image-preview` | | `"1K"` | 1024×1024 | 全部 (推荐默认) | | `"2K"` | 2048×2048 | gemini-3.1-flash / gemini-3-pro | | `"4K"` | 4096×4096 | gemini-3.1-flash / gemini-3-pro | **`aspect_ratio` 取值** (`gemini-3.1-flash` 支持 14 种): `"1:1"` / `"16:9"` / `"9:16"` / `"3:2"` / `"2:3"` / `"4:3"` / `"3:4"` / `"5:4"` / `"4:5"` + `"1:4"` / `"4:1"` / `"1:8"` / `"8:1"` (仅 `gemini-3.1-flash`) 不传 `response_format` 时走默认 (通常 1K 1:1)。 ## 字段命名 REST API 全部 **snake_case** (`response_format`, `mime_type`, `image_size`, `aspect_ratio`, `previous_interaction_id`, `generation_config`, `system_instruction` 等)。 ## 多轮编辑链 (重要) 服务端默认持有对话历史 (`store: true`)。多轮编辑 1 张图时: 1. 首轮 POST 拿到 `id` (`v1_xxx...`) 2. 第二轮把上次 `id` 放入 `previous_interaction_id` 字段, 重新 POST (无需重传图片, 服务端记得) **数据保留**: 付费层 55 天, 免费层 1 天。 参考: [Google Interactions Image Generation](https://ai.google.dev/gemini-api/docs/interactions/image-generation)
Authorization | string | required | Bearer sk-xxx |
Content-Type | string | required |
model | string | required | 模型 ID, 见上方支持的模型列表 |
input | string|array | required | 输入内容. 单字符串 (`"a red apple"`) 或 input block 数组 (混合 text + image) |
type | string | input block 类型 (仅 array 形态) | |
text | string | 文本提示 (type=text 时使用) | |
mime_type | string | 图像 MIME 类型 (type=image 时使用) | |
data | string | 图像 base64 编码字符串 (不含 data: URI prefix, type=image 时使用) | |
system_instruction | string | 系统提示 (可选, 控制模型行为) | |
response_format | object | 输出格式 (可选). 不传时走默认 1K 1:1 | |
type | string | 响应类型, 图像生成必传 `image` | |
mime_type | string | 输出图像 MIME 类型 | |
image_size | string | 输出分辨率. `512px` 仅 3.1 Flash 支持 (注意 `px` 后缀); 其他模型仅 1K/2K/4K | |
aspect_ratio | string | 宽高比. 14 种 (3.1 Flash 全部支持; 其他模型仅 9 种基础) | |
generation_config | object | 生成参数 (可选) | |
thinking_level | string | 思考强度 (gemini-3.1-flash / gemini-3-pro 支持). 取 `minimal` (默认快速) 或 `high` (高质量推理) | |
temperature | number | 采样温度 (0.0-2.0, 影响生成多样性) | |
previous_interaction_id | string | 多轮链接. 上一次 POST 响应中的 `id` (`v1_xxx`); 服务端持有 history, 客户端无需重传上下文 | |
tools | array | 工具调用 (可选). 注: `google_search` 类工具会被网关入口安全过滤层移除, 详见提示 | |
store | boolean | 服务端是否持有对话历史. `true` (默认) 服务端记忆; `false` 客户端自己维护 history |
200 — 生成成功. Response 含 `id` (多轮用) + `steps[]` (含 model_output 步骤, 内嵌 image content) + `usage` (modality 分)200 — Thinking 模式响应 (steps[] 含 thought 步骤 + model_output, generation_config.thinking_level 设置时返回)200 — 失败状态 (status=failed, steps[] 空; 上游图像安全过滤拦截或 quota 不足)400 — 请求参数错误 (注意 Interactions API 错误信息简洁, 不返 fieldViolations)401 — API Key 无效或缺失503 — 上游服务暂时不可用# 1. 文生图 (T2I, input 单字符串, 默认 1K 1:1)
curl -X POST https://api.router.ai/v1/images/generations \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-image-preview",
"input": "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme"
}'
# 2. T2I + 指定分辨率 2K 16:9 (response_format)
curl -X POST https://api.router.ai/v1/images/generations \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-image-preview",
"input": "a futuristic city at sunset, photorealistic, cinematic",
"response_format": {
"type": "image",
"mime_type": "image/jpeg",
"aspect_ratio": "16:9",
"image_size": "2K"
}
}'
# 3. I2I 图像编辑 (input 数组, text + image base64)
curl -X POST https://api.router.ai/v1/images/generations \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-image-preview",
"input": [
{"type": "text", "text": "Create a picture of my cat eating a nano-banana in a fancy restaurant under the Gemini constellation"},
{"type": "image", "mime_type": "image/jpeg", "data": "<BASE64_IMAGE_DATA>"}
]
}'
# 4. 多图合成 (Multi-Image Composition, 多张参考图)
curl -X POST https://api.router.ai/v1/images/generations \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-image-preview",
"input": [
{"type": "text", "text": "An office group photo of these people, they are making funny faces."},
{"type": "image", "mime_type": "image/png", "data": "<BASE64_DATA_IMG_1>"},
{"type": "image", "mime_type": "image/png", "data": "<BASE64_DATA_IMG_2>"},
{"type": "image", "mime_type": "image/png", "data": "<BASE64_DATA_IMG_3>"}
],
"response_format": {
"type": "image",
"mime_type": "image/jpeg",
"aspect_ratio": "5:4",
"image_size": "2K"
}
}'
# 5. 多轮编辑链 (上次 id 放入 previous_interaction_id, 无需重传图片)
curl -X POST https://api.router.ai/v1/images/generations \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-image-preview",
"input": "Update this infographic to be in Spanish. Do not change any other elements of the image.",
"previous_interaction_id": "v1_ChdEdGNPYXY3bkhNV00xZThQNjlyaXdBMBIXRHRjT2F2N25ITVdNMWU4UDY5cml3QTA",
"response_format": {
"type": "image",
"mime_type": "image/jpeg",
"aspect_ratio": "16:9",
"image_size": "2K"
}
}'
# 6. Thinking 模式 (gemini-3.1-flash / gemini-3-pro 支持; minimal 默认 / high 推理强度)
curl -X POST https://api.router.ai/v1/images/generations \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-pro-image-preview",
"input": "draw a step-by-step guide on how to bake a banana bread",
"generation_config": {
"thinking_level": "high"
},
"response_format": {
"type": "image",
"image_size": "1K"
}
}'