如需查看完整的文档索引,请参阅 llms.txt。在文档页面的 URL 末尾加上 .md 即可查看该页面的 Markdown 版本。
主导航
主题

图像生成

了解如何生成或编辑图像。

概览

您可以通过 API 使用 gpt-image-2.5-sunburst 和 gpt-image-2.5-flare,根据文本提示词生成和编辑图像。如果工作流程最看重编辑精度,请选择 Sunburst。如果日常使用时需要快速生成高质量图像,请选择 Flare。您可以通过以下两种 API 使用图像生成功能:

Image API

Image API 提供两个端点,各有不同用途:

  • 生成:根据文本提示词从头生成图像。
  • 编辑:使用新的提示词修改现有图像,可以修改局部,也可以修改整张图像。

Responses API

Responses API 允许您在对话或多步骤流程中生成图像。它提供图像生成内置工具,支持在上下文中使用图像输入和输出。

与 Image API 相比,Responses API 还支持:

  • 多轮编辑:通过提示词对图像进行多轮高保真编辑。
  • 灵活输入:除了传入图像字节数据,还可以通过图像的文件 ID 指定输入图像。

有关哪些主模型可以调用图像生成工具,请参阅支持的模型。

选择合适的 API

  • 如果您只需要根据一条提示词生成或编辑一张图像,Image API 最合适。
  • 如果您希望使用 GPT Image 构建可通过对话生成和编辑图像的应用,请选择 Responses API。

使用 Image API 时,直接将 model 设置为 gpt-image-2.5-sunburst 或 gpt-image-2.5-flare。使用 Responses API 时,在请求的顶层选择受支持的主模型,并将 gpt-image-2.5-sunburst 或 gpt-image-2.5-flare 填入图像生成工具的 model 字段。

两种 API 都允许您通过调整质量、尺寸、格式和压缩程度来自定义输出。

为确保负责任地使用这些模型,您在使用 GPT Image 模型之前,可能需要完成 API 组织验证。您可以在开发者控制台中完成验证。

GPT-Image-2.6

GPT-Image-2.6 包括适合精细视觉创作的 Sunburst 和适合快速迭代的 Flare。通过 Image API 使用这两个模型时,都可以调整复杂度、用标注引导编辑,以及输出 SVG 图像和 GIF 动画。您可以查看 Sunburst 示例及各示例使用的生成设置。

设置图像复杂度

使用请求字段 complexity 控制图像模型在渲染前进行规划的程度。更换背景或修改标牌等目标明确的编辑任务适合使用较低的复杂度。如果画面中文字密集、布局复杂,或需要表现物体之间的关系,较高的复杂度可能有帮助。提高复杂度也可能增加延迟,或引入不希望出现的改动,请结合具体任务比较不同设置的效果。

Sunburst 和 Flare 支持的复杂度等级为 low、medium 和 high。如果省略 complexity,两个模型均使用 high。渲染质量由 quality 单独控制。

调整复杂度不会单独收费。

下图是 Sunburst 在中等质量设置下根据同一条提示词生成的剪纸风格足球场景。请比较不同复杂度下奖杯、球衣以及“CHAMPIONS”标牌位置的变化。

低复杂度

中等复杂度

高复杂度

安全标识符

如果您的应用面向个人终端用户,请提供稳定且能保护隐私的 safety_identifier,并在每个图像生成或图像编辑请求中传入该标识符。请对用户名或电子邮件地址进行哈希处理,避免直接发送可识别用户身份的信息,并将标识符长度控制在 128 个字符以内。建议提供安全标识符,但并非必须。

Images API 的 user 参数已弃用。请改用 safety_identifier 发送同一标识符。API 仍支持使用 user 的现有请求。非空的 safety_identifier 优先于 user。如果省略 safety_identifier,或其值为 null 或空字符串,API 将使用请求中提供的 user。

木桌上的米色咖啡杯

