如需完整的文件索引,請參閱 llms.txt。在文件頁面的網址末尾加上 .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-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-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-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-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。如需瞭解各模型的可用情況與存取條件,請參閱個別模型的說明頁面。

如需查詢模型的可用情況與停止服務日期,請參閱棄用資訊。