Streaming
Responses API 和 Image API 支持流式图像生成。可以随着 API 的生成过程流式获取部分图像,从而提供更具交互性的体验。
可以通过调整 partial_images 参数来接收 0 到 3 张部分图像。
- 如果将 partial_images 设置为 0,将仅收到最终图像。
- 对于大于零的值,如果完整图像生成得更快,可能无法收到所请求的全部部分图像。
Responses API
from openai import OpenAI
import base64
client = OpenAI()
stream = client.responses.create(
model="gpt-5.5",
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", "partial_images": 2}],
)
for event in stream:
if event.type == "response.image_generation_call.partial_image":
idx = event.partial_image_index
image_base64 = event.partial_image_b64
image_bytes = base64.b64decode(image_base64)
with open(f"river{idx}.png", "wb") as f:
f.write(image_bytes)
Image API
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)
结果



提示:画一幅由白猫头鹰羽毛构成的河流,蜿蜒流经一片宁静的冬日风景,画面要美不胜收。
