VibeAPI

GPT-Image-2

GPT-Image-2 图片生成 API 完整文档

概述

VibeAPI 兼容 OpenAI 图片生成接口,支持使用 GPT Image 模型(gpt-image-2)从文本提示生成和编辑图片。通过 Image API 提供两个端点:

两个端点都支持自定义输出,包括调整质量、尺寸、格式和压缩率。

本指南以 gpt-image-2 为例。

生成图片

使用图片生成端点根据文本提示创建图片。

关于自定义输出(尺寸、质量、格式、压缩率),请参考下方的自定义图片输出章节。

基本用法

import OpenAI from "openai";
import fs from "fs";
const openai = new OpenAI({
    baseURL: "https://www.vibeapi.cn/v1",
    apiKey: "YOUR_API_KEY",
});

const prompt = `
A children's book drawing of a veterinarian using a stethoscope to 
listen to the heartbeat of a baby otter.
`;

const result = await openai.images.generate({
    model: "gpt-image-2",
    prompt,
});

// 保存图片到文件
const image_base64 = result.data[0].b64_json;
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("otter.png", image_bytes);
from openai import OpenAI
import base64

client = OpenAI(
    base_url="https://www.vibeapi.cn/v1",
    api_key="YOUR_API_KEY",
)

prompt = """
A children's book drawing of a veterinarian using a stethoscope to 
listen to the heartbeat of a baby otter.
"""

result = client.images.generate(
    model="gpt-image-2",
    prompt=prompt
)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

# 保存图片到文件
with open("otter.png", "wb") as f:
    f.write(image_bytes)
curl -X POST "https://www.vibeapi.cn/v1/images/generations" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -H "Content-type: application/json" \
    -d '{
        "model": "gpt-image-2",
        "prompt": "A childrens book drawing of a veterinarian using a stethoscope to listen to the heartbeat of a baby otter."
    }' | jq -r '.data[0].b64_json' | base64 --decode > otter.png

参数说明

参数类型必填默认值说明
modelstring-固定为 gpt-image-2
promptstring-图片描述文本
sizestringauto图片尺寸,详见尺寸选项
qualitystringauto图片质量:low / medium / high / auto
output_formatstringpng输出格式:png / jpeg / webp
output_compressionnumber-压缩级别 0-100,仅 jpeg/webp 有效
response_formatstringb64_json响应格式:b64_json / urlb64_jsondata[0].b64_json 中返回纯 base64 字符串;urldata[0].url 中返回 data URI(data:image/<format>;base64,...)。注意:url 模式返回的是 data URI 而非 HTTP 链接,传输数据量与 b64_json 相同
moderationstringauto内容审核级别:auto / low
nnumber1不可用。参数会被静默忽略,始终只返回 1 张图片。如需多张请并发调用

编辑图片

图片编辑端点允许你:

  • 编辑已有图片
  • 使用其他图片作为参考生成新图片
  • 通过上传图片和蒙版来编辑图片的指定区域

使用参考图片生成新图片

你可以使用一张或多张图片作为参考来生成新图片。

以下示例使用 4 张输入图片生成一张包含参考图片中物品的礼品篮图片。

import base64
from openai import OpenAI

client = OpenAI(
    base_url="https://www.vibeapi.cn/v1",
    api_key="YOUR_API_KEY",
)

prompt = """
Generate a photorealistic image of a gift basket on a white background 
labeled 'Relax & Unwind' with a ribbon and handwriting-like font, 
containing all the items in the reference pictures.
"""

result = client.images.edit(
    model="gpt-image-2",
    image=[
        open("body-lotion.png", "rb"),
        open("bath-bomb.png", "rb"),
        open("incense-kit.png", "rb"),
        open("soap.png", "rb"),
    ],
    prompt=prompt
)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

# 保存图片到文件
with open("gift-basket.png", "wb") as f:
    f.write(image_bytes)
import fs from "fs";
import OpenAI, { toFile } from "openai";

const client = new OpenAI({
    baseURL: "https://www.vibeapi.cn/v1",
    apiKey: "YOUR_API_KEY",
});

const prompt = `
Generate a photorealistic image of a gift basket on a white background 
labeled 'Relax & Unwind' with a ribbon and handwriting-like font, 
containing all the items in the reference pictures.
`;