生成图像

您可以使用图像生成端点根据文本提示词创建图像,也可以使用 Responses API 中的图像生成工具在对话中生成图像。

要了解如何自定义输出的尺寸、质量、格式和压缩程度,请参阅下文的自定义图像输出。

您可以设置 n 参数,在一次请求中生成多张图像。默认情况下,API 返回一张图像。

生成图像
from openai import OpenAI
import base64

client = OpenAI()

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.5-sunburst", prompt=prompt)

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

# Save the image to a file
with open("otter.png", "wb") as f:
    f.write(image_bytes)

多轮图像生成

使用 Responses API 时,您可以在上下文中提供图像生成调用的输出(也可以只提供图像 ID),或者使用 previous_response_id 参数,以此构建包含图像生成的多轮对话。 随着对话推进,您可以逐轮调整提示词、给出新指令,逐步修改图像效果。

使用 Responses API 的图像生成工具时,受支持的工具模型可以选择生成新图像,也可以编辑对话中已有的图像。可选的 action 参数用于控制这一行为:保留 action: "auto",由模型决定;设置 action: "generate",始终创建新图像;或设置 action: "edit",在上下文中有图像时强制编辑。

使用 action 强制生成新图像
from openai import OpenAI
import base64

client = OpenAI()

response = client.responses.create(
    model="gpt-6.2-astra",
    input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
    tools=[
        {"type": "image_generation", "model": "gpt-image-2.5-sunburst", "action": "generate"}
    ],
)

# Save the image to a file
image_data = [
    output.result
    for output in response.output
    if output.type == "image_generation_call"
]

if image_data:
    image_base64 = image_data[0]
    with open("otter.png", "wb") as f:
        f.write(base64.b64decode(image_base64))

如果您强制执行 edit,却未在上下文中提供图像,调用将返回错误。将 action 保持为 auto,即可让模型决定何时生成或编辑图像。

多轮图像生成
from openai import OpenAI
import base64

client = OpenAI()

response = client.responses.create(
    model="gpt-6.2-astra",
    input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
    tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)

image_data = [
    output.result
    for output in response.output
    if output.type == "image_generation_call"
]

if image_data:
    image_base64 = image_data[0]

    with open("cat_and_otter.png", "wb") as f:
        f.write(base64.b64decode(image_base64))


# Follow up

response_fwup = client.responses.create(
    model="gpt-6.2-astra",
    previous_response_id=response.id,
    input="Now make it look realistic",
    tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)

image_data_fwup = [
    output.result
    for output in response_fwup.output
    if output.type == "image_generation_call"
]

if image_data_fwup:
    image_base64 = image_data_fwup[0]
    with open("cat_and_otter_realistic.png", "wb") as f:
        f.write(base64.b64decode(image_base64))

结果

“生成一张图像,画面中一只灰色虎斑猫正抱着一只围着橙色围巾的水獭。”

一只猫和一只水獭

“现在把画面改得逼真一些。”

一只猫和一只水獭

流式传输

Responses API 和 Image API 都支持流式图像生成。您可以在 API 生成图像的过程中,以流式方式接收中间图像,让用户实时看到生成进度。

您可以调整 partial_images 参数,接收 0 至 3 张中间图像。

  • 如果将 partial_images 设置为 0,您只会收到最终图像。
  • 如果设置的值大于 0,而完整图像生成得较快,您收到的中间图像可能少于请求的数量。
流式接收图像
from openai import OpenAI
import base64

client = OpenAI()


def save_base64_image(filename, image_base64):
    image_bytes = base64.b64decode(image_base64)
    with open(filename, "wb") as f:
        f.write(image_bytes)


stream = client.responses.create(
    model="gpt-6.2-astra",
    input="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
    stream=True,
    tools=[
        {"type": "image_generation", "model": "gpt-image-2.5-sunburst", "partial_images": 2}
    ],
)

