Skip to main content

gpt-image-2 API

gpt-image-2 supports text-to-image generation and image editing. It is available through both the AIHubMix unified generation endpoint and OpenAI-compatible endpoints. For new unified integrations, see Image Generation. The examples on this page use OpenAI-compatible endpoints for existing OpenAI SDK integrations.
Generation, editing, Base64 decoding, and the invalid-parameter flow on this page were verified with gpt-image-2. Before integration, query the Model Schema API, select an endpoint by path, and read its request.schema.

Prerequisites

Install or upgrade the OpenAI Python SDK and provide the API key through an environment variable:

Generate an image

This example generates one low-quality 1024x1024 PNG and saves the returned Base64 data.
Equivalent HTTP request:

Edit an image

The editing endpoint accepts multipart/form-data. Put the source image in the current directory as input.png.
Equivalent HTTP request:

Confirmed parameters

Do not copy parameters from other GPT Image versions into gpt-image-2. Use the request.schema for the selected endpoint as the source of truth.

Response example

Both compatible endpoints return an OpenAI image response. data[].b64_json contains Base64 image data, and usage reports token usage.

Error response

An unsupported parameter returns a structured 4xx error. For example, quality: "ultra" returns HTTP 400:
Check error.param and error.code, then correct the request against the selected endpoint’s request.schema.