const imageFiles = [
    "bath-bomb.png",
    "body-lotion.png",
    "incense-kit.png",
    "soap.png",
];

const images = await Promise.all(
    imageFiles.map(async (file) =>
        await toFile(fs.createReadStream(file), null, {
            type: "image/png",
        })
    ),
);

const response = await client.images.edit({
    model: "gpt-image-2",
    image: images,
    prompt,
});

// 保存图片到文件
const image_base64 = response.data[0].b64_json;
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("basket.png", image_bytes);
curl -s -D >(grep -i x-request-id >&2) \
  -o >(jq -r '.data[0].b64_json' | base64 --decode > gift-basket.png) \
  -X POST "https://www.vibeapi.cn/v1/images/edits" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F "model=gpt-image-2" \
  -F "image[]=@body-lotion.png" \
  -F "image[]=@bath-bomb.png" \
  -F "image[]=@incense-kit.png" \
  -F "image[]=@soap.png" \
  -F 'prompt=Generate a photorealistic image of a gift basket on a white background labeled "Relax & Unwind" with a ribbon and handwriting-like font, containing all the items in the reference pictures'

使用蒙版编辑图片

你可以提供蒙版来指定图片中需要编辑的区域。

使用 GPT Image 的蒙版时,会向模型发送额外指令以引导编辑过程。

GPT Image 的蒙版完全基于提示。模型将蒙版作为引导,但可能无法完全精确地遵循其形状。

如果你提供了多张输入图片,蒙版将应用于第一张图片。

from openai import OpenAI

client = OpenAI(
    base_url="https://www.vibeapi.cn/v1",
    api_key="YOUR_API_KEY",
)

result = client.images.edit(
    model="gpt-image-2",
    image=open("sunlit_lounge.png", "rb"),
    mask=open("mask.png", "rb"),
    prompt="A sunlit indoor lounge area with a pool containing a flamingo"
)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

# 保存图片到文件
with open("composition.png", "wb") as f:
    f.write(image_bytes)
import fs from "fs";
import OpenAI, { toFile } from "openai";

const client = new OpenAI({
    baseURL: "https://www.vibeapi.cn/v1",
    apiKey: "YOUR_API_KEY",
});

const rsp = await client.images.edit({
    model: "gpt-image-2",
    image: await toFile(fs.createReadStream("sunlit_lounge.png"), null, {
        type: "image/png",
    }),
    mask: await toFile(fs.createReadStream("mask.png"), null, {
        type: "image/png",
    }),
    prompt: "A sunlit indoor lounge area with a pool containing a flamingo",
});

// 保存图片到文件
const image_base64 = rsp.data[0].b64_json;
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("lounge.png", image_bytes);
curl -s -D >(grep -i x-request-id >&2) \
  -o >(jq -r '.data[0].b64_json' | base64 --decode > lounge.png) \
  -X POST "https://www.vibeapi.cn/v1/images/edits" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F "model=gpt-image-2" \
  -F "mask=@mask.png" \
  -F "image[]=@sunlit_lounge.png" \
  -F 'prompt=A sunlit indoor lounge area with a pool containing a flamingo'

蒙版要求

待编辑的图片和蒙版必须是相同格式和尺寸(大小不超过 50MB)。

蒙版图片还必须包含 alpha 通道。如果你使用图片编辑工具创建蒙版,请确保保存时包含 alpha 通道。

你可以通过代码将黑白图片转换为带有 alpha 通道的蒙版:

from PIL import Image
from io import BytesIO

# 1. 将黑白蒙版加载为灰度图片
mask = Image.open(img_path_mask).convert("L")

# 2. 转换为 RGBA 以添加 alpha 通道
mask_rgba = mask.convert("RGBA")

# 3. 使用蒙版自身填充 alpha 通道
mask_rgba.putalpha(mask)

# 4. 将蒙版转换为字节
buf = BytesIO()
mask_rgba.save(buf, format="PNG")
mask_bytes = buf.getvalue()

# 5. 保存结果文件
img_path_mask_alpha = "mask_alpha.png"
with open(img_path_mask_alpha, "wb") as f:
    f.write(mask_bytes)

图片输入保真度

gpt-image-2 会自动以高保真度处理每张输入图片,无需设置 input_fidelity 参数。

