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-quality1024x1024 PNG and saves the returned Base64 data.
Edit an image
The editing endpoint acceptsmultipart/form-data. Put the source image in the current directory as input.png.
Confirmed parameters
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 structured4xx error. For example, quality: "ultra" returns HTTP 400:
error.param and error.code, then correct the request against the selected endpoint’s request.schema.