for event in stream:
    if event.type == "response.image_generation_call.partial_image":
        idx = event.partial_image_index
        save_base64_image(f"river-partial-{idx}.png", event.partial_image_b64)
    elif event.type == "response.completed":
        image_data = [
            output.result
            for output in event.response.output
            if output.type == "image_generation_call"
        ]

        if image_data:
            save_base64_image("river-final.png", image_data[0])

结果

中间图像 1 中间图像 2 最终图像
第一张中间图像 第二张中间图像 最终图像

提示词:画一幅美丽的冬日风景,一条由白色猫头鹰羽毛汇成的河流蜿蜒穿过宁静的原野。

改写后的提示词

在 Responses API 中使用图像生成工具时,主模型(例如 gpt-5.5)会自动改写您的提示词,以改善生成效果。

您可以在图像生成调用的 revised_prompt 字段中查看改写后的提示词:

包含改写后提示词的响应
{
  "id": "ig_123",
  "type": "image_generation_call",
  "status": "completed",
  "revised_prompt": "A gray tabby cat hugging an otter. The otter is wearing an orange scarf. Both animals are cute and friendly, depicted in a warm, heartwarming style.",
  "result": "..."
}

编辑图像

您可以使用图像编辑端点:

  • 编辑现有图像。
  • 参考其他图像生成新图像。
  • 上传图像和标明待替换区域的蒙版,编辑图像的局部区域。

使用参考图像创建新图像

您可以使用一张或多张图像作为参考,生成一张新图像。

本例使用 4 张输入图像生成一张礼篮图像,篮中装有参考图像里的物品。

身体乳香皂熏香套装泡澡球
沐浴礼品套装

使用 Responses API 时,您可以通过以下三种方式提供输入图像:

  • 提供图像的完整 URL
  • 以 Base64 编码的数据 URL 形式提供图像
  • 提供文件 ID(使用 Files API 创建文件时获得)

创建文件

创建文件
from openai import OpenAI

client = OpenAI()


def create_file(file_path):
    with open(file_path, "rb") as file_content:
        result = client.files.create(
            file=file_content,
            purpose="vision",
        )
        return result.id

创建 Base64 编码的图像

创建 Base64 编码的图像
import base64


def encode_image(file_path):
    with open(file_path, "rb") as f:
        base64_image = base64.b64encode(f.read()).decode("utf-8")
    return base64_image
编辑图像
from openai import OpenAI
import base64

client = OpenAI()


