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

图像生成

让模型生成或编辑图像。

您可以使用图像生成工具根据文本提示词生成图像,也可以同时提供图像作为输入。该工具使用 GPT Image 模型,包括 gpt-image-2.5-sunburst、gpt-image-2.5-flare、gpt-image-2、gpt-image-1.5 和 gpt-image-1-mini,并会自动优化输入文本,以改善生成效果。

将 image_generation 工具的 model 设为 gpt-image-2.5-sunburst 可精确编辑图像,设为 gpt-image-2.5-flare 可快速生成高质量图像。在 Responses 请求的顶层 model 字段中指定支持该工具的主模型。

gpt-image-1 已于 2026 年 10 月 23 日停止服务。有关迁移的详细信息,请参阅弃用公告。

要详细了解图像生成,请参阅专门的图像生成指南。

使用方法

在请求中加入 image_generation 工具后,模型可以根据您的提示词和随附的输入图像,决定在对话中何时生成图像以及如何生成。

image_generation_call 工具调用的结果会包含一张经过 base64 编码的图像。

如果您的应用面向个人终端用户,请在每个顶层 Responses 请求中传入一个稳定且能保护用户隐私的 safety_identifier,后续编辑请求也应包含该参数。该参数属于请求本身,不属于 image_generation 工具的选项。有关如何在两种图像 API 中使用该参数,请参阅图像安全标识符。

生成图像
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"}],
)

# 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))

您可以使用文件 ID 或 base64 数据提供输入图像。

要强制调用图像生成工具,您可以将参数 tool_choice 设为 {"type": "image_generation"}。

工具选项

您可以通过图像生成工具的参数配置以下输出选项:

  • 尺寸:图像的宽度和高度,例如 1024 × 1024 或 1024 × 1536
  • 质量:渲染质量,例如低、中或高
  • 格式:输出文件的格式
  • 压缩:JPEG 和 WebP 格式的压缩程度(0% 至 100%)
  • 背景:透明、不透明或自动选择
  • 操作:让模型自动选择,或指定生成图像或编辑图像

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

对于 gpt-image-2.5-sunburst 和 gpt-image-2.5-flare,quality 还支持 xhigh 和 max。较早的 GPT Image 模型不支持这两个值。默认质量仍为 auto。

gpt-image-2 支持灵活设置 size,只要取值符合其分辨率限制即可。透明背景功能目前处于预览阶段。要使用透明背景,请设置 background: "transparent"。请选择 png(默认)或 webp 格式,jpeg 不支持透明背景。

有关可用选项的详细信息,请参阅图像生成指南。

使用 Responses API 的图像生成工具时,支持该工具的 GPT Image 模型可以自行决定是生成新图像,还是编辑对话中已有的图像。可选参数 action 用于控制此行为。将 action 保持为 auto,模型就会自行选择生成或编辑。您也可以将其设为 generate 或 edit,强制模型执行相应操作。如果未指定,默认值为 auto。

修订后的提示词

使用图像生成工具时,主模型(例如 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": "..."
}

提示词编写技巧

在提示词中使用 draw(绘制)或 edit(编辑)等词,图像生成效果最佳。

例如,如果您想合并图像,可以把 combine(组合)或 merge(合并)这样的笼统表述换成更具体的提示词,例如“编辑第一张图像,将第二张图像中的这个元素添加进去”。

多轮编辑

您可以引用之前的响应 ID 或图像 ID 来反复编辑图像,在多轮对话中逐步调整图像。

多轮图像生成
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))

流式传输

图像生成工具可以在生成最终图像的过程中,以流式方式返回中间图像,让用户更快看到生成进展,缩短用户感受到的等待时间。

您可以使用 partial_images 参数设置中间图像的数量(1 至 3 张)。

以流式方式返回图像
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])

支持的模型

以下模型尚未弃用,文档已明确说明它们支持 Responses API 的图像生成工具。请在顶层 model 字段中使用这些模型的 ID。有关可用性和访问要求,请参阅各模型的页面。

有关模型可用性和停止服务的日期,请参阅弃用公告。