> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anyfast.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini Nano Banana 2.1

> 通过 AnyFast 原生 Gemini API 使用 Google Gemini Nano Banana 2.1 生成和编辑图片。

Gemini Nano Banana 2.1 是 Google 最新的稳定版高效图片生成与对话式编辑模型。使用模型 ID `gemini-nano-banana-2.1` 调用原生 Gemini `GenerateContent` 接口。

<Info>
  Google 于 2026 年 10 月 6 日正式发布 `gemini-nano-banana-2.1`。Google 建议此前使用 `gemini-3.1-flash-image` 的新项目迁移到该模型。
</Info>

## 模型规格

| 项目 | 规格 |
| - | - |
| 模型 ID | `gemini-nano-banana-2.1` |
| 接口 | `POST /v1beta/models/gemini-nano-banana-2.1:generateContent` |
| 输入 | 文本、图片、视频、PDF |
| 输出 | 图片、文本 |
| 输入 Token 上限 | 131,072 |
| 输出 Token 上限 | 32,768 |
| 输出分辨率 | `1K`、`2K`、`4K`；默认 `1K` |
| 参考图片 | 最多 14 张；最多保持 4 个角色和 10 个物体的高保真一致性 |
| 思考强度 | `minimal`、`medium`、`high`；默认 `medium` |

## 核心能力

* 根据文本提示生成图片。
* 通过对话式指令编辑现有图片。
* 在单次请求中融合最多 14 张参考图片。
* 在连续编辑中保持角色和物体外观一致。
* 支持 `1:4`、`4:1`、`1:8`、`8:1` 等宽幅和全景比例。
* 生成信息图、菜单、图表等包含文字的视觉内容。

所有生成图片都包含 Google SynthID 水印。

## 请求示例

<Tabs sync={false}>
  <Tab title="文生图">
    使用 `responseModalities` 请求图片输出，并通过 `responseFormat.image` 设置宽高比和分辨率。

    ```bash cURL theme={null}
    curl "https://www.anyfast.ai/v1beta/models/gemini-nano-banana-2.1:generateContent?key=YOUR_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "contents": [{
          "role": "user",
          "parts": [{
            "text": "为一把半透明蓝色机械键盘设计简洁的 16:9 产品海报，深色影棚背景，标题为：TYPE FASTER。"
          }]
        }],
        "generationConfig": {
          "responseModalities": ["TEXT", "IMAGE"],
          "responseFormat": {
            "image": {
              "aspectRatio": "16:9",
              "imageSize": "2K"
            }
          }
        }
      }'
    ```
  </Tab>

  <Tab title="图片编辑">
    将 Base64 编码的参考图作为 `inline_data` 内容块，与编辑指令放在同一个 `parts` 数组中。

    ```bash cURL theme={null}
    curl "https://www.anyfast.ai/v1beta/models/gemini-nano-banana-2.1:generateContent?key=YOUR_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "contents": [{
          "role": "user",
          "parts": [
            {
              "text": "保持产品不变，将背景替换为温暖的木质桌面，并从左侧加入柔和的晨光。"
            },
            {
              "inline_data": {
                "mime_type": "image/png",
                "data": "YOUR_BASE64_IMAGE_DATA"
              }
            }
          ]
        }],
        "generationConfig": {
          "responseModalities": ["IMAGE"],
          "responseFormat": {
            "image": {
              "aspectRatio": "4:3",
              "imageSize": "2K"
            }
          }
        }
      }'
    ```
  </Tab>

  <Tab title="思考强度">
    使用 `thinkingConfig.thinkingLevel` 平衡延迟和图片质量，默认值为 `medium`。

    ```bash cURL theme={null}
    curl "https://www.anyfast.ai/v1beta/models/gemini-nano-banana-2.1:generateContent?key=YOUR_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "contents": [{
          "parts": [{
            "text": "设计一张技术结构准确的无反相机爆炸图信息图，并配上简洁的中文标签。"
          }]
        }],
        "generationConfig": {
          "responseModalities": ["IMAGE"],
          "thinkingConfig": {
            "thinkingLevel": "high"
          },
          "responseFormat": {
            "image": {
              "aspectRatio": "4:3",
              "imageSize": "4K"
            }
          }
        }
      }'
    ```
  </Tab>
