图像生成工具允许您使用文本提示词以及可选的输入图像来生成图像。它使用 GPT 图像模型,包括 gpt-image-2, gpt-image-1.5, gpt-image-1,且 gpt-image-1-mini,并自动优化文本输入以提升性能。
要了解更多关于图像生成的信息,请参阅我们专门的 图像生成指南.
用量
当您在请求中包含 image_generation 工具时,模型可以根据您的提示词和任何提供的输入图像,决定在对话过程中何时以及如何生成图像。
The 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 to {"type": "image_generation"}.
工具选项
您可以将以下输出选项配置为 将图像生成作为对话的一部分。:
- 的参数:尺寸:图像尺寸,例如 1024 × 1024 或 1024 × 1536
- 质量:渲染质量,例如 low、medium 或 high
- 格式:文件输出格式
- 压缩:JPEG 和 WebP 格式的压缩级别 (0-100%)
- 背景:透明或不透明
- 操作:请求应自动选择、生成还是编辑图片
size, quality,且 background 支持 auto 选项,模型将根据提示词自动选择最佳选项。
gpt-image-2 支持灵活的 size 满足其 分辨率限制的。它目前不支持透明背景,因此带有 background: "transparent" fail.
有关可用选项的更多详情,请参考 图像生成指南.
使用 Responses API 图片生成工具时,受支持的 GPT Image 模型可以选择生成新图片,或编辑对话中已有的图片。可选的 action 参数控制此行为:保持 action 进行上传,并将其设置为 auto 以便模型自行选择生成或编辑,或将其设置为 generate or edit 以强制执行该行为。如果未指定,默认值为 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 or edit in your prompt.
例如,如果你想合成图片,与其说 combine or merge,你可以说类似“通过添加第二张图片中的这个元素来编辑第一张图片。”
多轮编辑
你可以通过引用之前的响应或图片 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))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
44
45
46
47
48
49
50
51
import openai
import base64
response = openai.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_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("cat_and_otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))
# Follow up
response_fwup = openai.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": [{"type": "input_text", "text": "Now make it look realistic"}],
},
{
"type": "image_generation_call",
"id": image_generation_calls[0].id,
},
],
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 parameter.
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.5gpt-5.4-minigpt-5.4-nanogpt-5.2gpt-5gpt-5-nanoo3gpt-4.1gpt-4.1-minigpt-4.1-nanogpt-4ogpt-4o-mini
用于图片生成过程的模型始终是 GPT Image 模型,包括 gpt-image-2, gpt-image-1.5, gpt-image-1,且 gpt-image-1-mini,但这些模型并非 model Responses API 中的字段。请使用支持文本的主流模型(例如 gpt-5.5 or gpt-5)与托管的 image_generation tool.