主导航

遗留 API

图像生成

允许模型生成或编辑图像。

图像生成工具允许您使用文本提示词以及可选的图像输入来生成图像。它使用 GPT 图像模型,包括 gpt-image-2gpt-image-1.5gpt-image-1gpt-image-1-mini,并自动优化文本输入以提高性能。

要了解有关图像生成的更多信息,请参阅我们的专门图像生成指南

用量

当您在请求中包含 image_generation 工具时,模型可以根据您的提示词和提供的任何图像输入,决定在对话中何时以及如何生成图像。

image_generation_call 工具调用结果将包含一个 base64 编码的图像。

生成图像
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from openai import OpenAI
import base64

client = OpenAI() 

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

# 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"}

工具选项

您可以为图像生成工具配置以下输出选项作为参数

  • 尺寸 (Size):图像维度,例如 1024 × 1024 或 1024 × 1536
  • 质量 (Quality):渲染质量,例如 low (低)、medium (中) 或 high (高)
  • 格式 (Format):文件输出格式
  • 压缩 (Compression):JPEG 和 WebP 格式的压缩率 (0-100%)
  • 背景 (Background):透明或不透明
  • 操作 (Action):请求应自动选择、生成还是编辑图像

sizequalitybackground 支持 auto 选项,在此选项下模型将根据提示词自动选择最佳选项。

gpt-image-2 支持满足其分辨率限制的灵活 size 值。它目前不支持透明背景,因此带有 background: "transparent" 的请求会失败。

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

使用 Responses API 图像生成工具时,支持的 GPT 图像模型可以选择是生成新图像还是编辑对话中已有的图像。可选的 action 参数控制此行为:将 action 保持设置为 auto 以便模型选择生成或编辑,或者将其设置为 generateedit 以强制执行该行为。如果未指定,默认值为 auto

修改后的提示词

使用图像生成工具时,主线模型(例如 gpt-5.5)将自动修改您的提示词以提高性能。

您可以在图像生成调用的 revised_prompt 字段中访问修改后的提示词

1
2
3
4
5
6
7
{
  "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 来迭代地编辑图像。这允许您跨对话轮次优化图像。

多轮图像生成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from openai import OpenAI
import base64

client = OpenAI()

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

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-5.5",
    previous_response_id=response.id,
    input="Now make it look realistic",
    tools=[{"type": "image_generation"}],
)

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

流式传输图像
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from openai import OpenAI
import base64

client = OpenAI()

stream = client.images.generate(
    prompt="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
    model="gpt-image-2",
    stream=True,
    partial_images=2,
)

for event in stream:
    if event.type == "image_generation.partial_image":
        idx = event.partial_image_index
        image_base64 = event.b64_json
        image_bytes = base64.b64decode(image_base64)
        with open(f"river{idx}.png", "wb") as f:
            f.write(image_bytes)

支持的模型

以下模型支持图像生成工具

  • gpt-5.5
  • gpt-5.4-mini
  • gpt-5.4-nano
  • gpt-5.2
  • gpt-5
  • gpt-5-nano
  • o3
  • gpt-4.1
  • gpt-4.1-mini
  • gpt-4.1-nano
  • gpt-4o
  • gpt-4o-mini

用于图像生成过程的模型始终是 GPT 图像模型,包括 gpt-image-2gpt-image-1.5gpt-image-1gpt-image-1-mini,但这些模型在 Responses API 的 model 字段中不是有效值。请将支持文本的主线模型(例如 gpt-5.5gpt-5)与托管的 image_generation 工具配合使用。

© . This website operates independently and is not affiliated with or endorsed by OpenAI, Inc. All brand names, logos, and trademarks are the property of their respective owners.