如需完整的文件索引,請參閱 llms.txt。在文件頁面的網址末尾加上 .md ,即可取得該頁面的 Markdown 版本。
主要導覽
主題

圖像生成

瞭解如何生成或編輯圖像。

概觀

透過 API,你可以使用 gpt-image-2.5-sunburst 和 gpt-image-2.5-flare,依文字提示詞生成或編輯圖像。如果工作流程最重視編輯精準度,請選用 Sunburst。日常需要快速生成高品質圖像時,則可選用 Flare。以下兩種 API 都提供圖像生成功能:

Image API

Image API 提供兩個端點,各有不同用途:

Responses API

Responses API 可在對話或多步驟流程中生成圖像。它提供圖像生成內建工具,並可在上下文中接收輸入和輸出的圖像。

與 Image API 相比,Responses API 還提供以下功能:

  • 多輪編輯:透過提示詞逐輪編輯圖像,保留原圖細節。
  • 彈性輸入:除了直接傳入位元組,也可以使用圖像的檔案 ID 指定輸入圖像。

如需瞭解哪些主模型可以呼叫圖像生成工具,請參閱支援的模型。

選擇適合的 API

  • 如果只需要根據一個提示詞生成或編輯單張圖像,Image API 是最適合的選擇。
  • 如果想讓使用者透過對話與 GPT Image 互動並持續編輯圖像,請選用 Responses API。

使用 Image API 時,請直接將 model 設為 gpt-image-2.5-sunburst 或 gpt-image-2.5-flare。使用 Responses API 時,請在請求的最上層選擇支援的主模型,再指定 gpt-image-2.5-sunburst 或 gpt-image-2.5-flare 作為圖像生成工具的 model 欄位值。

這兩種 API 都可透過調整品質、尺寸、格式和壓縮程度來自訂輸出。

為確保以負責任的方式使用這些模型,你可能需要先完成 API 組織驗證,才能使用 GPT Image 模型。你可以在開發人員主控台完成驗證。

GPT-Image-2.6

GPT-Image-2.6 提供兩種模型:Sunburst 適合需要精細處理視覺細節的工作,Flare 適合快速嘗試不同版本。透過 Image API,這兩種模型都支援調整複雜度、使用標註引導編輯,以及輸出 SVG 和 GIF 動畫。下方原有的範例使用 GPT-Image-2.5。

設定圖像複雜度

使用請求中的 complexity 欄位,控制圖像模型在算繪前投入多少規劃。只需更換背景或招牌等局部修改時,可選用較低的複雜度。處理密集的文字、複雜的版面配置或物件之間的關係時,較高的複雜度可能有幫助。提高複雜度也可能增加延遲,或導致原本想保留的細節被改動,請依實際任務比較結果。

Sunburst 和 Flare 支援的複雜度等級為 low、medium 和 high。如果省略 complexity,兩種模型都會使用 high。算繪品質則由獨立的 quality 設定控制。

調整複雜度不另收費。

以下剪紙風格的足球場景都是 Sunburst 在中等品質下,根據同一個提示詞生成的。請比較不同複雜度下的獎盃、球衣,以及「CHAMPIONS」標語的位置。

低複雜度

中等複雜度

高複雜度

安全識別碼

如果應用程式直接為個別使用者提供服務,請為每位使用者設定固定且能保護隱私的 safety_identifier,並在每個圖像生成或圖像編輯請求中傳送該使用者的識別碼。請將使用者名稱或電子郵件地址轉換為雜湊值,避免傳送可識別身分的資訊,並將識別碼長度控制在 128 個字元以內。建議使用安全識別碼,但並非必要。

Images API 的 user 參數已棄用。請改用 safety_identifier 傳送相同的識別碼。API 仍支援使用 user 的既有請求。非空的 safety_identifier 優先於 user。如果未提供 safety_identifier,或其值為 null 或空字串,API 就會使用已提供的 user。

木桌上的米色咖啡馬克杯

生成圖像

你可以透過圖像生成端點根據文字提示詞建立圖像,也可以使用 Responses API 的圖像生成工具在對話中生成圖像。

如需瞭解如何自訂輸出尺寸、品質、格式和壓縮程度,請參閱下方的自訂圖像輸出。

你可以設定 n 參數,在單一請求中一次生成多張圖像。API 預設會傳回一張圖像。