</Tabs>

## 读取生成图片

响应可以同时包含文本内容块和图片内容块。对 `inlineData.data` 进行 Base64 解码，并根据 `inlineData.mimeType` 保存图片。

```python Python theme={null}
import base64
import requests

response = requests.post(
    "https://www.anyfast.ai/v1beta/models/gemini-nano-banana-2.1:generateContent",
    params={"key": "YOUR_API_KEY"},
    json={
        "contents": [{
            "parts": [{"text": "生成一张绿色数据中心的简洁等距插画。"}]
        }],
        "generationConfig": {
            "responseModalities": ["TEXT", "IMAGE"],
            "responseFormat": {
                "image": {"aspectRatio": "16:9", "imageSize": "1K"}
            },
        },
    },
)
response.raise_for_status()

for part in response.json()["candidates"][0]["content"]["parts"]:
    if "text" in part:
        print(part["text"])
    if "inlineData" in part:
        with open("output.png", "wb") as image_file:
            image_file.write(base64.b64decode(part["inlineData"]["data"]))
```

## 图片配置

| 字段 | 可选值 | 说明 |
| - | - | - |
| `generationConfig.responseModalities` | `IMAGE`，或 `TEXT` 和 `IMAGE` | 默认同时返回文本和图片。 |
| `generationConfig.responseFormat.image.imageSize` | `1K`、`2K`、`4K` | 默认 `1K`。Nano Banana 2.1 不支持 `512`。 |
| `generationConfig.responseFormat.image.aspectRatio` | `1:1`、`1:4`、`1:8`、`2:3`、`3:2`、`3:4`、`4:1`、`4:3`、`4:5`、`5:4`、`8:1`、`9:16`、`16:9`、`21:9` | 没有输入图片时默认 `1:1`；图片编辑通常沿用输入图比例，也可以显式覆盖。 |
| `generationConfig.thinkingConfig.thinkingLevel` | `minimal`、`medium`、`high` | 默认 `medium`。即使不返回思考内容，思考 Token 仍会计费。 |
| `generationConfig.thinkingConfig.includeThoughts` | boolean | 设为 `true` 后，响应中可能包含思考内容块；不要将其当作最终图片或回答。 |

<Warning>
  不要传入 `seed`、`topK`、`topP`、`temperature` 或 `logprobs`。Google 明确说明 Nano Banana 2.1 不支持这些参数，传入后会返回错误。
</Warning>

## 输入建议

* 将文本指令和参考图片放在同一个 `parts` 数组中。
* Base64 内容使用 `inline_data`，已上传文件 URI 使用 `file_data`。
* 支持的图片 MIME 类型包括 PNG、JPEG、WebP、HEIC 和 HEIF。
* 最多可以传入 14 张参考图片；角色一致性适合最多 4 个角色，物体保真适合最多 10 个物体。
* 视频和 PDF 输入同样占用 131,072 Token 上下文窗口。

图片中需要生成文字时，请在提示词中写出完整文字，并说明位置、字体风格和信息层级。

<Card title="Gemini Nano Banana 2.1 API 参考" icon="code" href="/zh/api-reference/model-api/google/gemini-nano-banana-2-1">
  查看原生 Gemini 图片生成接口的请求与响应字段。
</Card>

资料来源：[Gemini Nano Banana 2.1 模型页面](https://ai.google.dev/gemini-api/docs/models/gemini-nano-banana-2.1?hl=zh-cn)、[Google 图片生成指南](https://ai.google.dev/gemini-api/docs/generate-content/image-generation?hl=zh-cn)和 [Google Cloud 模型规格](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/gemini/nano-banana-2-1)。

<script src="/public/feedback.js" />


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.