VibeAPIVibeAPI 开发者文档

Gemini 图像生成

gemini-3-pro-image 与 gemini-3.1-flash-image 的原生 generateContent 生图:宽高比与分辨率档位、图片编辑、多轮迭代、搜索接地与思考模式。全部经实测验证

POST /v1beta/models/{model}:generateContent

Gemini 系图像模型走 Gemini 原生协议。生图请务必用这个端点——走 Chat 兼容协议也能出图, 但 imageConfigaspectRatio 画幅、imageSize 分辨率)会在协议转换中整个丢掉,只能拿到默认尺寸。 文本对话见 Gemini generateContent;GPT 生图见 GPT 图像 · Image APIGPT 图像 · Responses,Grok 见 Grok 图像

基本调用

建议用官方 SDK(Python google-genai、Node.js @google/genai),base_url 填不带 /v1 的根地址。

curl -s -X POST \
  "https://www.vibeapi.cn/v1beta/models/gemini-3.1-flash-image:generateContent" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"parts": [{"text": "一只可爱的猫咪在阳光下打盹"}]}],
    "generationConfig": {
      "responseModalities": ["IMAGE"],
      "imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"}
    }
  }' -o response.json

# 提取图片
jq -r '.candidates[0].content.parts[0].inlineData.data' response.json | base64 -d > output.png
from google import genai
from google.genai import types

client = genai.Client(
    api_key="YOUR_API_KEY",
    http_options=types.HttpOptions(base_url="https://www.vibeapi.cn"),
)

response = client.models.generate_content(
    model="gemini-3.1-flash-image",
    contents="一只可爱的猫咪在阳光下打盹",
    config=types.GenerateContentConfig(
        response_modalities=["IMAGE"],
        image_config=types.ImageConfig(aspect_ratio="16:9", image_size="1K"),
    ),
)

for part in response.parts:
    if part.inline_data is not None:
        part.as_image().save("cat.png")
        break
import { GoogleGenAI } from "@google/genai";
import fs from "fs";

const ai = new GoogleGenAI({
  apiKey: "YOUR_API_KEY",
  httpOptions: { baseUrl: "https://www.vibeapi.cn" },
});

const response = await ai.models.generateContent({
  model: "gemini-3.1-flash-image",
  contents: "一只可爱的猫咪在阳光下打盹",
  config: {
    responseModalities: ["IMAGE"],
    imageConfig: { aspectRatio: "16:9", imageSize: "1K" },
  },
});

for (const part of response.candidates[0].content.parts) {
  if (part.inlineData) {
    fs.writeFileSync("cat.png", Buffer.from(part.inlineData.data, "base64"));
    break;
  }
}

以下内容均来自对本网关的真实调用,最后验证:2026-08-10。

可用模型

超时建议:512px / 1K 约 80 秒,2K 约 200 秒,4K 约 350 秒。

模型适用场景特点
gemini-3-pro-image-preview专业素材、复杂指令高级推理、搜索接地、最高 4K、最多 14 张参考图
gemini-3.1-flash-image日常生成、批量任务性价比高、支持 512px-4K、思考等级控制、图片搜索接地

请求格式