生成圖像
from openai import OpenAI
import base64

client = OpenAI()

prompt = """
A children's book drawing of a veterinarian using a stethoscope to
listen to the heartbeat of a baby otter.
"""

result = client.images.generate(model="gpt-image-2.5-sunburst", prompt=prompt)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

# Save the image to a file
with open("otter.png", "wb") as f:
    f.write(image_bytes)

多輪圖像生成

使用 Responses API 時,你可以將圖像生成工具的呼叫結果放入上下文(也可以只提供圖像 ID),或使用 previous_response_id 參數,在多輪對話中生成圖像。 這樣就能隨著對話進展,逐輪調整提示詞、套用新指示,逐步修改圖像效果。

在 Responses API 的圖像生成工具中,支援此功能的工具模型可以決定要生成新圖像,還是編輯對話中已有的圖像。你可以使用選用的 action 參數控制這項行為:保留 action: "auto" 讓模型自行決定,設為 action: "generate" 一律生成新圖像,或設為 action: "edit",在上下文中有圖像時強制編輯。

使用 action 強制生成新圖像
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", "action": "generate"}
    ],
)

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

如果強制使用 edit,但上下文中沒有提供圖像,這次呼叫就會傳回錯誤。將 action 保持為 auto,即可讓模型自行決定何時生成或編輯圖像。

多輪圖像生成
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))

結果

「生成一張圖,畫一隻灰色虎斑貓抱著一隻圍著橘色圍巾的水獺。」

一隻貓和一隻水獺

「現在把這張圖改成寫實風格。」

一隻貓和一隻水獺

串流

Responses API 和 Image API 都支援串流圖像生成。你可以在 API 生成圖像的過程中,透過串流接收中間圖像,讓使用者即時看到生成進度。

你可以調整 partial_images 參數,接收 0 至 3 張中間圖像。

  • 如果將 partial_images 設為 0,就只會收到最終圖像。
  • 如果設為大於零的值,而完整圖像提早生成,你收到的中間圖像可能會少於要求的張數。
以串流接收圖像
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])

結果

第 1 張中間圖像 第 2 張中間圖像 最終圖像
第 1 張中間圖像 第 2 張中間圖像 最終圖像

提示詞:畫一幅優美的冬日風景,一條由白色貓頭鷹羽毛構成的河流蜿蜒穿過寧靜的大地。

修訂後的提示詞

使用 Responses API 的圖像生成工具時,主模型(例如 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": "..."
}

編輯圖像

透過圖像編輯端點,你可以:

  • 編輯現有圖像
  • 以其他圖像為參考,生成新圖像
  • 上傳圖像及標示替換區域的遮罩,編輯圖像的特定部分

使用參考圖像建立新圖像

你可以使用一張或多張圖像作為參考,生成新圖像。

在這個範例中,我們會使用 4 張輸入圖像生成一張禮物籃的新圖像,籃中裝有參考圖像裡的物品。

身體乳液香皂薰香組泡澡球
沐浴禮品組

使用 Responses API 時,你可以透過以下三種方式提供輸入圖片:

  • 提供圖片的完整 URL
  • 以 Base64 編碼的資料 URL 提供圖片
  • 提供使用 Files API 建立的檔案 ID

建立檔案

建立檔案
from openai import OpenAI

client = OpenAI()


def create_file(file_path):
    with open(file_path, "rb") as file_content:
        result = client.files.create(
            file=file_content,
            purpose="vision",
        )
        return result.id

建立以 Base64 編碼的圖片

建立以 Base64 編碼的圖片
import base64


def encode_image(file_path):
    with open(file_path, "rb") as f:
        base64_image = base64.b64encode(f.read()).decode("utf-8")
    return base64_image
編輯圖片
from openai import OpenAI
import base64

client = OpenAI()


