For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
メインナビゲーション

画像生成

モデルで画像を生成・編集できるようにします。

画像生成ツールでは、テキストプロンプトを使って画像を生成できます。必要に応じて画像も入力できます。gpt-image-2.5-sunburstgpt-image-2.5-flaregpt-image-2gpt-image-1.5gpt-image-1gpt-image-1-mini などの GPT Image モデルを使用し、より良い結果が得られるようにテキスト入力を自動で最適化します。

精密な編集には、image_generation ツールの modelgpt-image-2.5-sunburst に設定します。高品質な画像を高速に生成するには、gpt-image-2.5-flare に設定します。Responses のトップレベルの model フィールドには、対応するメインラインモデルを指定してください。

画像生成の詳細については、専用の画像生成 ガイドを参照してください。

使い方

リクエストに image_generation ツールを含めると、モデルはプロンプトと入力画像(指定されている場合)をもとに、会話の中でいつ、どのように画像を生成するかを判断できます。

image_generation_call のツール呼び出し結果には、base64 エンコードされた画像が含まれます。

画像の生成
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)
  • 品質:レンダリング品質(例:low、medium、high)
  • 形式:出力ファイルの形式
  • 圧縮:JPEG 形式と WebP 形式の圧縮率(0~100%)
  • 背景:透明、不透明、または自動
  • アクション:画像の生成、編集、またはその自動選択の指定

sizequalitybackgroundauto オプションに対応しており、モデルがプロンプトに基づいて最適な設定を自動で選択します。

gpt-image-2.5-sunburstgpt-image-2.5-flare では、qualityxhighmax も指定できます。以前の GPT Image モデルはこれらの値に対応していません。品質のデフォルト値は引き続き auto です。

gpt-image-2 では、解像度の制約を満たす範囲で size の値を柔軟に指定できます。透明な背景はプレビュー機能として利用できます。使用するには background: "transparent" を設定してください。形式には png(デフォルト)または webp を使用します。jpeg は透明な背景に対応していません。

利用可能なオプションの詳細については、画像生成ガイドを参照してください。

Responses API の画像生成ツールを使用する場合、対応する GPT Image モデルは、新しい画像を生成するか、会話内にある画像を編集するかを選択できます。この動作は、省略可能な action パラメーターで制御します。生成と編集のどちらを行うかをモデルに任せるには、actionauto のままにします。特定の動作を必ず実行させるには、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": "..."
}

プロンプトのヒント

画像生成では、プロンプトに drawedit などの語を使うと、最良の結果が得られます。

たとえば、画像を組み合わせたい場合は、combinemerge と指示する代わりに、「2 枚目の画像のこの要素を追加して、1 枚目の画像を編集してください」のように指示できます。

複数ターンでの編集

以前のレスポンス 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])

対応モデル

次のモデルが画像生成ツールに対応しています。

  • 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