Gemini 原生
- 走 Gemini 原生 generateContent 结构
- 图片以 inlineData Base64 返回
- 提示词与参考图都放在 contents 里
认证
Gemini 原生格式认两种写法:请求头 x-goog-api-key: YOUR_API_KEY,或查询参数 ?key=YOUR_API_KEY。两者同时存在时以请求头为准。Key 与控制台创建的 API Key 相同。
The native Gemini format accepts either the x-goog-api-key: YOUR_API_KEY header or a ?key=YOUR_API_KEY query parameter; the header wins when both are present. It's the same key you create in the console.
请求结构
请求体是 Gemini 的原生结构,和 OpenAI 的平铺 JSON 完全不同:
contents(array / 必填) — 对话内容,每条含role与parts。contents[].parts(array / 必填) — 文本用{"text": "..."},图片用{"inlineData": {...}}。generationConfig.responseModalities(array / 必填) — 必须同时包含TEXT与IMAGE,否则不返回图片。generationConfig.imageConfig(object / 可选) — 画幅与分辨率,见请求参数。
The body is Gemini's native shape, nothing like OpenAI's flat JSON:
contents(array / required) — the conversation, each turn withroleandparts.contents[].parts(array / required) — text as{"text": "..."}, images as{"inlineData": {...}}.generationConfig.responseModalities(array / required) — must include bothTEXTandIMAGEor no image is returned.generationConfig.imageConfig(object / optional) — aspect and size, described under request parameters.
支持的场景
| 场景 | 写法 |
|---|---|
| 文生图 | parts 里只有一段 text |
| 单图编辑 | parts 里加一段 inlineData,再跟 text 描述改动 |
| 多图参考 | parts 里加多段 inlineData,最多 14 张 |
| 指定画幅 | imageConfig.aspectRatio |
| 指定分辨率 | imageConfig.imageSize |
| Scenario | How |
|---|---|
| Text to image | parts holds one text entry |
| Single-image edit | Add an inlineData part, then a text part describing the change |
| Multi-image reference | Add several inlineData parts, up to 14 |
| Aspect ratio | imageConfig.aspectRatio |
| Resolution | imageConfig.imageSize |
请求参数
路径参数,模型 ID 原样写在 {model} 位置。必须写全名。
对话内容,含 role 与 parts。
固定 ["TEXT", "IMAGE"]。
画幅比例,如 1:1、4:3、16:9、9:16。
输出档位,1K、2K、4K。
安全阈值设置,按类别覆盖默认值。
Path parameter: the model ID where {model} sits. It must be the full name.
The conversation, with role and parts.
Fixed to ["TEXT", "IMAGE"].
Aspect ratio such as 1:1, 4:3, 16:9, 9:16.
Output tier: 1K, 2K, or 4K.
Safety thresholds that override the defaults per category.
参考图怎么传
inlineData 只接受 Base64,不接受 URL。data 是纯 Base64,不带 data:image/png;base64, 前缀,mimeType 写实际格式。
需要传公网 URL 时改用 fileData.fileUri,但它只对部分上游渠道有效。稳定做法是本地转 Base64 后用 inlineData。
inlineData takes Base64 only, never a URL. data is raw Base64 with no data:image/png;base64, prefix, and mimeType names the real format.
To pass a public URL, use fileData.fileUri, which only works on some upstream channels. The reliable route is to base64 the file locally and send inlineData.
响应
图片在 candidates[0].content.parts 里,以 inlineData 返回:
{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{ "text": "这是你要的图片" },
{ "inlineData": { "mimeType": "image/png", "data": "iVBORw0KGgoAAAANSUhEUg..." } }
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 24,
"candidatesTokenCount": 1290,
"totalTokenCount": 1314
}
}candidates[].content.parts[](array) — 文本段带text,图片段带inlineData。candidates[].content.parts[].inlineData.data(string) — 纯 Base64,自行拼data:<mimeType>;base64,才能放进<img>。candidates[].finishReason(string) — 结束原因,被安全策略拦截时为SAFETY。usageMetadata(object) — token 用量,含promptTokenCount、candidatesTokenCount、totalTokenCount。
返回值没有可访问的图片地址,只有 Base64。需要 URL 时先自行转存到对象存储。
Images come back in candidates[0].content.parts as inlineData:
{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{ "text": "这是你要的图片" },
{ "inlineData": { "mimeType": "image/png", "data": "iVBORw0KGgoAAAANSUhEUg..." } }
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 24,
"candidatesTokenCount": 1290,
"totalTokenCount": 1314
}
}candidates[].content.parts[](array) — text parts carrytext, image parts carryinlineData.candidates[].content.parts[].inlineData.data(string) — raw Base64; prependdata:<mimeType>;base64,to use it in an<img>.candidates[].finishReason(string) — why it stopped;SAFETYwhen blocked.usageMetadata(object) — token usage:promptTokenCount,candidatesTokenCount,totalTokenCount.
There's no fetchable image URL in the response, only Base64. Store it yourself if you need a URL.
与 OpenAI 兼容路径的差别
| 项 | Gemini 原生 | /v1/images/generations |
|---|---|---|
| 路径 | /v1beta/models/{model}:generateContent | /v1/images/generations |
| 请求体 | contents + generationConfig | 平铺 JSON |
| 返回 | inlineData 里的 Base64 | data[].url 或 data[].b64_json |
| 参考图 | inlineData,最多 14 张 | image,最多 14 张 |
| 流式 | 支持 streamGenerateContent | 不支持 |
已有 Gemini SDK 的项目用原生,其余用 OpenAI 兼容。
| Item | Gemini native | /v1/images/generations |
|---|---|---|
| Path | /v1beta/models/{model}:generateContent | /v1/images/generations |
| Body | contents + generationConfig | Flat JSON |
| Result | Base64 inside inlineData | data[].url or data[].b64_json |
| References | inlineData, up to 14 | image, up to 14 |
| Streaming | streamGenerateContent supported | Not supported |
Projects already on a Gemini SDK should use the native path; everything else uses the OpenAI-compatible one.
错误码
| HTTP | 含义 |
|---|---|
| 400 | responseModalities 缺 IMAGE,或 mimeType 非法 |
| 401 | Key 缺失或无效 |
| 429 | 触发限流 |
常见 400 原因:generationConfig 位置写错;inlineData.data 带了 data: 前缀;参考图超过 14 张。
| HTTP | Meaning |
|---|---|
| 400 | responseModalities missing IMAGE, or an illegal mimeType |
| 401 | Key missing or invalid |
| 429 | Rate limit hit |
Common 400 causes: generationConfig in the wrong place; a data: prefix inside inlineData.data; more than 14 reference images.
For more status codes see Error codes and rate limits; for troubleshooting see the FAQ.
使用示例
parts 里只放文本是文生图;先放 inlineData 再放文本是编辑,顺序会影响结果。imageConfig 控制画幅与分辨率档位。
Text-only parts is text to image; an inlineData part followed by text is an edit, and order affects the result. imageConfig sets aspect ratio and resolution tier.