def encode_image(file_path):
    with open(file_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


def create_file(file_path):
    with open(file_path, "rb") as file_content:
        result = client.files.create(file=file_content, purpose="vision")
    return result.id


prompt = """Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures."""

base64_image1 = encode_image("body-lotion.png")
base64_image2 = encode_image("soap.png")
file_id1 = create_file("bath-bomb.png")
file_id2 = create_file("incense-kit.png")

response = client.responses.create(
    model="gpt-6-astra",
    input=[
        {
            "role": "user",
            "content": [
                {"type": "input_text", "text": prompt},
                {
                    "type": "input_image",
                    "image_url": f"data:image/png;base64,{base64_image1}",
                },
                {
                    "type": "input_image",
                    "image_url": f"data:image/png;base64,{base64_image2}",
                },
                {
                    "type": "input_image",
                    "file_id": file_id1,
                },
                {
                    "type": "input_image",
                    "file_id": file_id2,
                },
            ],
        }
    ],
    tools=[{"type": "image_generation", "model": "gpt-image-2.5-sunburst"}],
)

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("gift-basket.png", "wb") as f:
        f.write(base64.b64decode(image_base64))
else:
    print(response.output_text)

使用標註引導編輯

搭配支援此功能的 GPT-Image-2.5 或 GPT-Image-2.6 模型時,你可以透過 Image API 的 POST /v1/images/edits 端點,使用標註引導編輯。如果要透過 Responses API 編輯局部區域,請使用遮罩。在標註圖層上標出要修改的位置,並在 prompt 中說明要如何修改。

例如,將廚房照片和透明標註圖層下載到同一個目錄。圖層上圈出了要變更顏色的花瓶。

使用 Image API 編輯圈選的花瓶
import base64
from pathlib import Path

from openai import OpenAI

client = OpenAI()
overlay = Path("markup-vase-overlay-gpt-image-2-6.png")
with open("kitchen.webp", "rb") as image, overlay.open("rb") as markup:
    result = client.images.edit(
        model="gpt-image-2.6-flare",
        image=("kitchen.webp", image, "image/webp"),
        markup=(overlay.name, markup, "image/png"),
        quality="medium",
        prompt=(
            "Recolor the striped vase inside the red circle to solid cobalt blue ceramic. "
            "Keep its shape, the dried stems, and the rest of the kitchen unchanged. "
            "Remove the red circle from the finished photo."
        ),
    )

Path("vase.png").write_bytes(base64.b64decode(result.data[0].b64_json))

疊加標註圖層後的照片

Flare 的輸出結果

  1. 將原圖放在輸入圖像的第一個位置。
  2. 另建一個透明 PNG 檔案,在上面畫出圓圈、草圖或其他標註。套用圖像方向設定後,標註圖層的寬度和高度必須與第一張圖像完全相同。PNG 必須包含透明像素,請勿將圖層與原圖合併成單一圖像。
  3. 使用 markup 傳送標註圖層。多部分請求支援直接上傳檔案。使用 JSON 請求時,請傳入一個物件,其中必須包含 image_url 或 file_id,兩者只能擇一。image_url 可填入 HTTPS 網址或 base64 資料網址。JSON 格式的編輯請求使用 images 傳入圖像陣列。
  4. 說明要做哪些修改,以及要保留哪些細節。使用結果前,請先檢查編輯後的圖像。

標註只套用到第一張輸入圖像,其餘圖像則作為參考。第一張輸入圖像與標註圖層各自最多可包含 16,777,216 個像素。標註檔案的大小也不得超過 50 MB。請勿將 markup 與 mask 搭配使用。

使用遮罩編輯圖像

你可以提供遮罩,指定要編輯的圖像區域。

在 GPT Image 中使用遮罩時,系統會一併傳送額外指示,引導模型依照遮罩編輯圖像。

GPT Image 完全透過提示詞執行遮罩編輯。模型會參考遮罩,但編輯範圍可能無法精確吻合遮罩的形狀。

如果提供多張輸入圖像,遮罩會套用到第一張圖像。

使用遮罩編輯圖像
from openai import OpenAI
import base64

client = OpenAI()


def create_file(file_path):
    with open(file_path, "rb") as file_content:
        result = client.files.create(file=file_content, purpose="vision")
    return result.id


fileId = create_file("sunlit_lounge.png")
maskId = create_file("mask.png")

response = client.responses.create(
    model="gpt-6-astra",
    input=[
        {
            "role": "user",
            "content": [
                {
                    "type": "input_text",
                    "text": "generate an image of the same sunlit indoor lounge area with a pool but the pool should contain a flamingo",
                },
                {
                    "type": "input_image",
                    "file_id": fileId,
                },
            ],
        },
    ],
    tools=[
        {
            "type": "image_generation",
            "model": "gpt-image-2.5-sunburst",
            "quality": "high",
            "input_image_mask": {
                "file_id": maskId,
            },
        },
    ],
)

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("lounge.png", "wb") as f:
        f.write(base64.b64decode(image_base64))
圖像 遮罩 輸出
設有泳池的粉紅色房間 遮罩覆蓋泳池的一部分 原本的泳池中,遮罩所在位置已換成充氣紅鶴

提示詞:陽光灑進室內休憩區,區內有座泳池,池中有一隻紅鶴。

遮罩要求

待編輯圖像與遮罩的格式和尺寸必須相同,且檔案大小都必須小於 50 MB。

遮罩圖像也必須包含 Alpha 色版。如果使用圖像編輯工具建立遮罩,請確認儲存時有保留 Alpha 色版。

你可以透過程式修改黑白圖像,為其新增 Alpha 色版。

為黑白遮罩新增 Alpha 色版
from PIL import Image
from io import BytesIO

# 1. Load your black & white mask as a grayscale image
mask = Image.open("mask.png").convert("L")

# 2. Convert it to RGBA so it has space for an alpha channel
mask_rgba = mask.convert("RGBA")

# 3. Then use the mask itself to fill that alpha channel
mask_rgba.putalpha(mask)

# 4. Convert the mask into bytes
buf = BytesIO()
mask_rgba.save(buf, format="PNG")
mask_bytes = buf.getvalue()

# 5. Save the resulting file
img_path_mask_alpha = "mask_alpha.png"
with open(img_path_mask_alpha, "wb") as f:
    f.write(mask_bytes)

自訂圖像輸出

你可以設定以下輸出選項:

  • 尺寸:圖像的寬度和高度(例如 1024x1024、1024x1536)。
  • 品質:算繪品質(例如 low、medium、high)。
  • 格式:輸出檔案的格式。
  • 壓縮:JPEG 和 WebP 格式的壓縮程度(0–100%)。
  • 背景:透明、不透明或自動選擇。

size、quality 和 background 都支援 auto 選項。選用時,模型會根據提示詞自動選擇最適合的設定。

尺寸與品質選項

gpt-image-2.5-sunburst 和 gpt-image-2.5-flare 新增了 xhigh 和 max 品質設定。兩個模型的品質預設值都是 auto。較早的 GPT Image 模型最高支援 high 品質。

設定 選項
建議尺寸 1024x1024(正方形)、1536x1024(橫向)、1024x1536(直向)
品質 low、medium、high、xhigh、max、auto

這兩個模型也支援以 WIDTHxHEIGHT 格式的字串指定自訂尺寸,例如 1536x864。寬度和高度都必須是 16 的倍數,寬高比必須介於 1:3 與 3:1 之間,任一邊均不得超過 3840 像素。總像素數必須介於 655,360 與 8,294,400(4K)之間。高於 2560x1440 的解析度仍屬實驗性功能。

如需讓任一模型生成透明背景,請設定 background: "transparent",並使用 output_format: "png" 或 "webp"。

若要快速產生草稿,請使用 quality: "low"。製作最終素材時,可比較較高的品質設定,在細節、延遲與成本之間取得適當平衡。

輸出格式

Image API 會傳回以 base64 編碼的圖像資料。 預設格式為 png,你也可以要求使用 jpeg 或 webp。

使用 jpeg 或 webp 時,也可以指定 output_compression 參數來控制壓縮程度(0–100%)。例如,output_compression=50 會將圖像壓縮 50%。

使用 jpeg 比 png 更快。如果需要降低延遲,請優先選用 JPEG 格式。

生成 SVG 圖像

將 output_format: "svg" 用於 POST /v1/images/generations 請求。如果圖形中的路徑、形狀和顏色需要保留供後續編輯,例如圖示或插畫,請使用 SVG。

將 n 設為 1。SVG 輸出不支援串流、中間圖像或 output_compression。圖像編輯端點不支援 SVG 輸出,也不接受 SVG 作為點陣圖輸入。

API 會先建立參考圖像,再依照該圖像產生 SVG。quality 設定控制的是參考圖像的品質。請以 base64 解碼 data[0].b64_json,並將得到的 UTF-8 XML 位元組儲存為副檔名為 .svg 的檔案。傳回的文件只包含靜態向量內容,不支援指令碼、外部資源或內嵌的點陣圖。

以下是 Flare 和 Sunburst 根據同一個提示詞,為 Field & Flour 麵包店生成的 SVG 標誌。

GPT-Image-2.6 Flare

GPT-Image-2.6 Sunburst

生成 GIF 動畫

設定 output_format: "gif" 即可透過 POST /v1/images/generations 或 POST /v1/images/edits 輸出 GIF 動畫。在提示詞中描述動畫主體、動作,以及你希望動畫如何循環播放。如果要用靜態參考圖像引導動畫生成,請將這些圖像傳送至圖像編輯端點。

無論使用哪個端點,都可以使用下列請求欄位設定 GIF 輸出:

欄位 可用值 預設值
frame_count 2 至 16 個影格 9
frame_duration_ms 每個影格 20 至 2000 毫秒,四捨五入至最接近的 10 毫秒倍數 200
size auto,或以 WIDTHxHEIGHT 指定尺寸,寬度和高度都必須是 16 的正整數倍 auto

size 指定輸出動畫中每個影格的尺寸。影格數量、影格尺寸和動畫數量的組合,必須符合模型的尺寸限制及單次請求的總像素上限。如果請求超出限制,請減少 frame_count、縮小影格尺寸,或調低 n。使用 auto 可讓 API 在模型允許的範圍內選擇影格尺寸。

回應中會包含完整的動畫。請以 base64 解碼每個結果中的 b64_json 值,並將解碼後的位元組存成副檔名為 .gif 的檔案。GIF 輸出不支援串流、中間圖像或自訂 output_compression。frame_count 和 frame_duration_ms 僅可用於 GIF 輸出。

Sunburst 使用 frame_count=9 和 frame_duration_ms=200 生成了這段提燈動畫。經過 1.8 秒,猴子會回到起始姿勢。

限制

GPT Image 模型功能強大,可用於多種圖像生成工作,但仍有以下限制:

  • 延遲: 處理複雜的提示詞可能需要長達 2 分鐘。
  • 文字算繪: 雖然文字算繪已有顯著改善,模型仍可能無法將文字精確放在指定位置,或讓文字清晰可辨。
  • 一致性: 模型能生成風格一致的圖像,但多次生成時,反覆出現的角色或品牌元素偶爾會有外觀差異。
  • 構圖控制: 模型遵循指示的能力已有改善,但對於結構嚴謹或對版面配置有特定要求的構圖,仍可能難以精確放置各個元素。

內容審核

所有提示詞和生成的圖像都會依據我們的內容政策進行篩選。

使用 GPT Image 模型生成圖像時,你可以透過 moderation 參數控制審核的嚴格程度。此參數支援兩個值:

  • auto(預設):採用標準篩選,盡量限制生成某些可能不適合特定年齡層的內容。
  • low:採用較寬鬆的篩選。

處理遭封鎖的請求及其他錯誤

圖像生成失敗時,請依照處理其他 API 錯誤的方式,檢查 HTTP 狀態碼或 SDK 例外狀況類型,並記錄請求 ID。遇到驗證、配額、速率限制或伺服器錯誤時,請參閱錯誤碼指南。如果是暫時性的速率限制或伺服器錯誤,請採用退避策略重試。遇到配額錯誤,或必須修改請求才能解決的圖像生成使用者錯誤時,請勿自動重試。

部分圖像生成錯誤可由使用者修正,這類錯誤可能會傳回 error.type = "image_generation_user_error"。遇到這些錯誤時,請勿在未修改提示詞或輸入圖像的情況下自動重試。若要透過程式處理錯誤,請使用 error.code 作為穩定的判別依據。

當 error.code = "moderation_blocked" 時,錯誤也可能包含選用的 error.moderation_details 物件:

{
  "error": {
    "type": "image_generation_user_error",
    "code": "moderation_blocked",
    "moderation_details": {
      "moderation_stage": "input",
      "categories": ["harassment"]
    }
  }
}

moderation_details 物件提供偵錯所需的概略資訊,不會揭露內部分類器的標籤或分數。

moderation_stage 的可能值如下:

  • input:提示詞或請求的輸入內容觸發了封鎖。
  • output:生成的圖像或後續的輸出審核階段觸發了封鎖。
  • unknown:極少數情況下難以判定封鎖來源,此時會使用這個備用值。

categories 包含公開的概略分類標籤。例如,你可能會看到 harassment、self-harm、sexual 或 violence 等值。

對於大多數應用程式,向使用者顯示的主要訊息應保持簡單、通用。可將 moderation_details 用於開發人員記錄、客服流程、資料分析,以及提供簡短的修正提示。

處理因內容審核遭封鎖的圖像生成錯誤
import OpenAI from "openai";

const openai = new OpenAI();

try {
  // The same error handling pattern applies to image generation requests,
  // image edits, and Responses API tool calls that generate images.
  await openai.images.generate({
    model: "gpt-image-2.5-sunburst",
    prompt: "Create a poster humiliating my coworker with insulting captions",
  });
} catch (error) {
  if (
    !(error instanceof OpenAI.APIError) ||
    error.code !== "moderation_blocked"
  ) {
    throw error;
  }

  const moderationDetails =
    error.error && "moderation_details" in error.error
      ? (error.error.moderation_details as {
          categories?: string[];
          moderation_stage?: string;
        })
      : undefined;
  const categories = moderationDetails?.categories ?? [];
  const stage = moderationDetails?.moderation_stage;

  let hint =
    "This request could not be completed because it did not meet safety requirements.";

  if (categories.includes("harassment")) {
    hint =
      "Try removing abusive or targeting language and focus on neutral visual details instead.";
  } else if (stage === "input") {
    hint =
      "Try revising the prompt or input images and submit the request again.";
  } else if (stage === "output") {
    hint =
      "The generated result was blocked by a safety check. Try changing the prompt and generating again.";
  }

  console.error("Image generation blocked", {
    request_id: error?.requestID,
    code: error?.code,
    moderation_details: moderationDetails,
  });

  console.log(hint);
}

支援的模型

在 Responses API 中生成圖像時,gpt-5 及更新的模型應可使用圖像生成工具。請查看模型詳細資訊頁面,確認你想使用的模型是否支援此工具。

成本與延遲

GPT-Image-2.6 費用

圖像輸出每百萬個計費 Token 收費 30 美元。你可以使用計算器估算單張圖像的輸出費用。

如需查詢文字與圖像每百萬個 Token 的費率,請參閱圖像生成定價。

使用標註引導編輯與其他編輯方式的 Token 費率相同,不另收費。輸出 SVG 時,生成參考圖像的費用按圖像模型的費率計算,撰寫 SVG 所用的 Token 則按負責撰寫的模型費率計算。總費用取決於兩個模型各自的 Token 用量。

GPT-Image-2.5 費用

除了圖像生成費用,Responses API 請求也會計入主模型的 Token 用量。

兩個 GPT-Image-2.5 模型的 Token 費率相同。以每百萬個 Token 計價,圖像輸入為 8 美元、快取圖像輸入為 2 美元、圖像輸出為 30 美元、文字輸入為 5 美元,快取文字輸入則為 1.25 美元。請參閱定價。

你可以透過回應中的 usage 衡量不同提示詞、尺寸與品質設定的 Token 用量。即使 Token 費率相同,每張圖像的費用仍可能不同,因為 Token 用量會因模型與品質設定而異。如需查看舊版模型的定價範例,請參閱較早的 GPT Image 模型。

快取輸入定價

對於 GPT-Image-2 和 GPT-Image-2.5,快取輸入定價僅適用於 Responses API 的圖像生成工具。直接呼叫 Images API 的請求(包括 /v1/images/edits)不適用此定價。

帳單會反映圖像生成輸入的快取用量,但 Responses API 的輸出不會列出這些快取 Token 的數量,因此無法透過回應中的 usage 確認這些快取命中情況。

計算費用

選擇模型、品質與尺寸,即可估算輸出 Token 數量和圖像輸出費用。 GPT-Image-2.6 和 GPT-Image-2.5 的 Sunburst 與 Flare 模型都提供 low、medium、high、xhigh 和 max 品質選項。 gpt-image-2 的品質選項為 low、medium 和 high。 各模型的圖像輸出 Token 單價相同,但即使採用相同的品質設定,Token 用量也可能不同。 估算時請明確指定品質與尺寸。使用 auto 時,實際設定取決於生成的圖像。

模型
品質
輸出 Token 數
823
預估圖像輸出費用
US$0.02469

每張圖像的費用按每百萬個圖像輸出 Token 30 美元計算。此估算不含文字與圖像輸入 Token 的費用,也不含以串流方式傳送未完成圖像的費用。

中間圖像費用

如果要以串流方式生成圖像並使用 partial_images 參數,每張中間圖像會額外計入 100 個圖像輸出 Token。

較早的 GPT Image 模型

以下資訊適用於較早的模型,不適用於 Sunburst 或 Flare。建立新的整合時,請使用 GPT-Image-2.6 Sunburst 或 Flare。

GPT-Image-2 設定與輸入保真度

gpt-image-2 的 size 參數接受任何符合以下限制的解析度。正方形圖像的生成速度通常最快。

常用尺寸
  • 1024x1024(正方形)

  • 1536x1024(橫向)

  • 1024x1536(直向)

  • 2048x2048(2K 正方形)

  • 2048x1152(2K 橫向)

  • 3840x2160(4K 橫向)

  • 2160x3840(4K 直向)

  • auto(預設)

尺寸限制
  • 最長邊不得超過 3840px

  • 寬度與高度都必須是 16px 的倍數

  • 長邊與短邊之比不得超過 3:1

  • 總像素數至少須為 655,360,且不得超過 8,294,400

品質選項
  • low
  • medium
  • high
  • auto(預設)

圖像輸入保真度

input_fidelity 參數控制模型在編輯圖像或使用參考圖像時保留輸入圖像細節的程度。使用 gpt-image-2 時,請省略此參數。模型會自動以高保真度處理每張輸入圖像,因此 API 不允許變更這項設定。

由於 gpt-image-2 一律以高保真度處理輸入圖像,含有參考圖像的編輯請求可能會使用更多圖像輸入 Token。如需瞭解這對費用的影響,請參閱視覺處理費用。

舊版模型定價範例

gpt-image-2 之前的模型

gpt-image-2 之前的 GPT Image 模型會先產生專用的圖像 Token,再生成圖像。延遲和最終費用都與算繪圖像所需的 Token 數量成正比。圖像尺寸越大、品質設定越高,所需的 Token 就越多。

生成的 Token 數量取決於圖像尺寸與品質:

品質 正方形(1024×1024) 直向(1024×1536) 橫向(1536×1024)
低 272 個 Token 408 個 Token 400 個 Token
中 1056 個 Token 1584 個 Token 1568 個 Token
高 4160 個 Token 6240 個 Token 6208 個 Token

你也需要計入輸入 Token,包括提示詞的文字 Token。若要編輯圖像,還要計入輸入圖像的圖像 Token。 由於 gpt-image-2 一律以高保真度處理輸入圖像,含有參考圖像的編輯請求可能會使用更多輸入 Token。

請參閱定價頁面,查看目前的文字與圖像 Token 價格,並依計算費用一節估算圖像輸出費用。

最終費用是下列項目的費用總和:

  • 文字輸入 Token
  • 圖像輸入 Token(使用編輯端點時)
  • 圖像輸出 Token

歷年圖像輸出費用

gpt-image-2 支援數千種解析度。為方便比較,下表列出與先前 GPT Image 模型相同的尺寸。下方也列出 GPT Image 1.5、GPT Image 1 和 GPT Image 1 mini 先前按張計算的圖像輸出價格。估算請求的總費用時,仍需計入文字與圖像輸入 Token。

gpt-image-1 已於 2026 年 10 月 23 日停止服務。下表保留其價格,供歷史比較。如需瞭解可替代的模型,請參閱棄用公告。

在相同的品質設定下,尺寸較大的非正方形圖像有時會比尺寸較小或正方形的圖像產生更少的輸出 Token。

模型

品質

1024 × 10241024 × 15361536 × 1024

GPT-Image-2


另支援其他尺寸
低$0.006$0.005$0.005
中$0.053$0.041$0.041
高$0.211$0.165$0.165

GPT Image 1.5

低$0.009$0.013$0.013
中$0.034$0.05$0.05
高$0.133$0.2$0.2

GPT Image 1

低$0.011$0.016$0.016
中$0.042$0.063$0.063
高$0.167$0.25$0.25

GPT Image 1 mini

低$0.005$0.006$0.006
中$0.011$0.015$0.015
高$0.036$0.052$0.052