def encode_image(file_path):
    with open(file_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


def create_file(file_path):
    with open(file_path, "rb") as file_content:
        result = client.files.create(file=file_content, purpose="vision")
    return result.id


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."""

base64_image1 = encode_image("body-lotion.png")
base64_image2 = encode_image("soap.png")
file_id1 = create_file("bath-bomb.png")
file_id2 = create_file("incense-kit.png")

response = client.responses.create(
    model="gpt-6.2-astra",
    input=[
        {
            "role": "user",
            "content": [
                {"type": "input_text", "text": prompt},
                {
                    "type": "input_image",
                    "image_url": f"data:image/png;base64,{base64_image1}",
                },
                {
                    "type": "input_image",
                    "image_url": f"data:image/png;base64,{base64_image2}",
                },
                {
                    "type": "input_image",
                    "file_id": file_id1,
                },
                {
                    "type": "input_image",
                    "file_id": file_id2,
                },
            ],
        }
    ],
    tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)

image_generation_calls = [
    output for output in response.output if output.type == "image_generation_call"
]

image_data = [output.result for output in image_generation_calls]

if image_data:
    image_base64 = image_data[0]
    with open("gift-basket.png", "wb") as f:
        f.write(base64.b64decode(image_base64))
else:
    print(response.output_text)

使用标注引导编辑

使用支持标注引导编辑的 GPT-Image-2.5 或 GPT-Image-2.6 模型进行此类编辑时,请使用 Image API 的 POST /v1/images/edits 端点。如果使用 Responses API 进行局部编辑,请使用蒙版。用标注图层指出需要修改的位置,并在 prompt 中说明要如何修改。

例如,将厨房照片和透明叠加图层下载到同一目录。图层上的圆圈标出了要改颜色的花瓶。

使用 Image API 编辑圈出的花瓶
import base64
from pathlib import Path

from openai import OpenAI

client = OpenAI()
overlay = Path("markup-vase-overlay-gpt-image-2-6.png")
with open("kitchen.webp", "rb") as image, overlay.open("rb") as markup:
    result = client.images.edit(
        model="gpt-image-2.6-flare",
        image=("kitchen.webp", image, "image/webp"),
        markup=(overlay.name, markup, "image/png"),
        quality="medium",
        prompt=(
            "Recolor the striped vase inside the red circle to solid cobalt blue ceramic. "
            "Keep its shape, the dried stems, and the rest of the kitchen unchanged. "
            "Remove the red circle from the finished photo."
        ),
    )

Path("vase.png").write_bytes(base64.b64decode(result.data[0].b64_json))

叠加标注后的照片

Flare 的输出结果

  1. 将原图作为第一张输入图像。
  2. 单独创建一张透明 PNG 图像,在其中画上圆圈、草图或其他标注。校正方向后,标注图层的宽和高必须分别与第一张输入图像相同。PNG 必须包含透明像素,请勿将标注图层与原图合并。
  3. 通过 markup 字段发送标注图层。使用 multipart 请求时,可以直接上传文件。使用 JSON 请求时,传入一个对象,其中必须包含 image_url 或 file_id 之一,且只能包含其中一个。image_url 可以是 HTTPS URL 或 Base64 数据 URL。对于 JSON 格式的编辑请求,请在 images 中传入输入图像数组。
  4. 说明要做哪些修改,以及哪些细节需要保留。使用结果前,请先检查编辑效果。

标注作用于第一张输入图像,其余图像用作参考。第一张输入图像和标注图层各自最多可包含 16,777,216 个像素。标注文件还不得超过 50 MB。请勿在同一请求中同时使用 markup 和 mask。

使用蒙版编辑图像

您可以提供蒙版,标明需要编辑的图像区域。

在 GPT Image 中使用蒙版时,系统会向模型发送额外的指令,引导模型根据蒙版进行编辑。

GPT Image 完全通过提示词来执行蒙版编辑。模型会参考蒙版,但不一定能精确遵循蒙版的轮廓。

如果您提供多张输入图像,蒙版将应用于第一张图像。

使用蒙版编辑图像
from openai import OpenAI
import base64

client = OpenAI()


def create_file(file_path):
    with open(file_path, "rb") as file_content:
        result = client.files.create(file=file_content, purpose="vision")
    return result.id


fileId = create_file("sunlit_lounge.png")
maskId = create_file("mask.png")

response = client.responses.create(
    model="gpt-6.2-astra",
    input=[
        {
            "role": "user",
            "content": [
                {
                    "type": "input_text",
                    "text": "generate an image of the same sunlit indoor lounge area with a pool but the pool should contain a flamingo",
                },
                {
                    "type": "input_image",
                    "file_id": fileId,
                },
            ],
        },
    ],
    tools=[
        {
            "type": "image_generation",
            "model": "gpt-image-2.5-sunburst",
            "quality": "high",
            "input_image_mask": {
                "file_id": maskId,
            },
        },
    ],
)

image_data = [
    output.result
    for output in response.output
    if output.type == "image_generation_call"
]

if image_data:
    image_base64 = image_data[0]
    with open("lounge.png", "wb") as f:
        f.write(base64.b64decode(image_base64))
图像 蒙版 输出
带泳池的粉色房间 覆盖泳池局部区域的蒙版 原图中的泳池,蒙版区域已替换为一只充气火烈鸟

提示词:阳光洒进室内休息区,里面有一座泳池,池中有一只火烈鸟。

蒙版要求

待编辑图像与蒙版必须具有相同的格式和尺寸,且每个文件都必须小于 50 MB。

蒙版图像还必须包含 Alpha 通道。如果您使用图像编辑工具创建蒙版,请确保保存时保留 Alpha 通道。

您也可以通过程序为黑白图像添加 Alpha 通道。

为黑白蒙版添加 Alpha 通道
from PIL import Image
from io import BytesIO

# 1. Load your black & white mask as a grayscale image
mask = Image.open("mask.png").convert("L")

# 2. Convert it to RGBA so it has space for an alpha channel
mask_rgba = mask.convert("RGBA")

# 3. Then use the mask itself to fill that alpha channel
mask_rgba.putalpha(mask)

# 4. Convert the mask into bytes
buf = BytesIO()
mask_rgba.save(buf, format="PNG")
mask_bytes = buf.getvalue()

# 5. Save the resulting file
img_path_mask_alpha = "mask_alpha.png"
with open(img_path_mask_alpha, "wb") as f:
    f.write(mask_bytes)

自定义图像输出

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

  • 尺寸:图像的宽和高(例如 1024x1024、1024x1536)。
  • 质量:渲染质量(例如 low、medium、high)。
  • 格式:输出文件的格式。
  • 压缩:JPEG 和 WebP 格式的压缩程度(0% 至 100%)。
  • 背景:透明、不透明或自动选择。

size、quality 和 background 均支持 auto 选项。使用该选项时,模型会根据提示词自动选择最合适的设置。

尺寸和质量选项

GPT-Image-2.6 和 GPT-Image-2.5 两个系列中的 Sunburst 和 Flare 模型均支持 xhigh 和 max 两档质量设置,默认值为 auto。早期 GPT Image 模型支持的最高质量设置为 high。

设置 选项
推荐尺寸 1024x1024(正方形)、1536x1024(横向)、1024x1536(纵向)
质量 low、medium、high、xhigh、max、auto

Sunburst 和 Flare 还支持以 WIDTHxHEIGHT 格式的字符串指定自定义尺寸,例如 1536x864。宽和高都必须是 16 的倍数,宽高比必须介于 1:3 和 3:1 之间,且任一边长均不得超过 3840 像素。总像素数必须介于 655,360 和 8,294,400(4K)之间。对于 GPT-Image-2.5,超过 2560x1440 的分辨率属于实验性功能。

要生成透明背景,请设置 background: "transparent",并使用 output_format: "png" 或 "webp"。

要快速生成草图,请使用 quality: "low"。制作最终素材时,请比较较高质量设置的效果,权衡细节、延迟和成本。

输出格式

Image API 返回经过 Base64 编码的图像数据。 默认格式为 png,您也可以请求 jpeg 或 webp 格式。

使用 jpeg 或 webp 时,您还可以通过 output_compression 参数控制压缩程度(0% 至 100%)。例如,output_compression=50 会将图像压缩 50%。

使用 jpeg 比使用 png 更快。如果您对延迟有要求,请优先选择 JPEG 格式。

生成 SVG 图像

要输出 SVG,请设置 output_format: "svg",并将请求发送到 POST /v1/images/generations。如果需要编辑图标、插画等图形的路径、形状和颜色,可以使用 SVG。

将 n 设置为 1。SVG 输出不支持流式传输、中间图像或 output_compression。图像编辑端点不支持输出 SVG,也不接受 SVG 作为编辑所需的栅格图像输入。

API 会先创建参考图像,再根据该图像生成 SVG。quality 设置控制参考图像的质量。对 data[0].b64_json 进行 Base64 解码,将得到的 UTF-8 编码的 XML 字节保存为扩展名为 .svg 的文件。返回的文档包含静态矢量图形,不支持脚本、外部资源或嵌入的栅格图像。

以下是 Flare 和 Sunburst 根据同一条提示词为 Field & Flour 面包店生成的 SVG 标志。

GPT-Image-2.6 Flare

GPT-Image-2.6 Sunburst

生成 GIF 动画

要输出 GIF,请设置 output_format: "gif",并将请求发送到 POST /v1/images/generations 或 POST /v1/images/edits。在提示词中描述动画主体、动作以及希望动画如何循环播放。如果要用静态参考图像引导动画生成,请将这些图像发送到图像编辑端点。

无论使用哪个端点,都可以通过以下请求字段配置 GIF 输出:

字段 取值 默认值
frame_count 2 至 16 帧 9
frame_duration_ms 每帧 20 至 2000 毫秒,按最接近的 10 毫秒取整 200
size auto,或使用 WIDTHxHEIGHT 指定尺寸,宽和高都必须是 16 的正整数倍 auto

size 指定输出动画中每一帧的尺寸。帧数、每帧尺寸和动画数量必须同时满足模型的尺寸限制和单次请求的总像素上限。如果请求超出限制,请减少 frame_count、缩小每帧尺寸或减小 n。使用 auto 可让 API 在模型允许的范围内选择每帧尺寸。

响应中包含生成好的动画。对每个结果中的 b64_json 值进行 Base64 解码,将得到的字节保存为扩展名为 .gif 的文件。GIF 输出不支持流式传输、中间图像或自定义 output_compression。frame_count 和 frame_duration_ms 仅适用于 GIF 输出。

Sunburst 使用 frame_count=9 和 frame_duration_ms=200 生成了这段提灯动画。1.8 秒后,猴子回到起始姿势。

局限性

GPT Image 模型功能强大,适用于多种图像生成任务,但仍有以下局限性:

  • 延迟: 处理复杂提示词可能需要长达 2 分钟。
  • 文字渲染: 虽然效果已有明显改善,但模型有时仍难以准确放置文字或保证文字清晰。
  • 一致性: 模型可以生成风格一致的图像,但在多次生成时,反复出现的角色或品牌元素偶尔会有视觉差异。
  • 构图控制: 模型遵循指令的能力已有改善,但对于结构严谨或对布局要求较高的构图,仍可能难以将元素放在准确的位置。

内容审核

所有提示词和生成的图像都会按照我们的内容政策进行过滤。

使用 GPT Image 模型生成图像时,您可以通过 moderation 参数控制审核的严格程度。此参数支持两个值:

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

处理被拦截的请求和其他错误

图像生成失败时,请按处理其他 API 错误的方式排查:检查 HTTP 状态码或 SDK 异常类型,记录请求 ID,并参阅错误代码指南排查身份验证、配额、速率限制和服务器问题。遇到暂时性的速率限制或服务器故障时,请采用退避策略重试。对于配额错误,或需要修改请求才能解决的图像生成用户错误,请勿自动重试。

部分图像生成错误可以由用户修正,此时 API 可能返回 error.type = "image_generation_user_error"。遇到此类错误时,请勿在未修改提示词或输入图像的情况下自动重试。在程序中处理错误时,请使用 error.code 作为区分错误的稳定标识。

当 error.code = "moderation_blocked" 时,错误响应还可能包含可选的 error.moderation_details 对象:

{
  "error": {
    "type": "image_generation_user_error",
    "code": "moderation_blocked",
    "moderation_details": {
      "moderation_stage": "input",
      "categories": ["harassment"]
    }
  }
}

moderation_details 对象提供概略的调试信息,不会披露内部分类器的标签或评分。

moderation_stage 的可能值如下:

  • input:提示词或请求输入触发了拦截。
  • output:生成的图像触发了拦截,或下游输出审核环节进行了拦截。
  • unknown:难以确定拦截来源时使用的回退值,较为少见。

categories 包含公开的粗粒度分类标签,例如 harassment、self-harm、sexual 或 violence。

对于大多数应用,面向终端用户的主要提示应保持通用。您可以将 moderation_details 用于开发者日志、支持流程和分析,也可以据此简要提示用户如何调整输入。

处理内容审核拦截导致的图像生成错误
import OpenAI from "openai";

const openai = new OpenAI();

try {
  // The same error handling pattern applies to image generation requests,
  // image edits, and Responses API tool calls that generate images.
  await openai.images.generate({
    model: "gpt-image-2.5-sunburst",
    prompt: "Create a poster humiliating my coworker with insulting captions",
  });
} catch (error) {
  if (
    !(error instanceof OpenAI.APIError) ||
    error.code !== "moderation_blocked"
  ) {
    throw error;
  }

  const moderationDetails =
    error.error && "moderation_details" in error.error
      ? (error.error.moderation_details as {
          categories?: string[];
          moderation_stage?: string;
        })
      : undefined;
  const categories = moderationDetails?.categories ?? [];
  const stage = moderationDetails?.moderation_stage;

  let hint =
    "This request could not be completed because it did not meet safety requirements.";

  if (categories.includes("harassment")) {
    hint =
      "Try removing abusive or targeting language and focus on neutral visual details instead.";
  } else if (stage === "input") {
    hint =
      "Try revising the prompt or input images and submit the request again.";
  } else if (stage === "output") {
    hint =
      "The generated result was blocked by a safety check. Try changing the prompt and generating again.";
  }

  console.error("Image generation blocked", {
    request_id: error?.requestID,
    code: error?.code,
    moderation_details: moderationDetails,
  });

  console.log(hint);
}

支持的模型

在 Responses API 中生成图像时,gpt-5 及更新的模型应支持图像生成工具。请查看所选模型的详情页,确认该模型是否可以使用图像生成工具。

成本与延迟

GPT-Image-2.6 的费用

图像输出按每百万计费 Token 30 美元收费。您可以使用计算器估算单张图像的输出费用。

有关文本和图像每百万 Token 的价格,请参阅图像生成定价。

使用标注引导编辑时,Token 单价与其他编辑请求相同,不额外收费。输出 SVG 时,生成参考图像的费用按图像模型的单价计算,编写 SVG 所用的 Token 按负责编写的模型的单价计算。总费用取决于两个模型各自的 Token 用量。

GPT-Image-2.5 的费用

使用 Responses API 时,除图像生成费用外,请求还会产生主模型的 Token 用量费用。

两个 GPT-Image-2.5 模型的 Token 单价相同。按每百万 Token 计费,图像输入为 8 美元,缓存的图像输入为 2 美元,图像输出为 30 美元,文本输入为 5 美元,缓存的文本输入为 1.25 美元。请参阅定价。

您可以通过响应中的 usage 查看在所用提示词、尺寸和质量设置下的 Token 用量。即使 Token 单价相同,生成每张图像的成本也可能不同,因为 Token 用量会随模型和质量设置而变化。有关旧版模型的定价示例,请参阅早期 GPT Image 模型。

缓存输入定价

对于 GPT-Image-2 和 GPT-Image-2.5,缓存输入定价仅适用于 Responses API 中的图像生成工具。直接向 Images API 发送的请求(包括 /v1/images/edits 请求)不适用此定价。

图像生成输入命中缓存时,系统会按缓存价格计费,但 Responses API 的输出不包含这些缓存输入的 Token 数量,因此无法通过响应中的 usage 确认这些缓存命中情况。

计算成本

选择模型、质量和尺寸,即可估算输出 Token 数量及图像输出费用。 GPT-Image-2.6 和 GPT-Image-2.5 两个系列中的 Sunburst 和 Flare 模型均支持 low、medium、high、xhigh 和 max 五档质量设置。 对于 gpt-image-2,可选质量为 low、medium 和 high。 这些模型的图像输出 Token 单价相同,但在相同质量设置下,消耗的 Token 数量可能不同。 估算时请指定具体的质量和尺寸值。使用 auto 时,Token 数量取决于实际生成的图像。

模型
质量
输出 Token 数
823
预计图像输出费用
US$0.02469

每张图像的费用按每百万个图像输出 Token 30 美元计算。此估算不含文本和图像输入 Token 的费用,也不含流式传输中间图像的费用。

中间图像费用

流式生成图像时,如果您使用 partial_images 参数,每张中间图像都会额外消耗 100 个图像输出 Token。

早期 GPT Image 模型

以下说明适用于早期模型,不适用于 Sunburst 或 Flare。对于新接入的应用,请使用 GPT-Image-2.6 Sunburst 或 Flare。

GPT-Image-2 的设置与输入保真度

只要满足以下约束,gpt-image-2 的 size 参数就接受任意分辨率。正方形图像通常生成得最快。

常用尺寸
  • 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(默认)

图像输入保真度

input_fidelity 参数控制模型在编辑图像或使用参考图像时保留输入图像细节的程度。对于 gpt-image-2,请省略此参数。该模型会自动以高保真度处理每张输入图像,因此 API 不允许更改此设置。

由于 gpt-image-2 始终以高保真度处理输入图像,包含参考图像的编辑请求可能消耗更多图像输入 Token。有关这对成本的影响,请参阅视觉输入费用。

旧版模型定价示例

早于 gpt-image-2 的模型

在生成图像时,早于 gpt-image-2 的 GPT Image 模型会先生成专用的图像 Token。延迟和最终成本均与渲染图像所需的 Token 数量成正比。图像尺寸越大、质量设置越高,所需的 Token 就越多。

生成的 Token 数量取决于图像尺寸和质量:

质量 正方形(1024×1024) 纵向(1024×1536) 横向(1536×1024)
低 272 个 Token 408 个 Token 400 个 Token
中 1056 个 Token 1584 个 Token 1568 个 Token
高 4160 个 Token 6240 个 Token 6208 个 Token

您还需要计入输入 Token,包括提示词的文本 Token,以及编辑图像时所用的图像输入 Token。 由于 gpt-image-2 始终以高保真度处理输入图像,包含参考图像的编辑请求可能消耗更多输入 Token。

请参阅定价页面了解当前文本和图像 Token 的价格,并按计算成本一节的方法估算图像输出费用。

最终成本为以下各项费用之和:

  • 文本输入 Token 的费用
  • 图像输入 Token 的费用(使用编辑端点时)
  • 图像输出 Token 的费用

历史输出费用

gpt-image-2 支持数千种分辨率。为便于比较,下表列出了早期 GPT Image 模型所用的尺寸。表中还列出了 GPT Image 1.5、GPT Image 1 和 GPT Image 1 mini 以往按张计费的图像输出价格。估算请求总成本时,仍需计入文本和图像输入 Token 的费用。

gpt-image-1 已于 2026 年 10 月 23 日停止服务。下表保留了其价格,供历史对比。有关替代模型,请参阅弃用通知。

在相同质量设置下,分辨率较高的非正方形图像有时比分辨率较低的图像或正方形图像产生更少的输出 Token。

模型

质量

1024 × 10241024 × 15361536 × 1024

GPT-Image-2


还支持其他尺寸
低$0.006$0.005$0.005
中$0.053$0.041$0.041
高$0.211$0.165$0.165

GPT Image 1.5

低$0.009$0.013$0.013
中$0.034$0.05$0.05
高$0.133$0.2$0.2

GPT Image 1

低$0.011$0.016$0.016
中$0.042$0.063$0.063
高$0.167$0.25$0.25

GPT Image 1 mini

低$0.005$0.006$0.006
中$0.011$0.015$0.015
高$0.036$0.052$0.052