由于 gpt-image-2 始终以高保真度处理图片输入,包含参考图片的编辑请求可能会使用更多输入 token。

自定义图片输出

你可以配置以下输出选项:

  • 尺寸(size):图片尺寸(例如 1024x10241024x1536
  • 质量(quality):渲染质量(例如 lowmediumhigh
  • 格式(format):文件输出格式
  • 压缩率(compression):JPEG 和 WebP 格式的压缩级别(0-100%)

sizequality 支持 auto 选项,模型会根据提示自动选择最佳选项。

gpt-image-2 不支持透明背景。不支持 background: "transparent" 的请求。

尺寸和质量选项

gpt-image-2size 参数中接受满足以下约束的任意分辨率。正方形图片通常生成速度最快。

常用尺寸:

  • 1024x1024(正方形)
  • 1536x1024(横向)
  • 1024x1536(纵向)
  • 2048x2048(2K 正方形)
  • 2048x1152(2K 横向)
  • 3840x2160(4K 横向)
  • 2160x3840(4K 纵向)
  • auto(默认)

尺寸约束:

  • 最大边长不超过 3840px
  • 两条边必须为 16px 的倍数
  • 长边与短边的比例不超过 3:1
  • 总像素数至少为 655,360,不超过 8,294,400

质量选项:

  • low — 快速草稿,适合缩略图和快速迭代
  • medium
  • high
  • auto(默认)

超过 2560x1440(总像素 3,686,400,通常称为 2K)的输出为实验性功能。

输出格式

Image API 返回 base64 编码的图片数据。默认格式为 png,也可以请求 jpegwebp

如果使用 jpegwebp,还可以指定 output_compression 参数来控制压缩级别(0-100%)。例如 output_compression=50 会将图片压缩 50%。

使用 jpegpng 更快,如果延迟是关注点,应优先使用此格式。

限制

  • 延迟:复杂提示的处理时间可能长达 2 分钟。
  • 文字渲染:虽然已显著改进,但模型在精确的文字放置和清晰度方面仍可能存在困难。
  • 一致性:模型偶尔可能难以在多次生成中保持重复角色或品牌元素的视觉一致性。
  • 构图控制:模型在结构化或布局敏感的构图中可能难以精确放置元素。

内容审核

所有提示和生成的图片均按照内容政策进行过滤。

你可以通过 moderation 参数控制审核严格程度:

  • auto(默认):标准过滤,限制创建某些可能不适合特定年龄段的内容。
  • low:较宽松的过滤。

费用参考

质量1024x10241024x15361536x1024
Low$0.006$0.005$0.005
Medium$0.053$0.041$0.041
High$0.211$0.165$0.165

费用 = 输入文本 token + 输入图片 token(编辑时)+ 图片输出 token。


Responses API 图片生成

已测试验证(2026-05-08):以下所有特性均通过 https://www.vibeapi.cn/v1/responses 实际测试验证。

Responses API 允许你通过文本模型(如 gpt-5.5)调用 image_generation 工具来生成图片。文本模型负责理解意图和优化提示词,内部调用 gpt-image-2 完成实际图片生成。

与 Image API 的主要区别:

Image APIResponses API
端点/v1/images/generations, /v1/images/edits/v1/responses
请求模型gpt-image-2 直接gpt-5.5(文本模型)
图片生成直接执行gpt-5.5 理解意图 → 调用 gpt-image-2
Prompt 优化自动优化(revised_prompt
计费仅图片 token文本 token + 图片 token(两份)
图片输入FormData 上传结构化 input_image(base64 data URI)
多轮编辑不支持支持(需流式)
一次多张不支持(n 参数被忽略)支持(在 prompt 中要求多张即可)

重要限制与要求

  • 必须使用流式模式stream: true)。非流式请求会返回错误或空数据。
  • instructions 字段为必填。不传会返回 400: Instructions are required
  • input 必须为数组格式。不接受纯字符串,必须使用 [{role: "user", content: [...]}] 格式,否则返回 400: Input must be a list
  • n 参数不可用(会被忽略或返回 400)。如需一次生成多张图,请在 prompt 中描述(见下方一次生成多张图片章节)。
  • 不支持 previous_response_id(仅 WebSocket v2 支持,HTTP 端点不可用)。

基本用法(流式)

import OpenAI from "openai";
import fs from "fs";

const openai = new OpenAI({
    baseURL: "https://www.vibeapi.cn/v1",
    apiKey: "YOUR_API_KEY",
});

const stream = await openai.responses.create({
    model: "gpt-5.5",
    instructions: "You are a helpful image generation assistant.",
    input: [
        {
            role: "user",
            content: [
                {
                    type: "input_text",
                    text: "Generate an image of a cat hugging an otter with an orange scarf",
                },
            ],
        },
    ],
    stream: true,
    tools: [{ type: "image_generation" }],
});

for await (const event of stream) {
    if (event.type === "response.image_generation_call.partial_image") {
        const imageBase64 = event.partial_image_b64;
        const imageBuffer = Buffer.from(imageBase64, "base64");
        fs.writeFileSync("output.png", imageBuffer);
    }
}
from openai import OpenAI
import base64

client = OpenAI(
    base_url="https://www.vibeapi.cn/v1",
    api_key="YOUR_API_KEY",
)

stream = client.responses.create(
    model="gpt-5.5",
    instructions="You are a helpful image generation assistant.",
    input=[
        {
            "role": "user",
            "content": [
                {
                    "type": "input_text",
                    "text": "Generate an image of a cat hugging an otter with an orange scarf",
                },
            ],
        },
    ],
    stream=True,
    tools=[{"type": "image_generation"}],
)

for event in stream:
    if event.type == "response.image_generation_call.partial_image":
        image_base64 = event.partial_image_b64
        image_bytes = base64.b64decode(image_base64)
        with open("output.png", "wb") as f:
            f.write(image_bytes)
curl -sN "https://www.vibeapi.cn/v1/responses" \
    -H "Authorization: Bearer $API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "model": "gpt-5.5",
        "instructions": "You are a helpful image generation assistant.",
        "input": [{"role": "user", "content": [{"type": "input_text", "text": "Generate an image of a cat hugging an otter"}]}],
        "stream": true,
        "tools": [{"type": "image_generation"}]
    }'

流式事件序列

完整的 SSE 事件流如下:

response.created                              # 请求创建
response.in_progress                          # 开始处理
response.output_item.added                    # image_generation_call 开始
response.image_generation_call.in_progress    # 图片生成中
response.image_generation_call.generating     # 正在生成
keepalive                                     # 心跳(长时间生成时出现)
response.image_generation_call.partial_image  # ★ 图片数据在这里(partial_image_b64)
response.output_item.done                     # ★ revised_prompt 在这里
response.completed                            # 完成

关键字段:

  • partial_image_b64 — base64 编码的完整图片数据
  • revised_prompt — 模型自动优化后的提示词(在 response.output_item.done 事件中)
  • size — 实际生成的图片尺寸
  • quality — 实际使用的质量级别

自定义输出参数

通过 tools 数组中的 image_generation 对象传递参数:

{
    "tools": [{
        "type": "image_generation",
        "quality": "low",
        "size": "1536x1024",
        "output_format": "webp",
        "output_compression": 50,
        "moderation": "low"
    }]
}
参数类型可选值说明
qualitystringlow / medium / high / auto图片质量
sizestring同 Image API 尺寸约束图片尺寸
output_formatstringpng / jpeg / webp输出格式
output_compressionnumber0-100压缩级别(jpeg/webp)
moderationstringauto / low内容审核级别
actionstringauto / generate / edit强制生成或编辑模式

图片输入(编辑模式)

通过结构化 input 传入图片,支持 base64 data URI:

const stream = await openai.responses.create({
    model: "gpt-5.5",
    instructions: "You are a helpful image generation assistant.",
    input: [
        {
            role: "user",
            content: [
                { type: "input_text", text: "Make this image look like a watercolor painting" },
                { type: "input_image", image_url: "data:image/png;base64,..." },
            ],
        },
    ],
    stream: true,
    tools: [{ type: "image_generation" }],
});
stream = client.responses.create(
    model="gpt-5.5",
    instructions="You are a helpful image generation assistant.",
    input=[
        {
            "role": "user",
            "content": [
                {"type": "input_text", "text": "Make this image look like a watercolor painting"},
                {"type": "input_image", "image_url": "data:image/png;base64,..."},
            ],
        },
    ],
    stream=True,
    tools=[{"type": "image_generation"}],
)

强制生成/编辑模式

使用 action 参数控制行为:

  • auto(默认):模型自动决定生成新图或编辑已有图片
  • generate:强制生成新图
  • edit:强制编辑模式(需要上下文中有图片,否则报错)
{
    "tools": [{ "type": "image_generation", "action": "generate" }]
}

一次生成多张图片

n 参数在 Responses API 中不可用。如需一次生成多张图片,在 prompt 中直接描述即可。模型会自动多次调用 image_generation 工具,每张图片独立生成和返回。

from openai import OpenAI
import base64

client = OpenAI(
    base_url="https://www.vibeapi.cn/v1",
    api_key="YOUR_API_KEY",
)

stream = client.responses.create(
    model="gpt-5.5",
    instructions="You are a helpful image generation assistant. Generate ALL images the user requests.",
    input=[
        {
            "role": "user",
            "content": [
                {
                    "type": "input_text",
                    "text": "Generate 3 different images: 1) a red circle 2) a blue square 3) a green triangle",
                },
            ],
        },
    ],
    stream=True,
    tools=[{"type": "image_generation", "quality": "low", "size": "1024x1024"}],
)

img_index = 0
for event in stream:
    if event.type == "response.image_generation_call.partial_image":
        image_bytes = base64.b64decode(event.partial_image_b64)
        with open(f"output_{img_index}.png", "wb") as f:
            f.write(image_bytes)
        img_index += 1

print(f"共生成 {img_index} 张图片")
import OpenAI from "openai";
import fs from "fs";

const openai = new OpenAI({
    baseURL: "https://www.vibeapi.cn/v1",
    apiKey: "YOUR_API_KEY",
});

const stream = await openai.responses.create({
    model: "gpt-5.5",
    instructions: "You are a helpful image generation assistant. Generate ALL images the user requests.",
    input: [
        {
            role: "user",
            content: [
                {
                    type: "input_text",
                    text: "Generate 3 different images: 1) a red circle 2) a blue square 3) a green triangle",
                },
            ],
        },
    ],
    stream: true,
    tools: [{ type: "image_generation", quality: "low", size: "1024x1024" }],
});

let imgIndex = 0;
for await (const event of stream) {
    if (event.type === "response.image_generation_call.partial_image") {
        const imageBuffer = Buffer.from(event.partial_image_b64, "base64");
        fs.writeFileSync(`output_${imgIndex}.png`, imageBuffer);
        imgIndex++;
    }
}

console.log(`共生成 ${imgIndex} 张图片`);

每张图片会触发一个独立的 image_generation_call,在流式事件中依次返回。实测 3 张图片约 30-45 秒完成。

费用说明

Responses API 会产生两部分费用

  1. 文本模型费用gpt-5.5):usage.input_tokens + usage.output_tokens,约 1600-2300 token/次
  2. 图片生成费用gpt-image-2):tool_usage.image_gen.output_tokens,与 Image API 相同

示例(1024x1024, quality=low):

  • 文本 token:约 1670 total
  • 图片 token:约 196 output
  • 总耗时:约 13-20 秒

已知限制(VibeAPI 网关)

特性状态说明
流式生成 stream: true可用唯一可靠的方式
非流式生成不可用返回 500 错误或空数据
instructions 字段必填不传会返回 400: Instructions are required
input 格式必须为数组不接受纯字符串,必须用 [{role, content}] 格式
n > 1 多图生成不可用参数被忽略或返回 400,改用 prompt 描述多张图
prompt 要求多张图可用在 prompt 中说明要生成几张图,模型自动多次调用 tool
previous_response_id不可用仅支持 WebSocket v2,HTTP 端点报错
partial_images 进度图未测试需要生成时间较长的图片才有效

与 OpenAI 官方文档的差异

最后更新:2026-05-08

项目OpenAI 官方VibeAPI
Responses API 模型gpt-5.5gpt-5.5(一致)
instructions可选必填
input 格式支持字符串和数组仅支持数组
n 参数(Image API)支持多张静默忽略,始终返回 1 张
n 参数(Responses API)支持不可用
非流式 Responses支持不可用
previous_response_id支持不可用
流式多张图(prompt)支持支持(一致)