# Gemini 原生生图

使用 gemini-nano-banana-2.1-c，通过单轮 generateContent 生成或编辑图片，返回原生 Base64，支持 1K/2K 档位。

- 原文: https://www.unifyllm.com/zh/docs/api-reference/nano-banana-image/
- 语言: zh-CN
- 更新于: 2026-11-27

## 接口

```http
POST https://api.unifyllm.top/v1beta/models/{model}:generateContent
```

认证与请求头以本页调用示例为准。 Content-Type: application/json

[相关接入说明](https://www.unifyllm.com/zh/docs/gemini-image/)

## 适用模型与返回方式

本页说明 gemini-nano-banana-2.1-c 在 UnifyLLM 的兼容规则。客户端统一请求 https://api.unifyllm.top，按下方示例填写完整接口路径。其他图像模型请查对应文档。

- 原生接口返回 candidates[].content.parts[].inlineData Base64。思考部分和不透明签名保持原样；解析时跳过 thought=true 的部分。

- 一次请求只向上游提交一次生成，不自动重试有费用的 POST。

- 参考图默认最多 14 张、原件总量 40 MB；平台或账户可进一步限制。PNG/JPEG/WebP 等受支持图片需能正常读取。

## 尺寸与比例

尺寸是清晰度档位和构图指令，不是精确像素承诺。2026-11-27 经本站公开接口实测，1920x1080 请求实际输出 2752×1536；结果不额外缩放或裁剪。

| 输入方式 | 示例 | 处理结果 |
| --- | --- | --- |
| imageSize | 1K / 2K | 仅启用的档位；不支持 4K |
| aspectRatio | 1:1, 2:3, 3:2, 4:3, 3:4, 16:9, 9:16 | 以当前模型启用的比例为准 |

## 单轮、参考图与限制

- 仅支持 generateContent，streamGenerateContent 不支持。contents 只能有一条当前 user content；不恢复多轮历史。

- URL 参考图需要公开可访问，拒绝私网地址；示例中的 example.com URL 和 Base64 占位符必须替换。不要将 Key 放在图片 URL 中。

- responseModalities 必须含 IMAGE。兼容 inlineData/inline_data 等字段；冲突并传会被拒绝。

- 整个请求包含 Base64 与 JSON 包装，默认不超过 64 MB；原件总量上限不等于最终请求体大小。

## 用量、错误和保存结果

- usage / usageMetadata 仅在上游提供时返回，不估算缺失值。图像接口为 input_tokens/output_tokens，Chat 为 prompt_tokens/completion_tokens；不要把缺失值当成免费。

- 400 通常为参数或模型能力不匹配，401 为认证问题，413 为请求过大，429 为容量或限流。上游拒绝、超时、无有效图片、保存失败等可能返回 500，保留请求 ID 和错误信息排查。

- 生成可能需要较长时间。Chat stream=true 也要等生成成功并保存后才输出结果，没有等待期间的进度。读取 SSE 直至 data: [DONE]，不要按 JSON 一次解析整条流。

- URL 不是永久存档：图片清理或签名变更后可能失效。请及时下载，不要改写 URL 的 id、sig 或查询串。

## 参数

| 参数 | 位置 | 类型 | 必填 | 说明 | 可选值 | 示例 |
| --- | --- | --- | --- | --- | --- | --- |
| model | path | string | 是 | 本站模型名。示例使用 gemini-nano-banana-2.1-c，具体权限以当前 Key 的模型列表为准。 |  | gemini-nano-banana-2.1-c |
| contents | body | array | 是 | 只接受一条当前 user content，role 可省略。parts 中必须有非空文字，可附参考图；不接受多轮历史。 |  |  |
| contents[].parts[].inlineData | body | object | 否 | Base64 图片：mimeType 与纯 Base64 data，不含 data URL 前缀。兼容 inline_data / mime_type。 |  |  |
| contents[].parts[].fileData | body | object | 否 | 参考图的公网 HTTP(S) URL 或图片 data URL，使用 fileUri；不支持账户 Files API 的 file_id。兼容 file_data / file_uri。 |  |  |
| generationConfig.responseModalities | body | array | 否 | 默认 ["IMAGE"]；可用 ["TEXT","IMAGE"]，必须含 IMAGE，不能只请求文字。 |  |  |
| generationConfig.imageConfig.imageSize | body | string | 否 | 仅支持模型启用的 1K、2K，默认 1K；显式 4K 会被拒绝。 | 1K, 2K | 1K |
| generationConfig.imageConfig.aspectRatio | body | string | 否 | 可用比例见下表；不填时，无参考图使用默认比例，有参考图时由上游决定。 | 1:1, 2:3, 3:2, 4:3, 3:4, 16:9, 9:16 |  |
| generationConfig.candidateCount | body | integer | 否 | 固定为 1。其他受支持的原生 generationConfig 属性保留；camelCase 和 snake_case 对象不能冲突并传。 |  |  |
| systemInstruction / safetySettings | body | object / array | 否 | 原生字段保留，能否生效仍由模型决定。不用于恢复聊天历史。 |  |  |

## 原生文生图

```json
{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "A red ceramic teapot on a white background. No text."
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "IMAGE"
    ],
    "imageConfig": {
      "imageSize": "1K",
      "aspectRatio": "1:1"
    }
  }
}
```

### cURL

```bash
curl --fail-with-body --max-time 300 'https://api.unifyllm.top/v1beta/models/gemini-nano-banana-2.1-c:generateContent' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "A red ceramic teapot on a white background. No text."
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "IMAGE"
    ],
    "imageConfig": {
      "imageSize": "1K",
      "aspectRatio": "1:1"
    }
  }
}'
```

### JavaScript

```javascript
const response = await fetch("https://api.unifyllm.top/v1beta/models/gemini-nano-banana-2.1-c:generateContent", {
  method: "POST",
  headers: {
  "Authorization": "Bearer <YOUR_API_KEY>",
  "Content-Type": "application/json"
},
  body: JSON.stringify({
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "A red ceramic teapot on a white background. No text."
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "IMAGE"
    ],
    "imageConfig": {
      "imageSize": "1K",
      "aspectRatio": "1:1"
    }
  }
}),
  signal: AbortSignal.timeout(300_000),
});
if (!response.ok) throw new Error(await response.text());
const data = await response.json();
console.log(data);
// Generated image: skip thought=true parts, then decode inlineData.data.
```

### Python

```python
import json
import requests

payload = json.loads("{\n  \"contents\": [\n    {\n      \"role\": \"user\",\n      \"parts\": [\n        {\n          \"text\": \"A red ceramic teapot on a white background. No text.\"\n        }\n      ]\n    }\n  ],\n  \"generationConfig\": {\n    \"responseModalities\": [\n      \"IMAGE\"\n    ],\n    \"imageConfig\": {\n      \"imageSize\": \"1K\",\n      \"aspectRatio\": \"1:1\"\n    }\n  }\n}")
response = requests.post(
    "https://api.unifyllm.top/v1beta/models/gemini-nano-banana-2.1-c:generateContent",
    headers={"Authorization": "Bearer <YOUR_API_KEY>"},
    json=payload,
    timeout=(10, 300),
)
response.raise_for_status()
print(response.json())
```

## 原生参考图

```json
{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "Change the teapot to blue; preserve its shape and white background."
        },
        {
          "inlineData": {
            "mimeType": "image/jpeg",
            "data": "<BASE64_IMAGE_BYTES>"
          }
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "IMAGE"
    ],
    "imageConfig": {
      "imageSize": "1K"
    }
  }
}
```

### cURL

```bash
curl --fail-with-body --max-time 300 'https://api.unifyllm.top/v1beta/models/gemini-nano-banana-2.1-c:generateContent' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "Change the teapot to blue; preserve its shape and white background."
        },
        {
          "inlineData": {
            "mimeType": "image/jpeg",
            "data": "<BASE64_IMAGE_BYTES>"
          }
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "IMAGE"
    ],
    "imageConfig": {
      "imageSize": "1K"
    }
  }
}'
```

### JavaScript

```javascript
const response = await fetch("https://api.unifyllm.top/v1beta/models/gemini-nano-banana-2.1-c:generateContent", {
  method: "POST",
  headers: {
  "Authorization": "Bearer <YOUR_API_KEY>",
  "Content-Type": "application/json"
},
  body: JSON.stringify({
  "contents": [
    {
      "role": "user",
      "parts": [
        {
          "text": "Change the teapot to blue; preserve its shape and white background."
        },
        {
          "inlineData": {
            "mimeType": "image/jpeg",
            "data": "<BASE64_IMAGE_BYTES>"
          }
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "IMAGE"
    ],
    "imageConfig": {
      "imageSize": "1K"
    }
  }
}),
  signal: AbortSignal.timeout(300_000),
});
if (!response.ok) throw new Error(await response.text());
const data = await response.json();
console.log(data);
// Generated image: skip thought=true parts, then decode inlineData.data.
```

### Python

```python
import json
import requests

payload = json.loads("{\n  \"contents\": [\n    {\n      \"role\": \"user\",\n      \"parts\": [\n        {\n          \"text\": \"Change the teapot to blue; preserve its shape and white background.\"\n        },\n        {\n          \"inlineData\": {\n            \"mimeType\": \"image/jpeg\",\n            \"data\": \"<BASE64_IMAGE_BYTES>\"\n          }\n        }\n      ]\n    }\n  ],\n  \"generationConfig\": {\n    \"responseModalities\": [\n      \"IMAGE\"\n    ],\n    \"imageConfig\": {\n      \"imageSize\": \"1K\"\n    }\n  }\n}")
response = requests.post(
    "https://api.unifyllm.top/v1beta/models/gemini-nano-banana-2.1-c:generateContent",
    headers={"Authorization": "Bearer <YOUR_API_KEY>"},
    json=payload,
    timeout=(10, 300),
)
response.raise_for_status()
print(response.json())
```

## 响应示例

### 原生 Base64 图片 — HTTP 200

```json
{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "inlineData": {
              "mimeType": "image/jpeg",
              "data": "<BASE64_GENERATED_IMAGE>"
            }
          }
        ]
      },
      "finishReason": "STOP",
      "index": 0
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 28,
    "candidatesTokenCount": 1120,
    "totalTokenCount": 1148
  },
  "modelVersion": "gemini-nano-banana-2.1-c"
}
```

## 相关接口

- [Gemini 生图 — Images 格式](https://www.unifyllm.com/zh/docs/api-reference/gemini-image-create/)
- [Gemini 改图 — 文件上传](https://www.unifyllm.com/zh/docs/api-reference/gemini-image-edit/)
- [Gemini 生图 — Chat 格式](https://www.unifyllm.com/zh/docs/api-reference/gemini-image-chat/)