{
  "contents": [
    {
      "parts": [
        { "text": "你的提示词" }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": ["IMAGE"],
    "imageConfig": {
      "aspectRatio": "16:9",
      "imageSize": "1K"
    }
  }
}

generationConfig 参数

参数类型说明
responseModalitiesstring[]["IMAGE"] 仅图片;["TEXT", "IMAGE"] 图文混合(默认)
imageConfig.aspectRatiostring宽高比,见下方支持列表
imageConfig.imageSizestring分辨率档位:512px(仅 flash)、1K2K4K必须大写 K

支持的宽高比

全部 14 种,两个模型均已验证通过:

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

响应格式

{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [
          {
            "inlineData": {
              "mimeType": "image/png",
              "data": "<BASE64_IMAGE_DATA>"
            }
          }
        ]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 10,
    "candidatesTokenCount": 1120,
    "totalTokenCount": 1130
  }
}

图片在 candidates[0].content.parts[].inlineData 中,base64 编码。

responseModalities 包含 "TEXT" 时,parts 中可能同时包含 textinlineData

分辨率参考表

gemini-3.1-flash-image

宽高比512px1K2K4K
1:1512×5121024×10242048×20484096×4096
1:4256×1024512×20641024×41282048×8256
1:8176×1456352×2928704×58561408×11712
2:3416×624848×12641696×25283392×5056
3:2624×4161264×8482528×16965056×3392
3:4448×592896×12001792×24003584×4800
4:11024×2562064×5124128×10248256×2048
4:3592×4481200×8962400×17924800×3584
4:5464×576928×11521856×23043712×4608
5:4576×4641152×9282304×18564608×3712
8:11456×1762928×3525856×70411712×1408
9:16384×688768×13761536×27523072×5504
16:9688×3841376×7682752×15365504×3072
21:9784×3361584×6723168×13446336×2688

512px 档位仅 flash 模型支持。

gemini-3-pro-image-preview

支持 1K2K4K,不支持 512px。分辨率与 flash 的 1K/2K/4K 一致。

耗时参考

档位典型耗时
512px10-17s
1K13-40s
2K40-170s
4K120-310s

图片编辑

原图与文字指令一起放进 contents,即为编辑;SDK 直接传 PIL Image,REST 用 inline_data 传 base64。 只改局部时,在指令里明确说清保留什么:

from PIL import Image

response = client.models.generate_content(
    model="gemini-3.1-flash-image",
    contents=[Image.open("cat.png"), "给这只猫戴上一顶圣诞帽"],
    config=types.GenerateContentConfig(
        response_modalities=["IMAGE"],
        image_config=types.ImageConfig(aspect_ratio="1:1", image_size="1K"),
    ),
)
for part in response.parts:
    if part.inline_data is not None:
        part.as_image().save("cat_hat.png")
        break

同一写法覆盖三类常见任务,只换指令:

任务指令示例
局部重绘(语义遮盖)Change only the background to a snowy winter scene. Keep the cat exactly the same.
风格迁移Transform this photograph into the style of Van Gogh's Starry Night. Preserve the composition but render with swirling, impasto brushstrokes.
多图合成contents 里放多张图 + 指令:让第二张图中的人穿上第一张图中的蓝色连衣裙,生成一张专业电商照片

REST 写法(多图合成):

curl -s -X POST \
  "https://www.vibeapi.cn/v1beta/models/gemini-3.1-flash-image:generateContent" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"parts": [
      {"text": "让第二张图中的人穿上第一张图中的蓝色连衣裙,生成一张专业电商照片"},
      {"inline_data": {"mime_type": "image/png", "data": "<DRESS_BASE64>"}},
      {"inline_data": {"mime_type": "image/png", "data": "<MODEL_BASE64>"}}
    ]}],
    "generationConfig": {
      "responseModalities": ["IMAGE"],
      "imageConfig": {"aspectRatio": "3:4", "imageSize": "2K"}
    }
  }'

多轮迭代

把上一轮模型返回的完整 parts(含 inlineDatathoughtSignature)以 role: "model" 原样放回 contents,再追加新指令:

history = [{"role": "user", "parts": [{"text": "画一只橘猫坐在窗台上"}]}]
r1 = client.models.generate_content(model="gemini-3.1-flash-image", contents=history,
        config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]))

history.append(r1.candidates[0].content)                       # 模型回复原样入史
history.append({"role": "user", "parts": [{"text": "把背景改成下雨天"}]})
r2 = client.models.generate_content(model="gemini-3.1-flash-image", contents=history,
        config=types.GenerateContentConfig(response_modalities=["TEXT", "IMAGE"]))

高级功能

Google 搜索接地

基于实时搜索数据生成图片(如天气、新闻、股票)。在请求中添加 tools 字段:

{
  "contents": [{"parts": [{"text": "可视化旧金山今天的天气预报"}]}],
  "tools": [{"google_search": {}}],
  "generationConfig": {
    "responseModalities": ["TEXT", "IMAGE"],
    "imageConfig": {"aspectRatio": "16:9"}
  }
}

响应中会额外返回 groundingMetadata,包含 webSearchQueries(搜索词)和 groundingChunks(来源链接)。

Flash 还额外支持图片搜索接地,可用网络图片作为视觉参考:

"tools": [{"google_search": {"search_types": {"web_search": {}, "image_search": {}}}}]

思考模式

Pro 模型默认启用思考模式,会先生成构思草图再输出最终图片。响应中 part.thought == true 的为思考过程,可跳过。

Flash 模型支持控制思考等级(minimal 默认 或 high),通过 generationConfig.thinkingConfig 设置:

{
  "contents": [{"parts": [{"text": "A futuristic city inside a glass bottle floating in space"}]}],
  "generationConfig": {
    "responseModalities": ["IMAGE"],
    "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"},
    "thinkingConfig": {
      "thinkingLevel": "high",
      "includeThoughts": true
    }
  }
}

high 模式下响应 parts 会包含多种类型:

part 类型说明
thought == true + text思考文本(推理过程)
thought == true + inlineData构思草图(临时图片)
inlineData(无 thought)最终输出图片

提取最终图片时跳过 thought parts:

for part in response.candidates[0].content.parts:
    if getattr(part, "thought", False):
        continue  # 跳过思考过程
    if part.get("inlineData"):
        # 这是最终图片
        save(part["inlineData"])

无论 includeThoughts 设为 true 还是 false,思考 token 都会计费。high 模式会消耗更多 token 但图片质量更高。

多张参考图片

Pro 支持最多 6 张对象图 + 5 张人物图(共 14 张);Flash 支持最多 10 张对象图 + 4 张人物图。

能力边界

  1. imageSize 大小写:必须用大写 K1K2K4K),小写 1k 会被拒绝
  2. 512px 仅 flash:Pro 模型不支持 512px 档位
  3. 超时:4K 分辨率生成可能需要 2-5 分钟,务必设置足够的超时
  4. Token 消耗:512px 约 747 token,1K 约 1120 token,4K 约 2000 token
  5. 默认行为:不传 imageConfig 时,默认输出约 1408×768(接近 16:9 的 1K)
  6. 图片格式:响应中 mimeType 通常为 image/png,偶尔为 image/jpeg
  7. SynthID 水印:所有生成图片均包含 SynthID 数字水印
  8. 推荐语言:英语、中文、日语、韩语、法语、德语、西班牙语等

在线调试

填入你自己的 API Key 即可直接发起请求,参数表与响应结构由接口定义生成。

POST
/v1beta/models/{model}:generateContent/

Authorization

BearerAuth

AuthorizationBearer <token>

使用 Bearer Token 认证。 格式: Authorization: Bearer sk-xxxxxx

In: header

Path Parameters

model*string

模型名称

Request Body

application/json

contents*
generationConfig*

Response Body

application/json

curl -X POST "https://www.vibeapi.cn/v1beta/models/string:generateContent/" \  -H "Content-Type: application/json" \  -d '{    "contents": [      {}    ],    "generationConfig": {      "responseModalities": [        "string"      ],      "imageConfig": {        "aspectRatio": "string",        "imageSize": "string"      }    }  }'
{
  "candidates": [
    {
      "content": {
        "role": "string",
        "parts": [
          {}
        ]
      },
      "finishReason": "string",
      "safetyRatings": [
        {}
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 0,
    "candidatesTokenCount": 0,
    "totalTokenCount": 0
  }
}

官方文档