Create an image
Use CometAPI POST /v1/images/generations to create images with OpenAI-compatible image models and model-specific controls.
{
"created": 1781075000,
"background": "opaque",
"output_format": "jpeg",
"quality": "low",
"size": "1024x1024",
"usage": {
"input_tokens": 13,
"input_tokens_details": {
"image_tokens": 0,
"text_tokens": 13
},
"output_tokens": 196,
"output_tokens_details": {
"image_tokens": 196,
"text_tokens": 0
},
"total_tokens": 209
},
"data": [
{
"b64_json": "/9j/4AAQSkZJRgABAQAAAQABAAD..."
}
]
}Official reference
- Read the OpenAI image generation guide before you rely on model-specific controls such as
background,output_compression, streaming, or future GPT image options. - Use the OpenAI Create image reference for the current parameter list.
Choose a model first
- Use a GPT image model such as
gpt-image-2for text-to-image requests with controls likeoutput_format,quality, orbackground - Use
gpt-image-2.5-sunburstorgpt-image-2.5-flarewhen you need transparent backgrounds or thexhighandmaxquality levels - Use the same request shape with other image model families, such as Qwen-Image, Doubao Seedream, or Grok Imagine; start with
modelandprompt - Pick a current image model ID from the Models page
Safe first request
- Start with
gpt-image-2 - Keep
sizeat1024x1024 - GPT image models return base64-encoded image data in
b64_json; decode it to save the image file - Add
output_formatonly when you need a specific encoded image type such asjpeg - Use one prompt and one output image before you add batch generation or style tuning
Run an async image task
Useasync: true for long-running image jobs when your client prefers a submit-and-poll flow instead of holding one HTTP connection open. The create request returns data.task_id.
Poll Retrieve an image generation task until data.status is success or failure.
The async field is a CometAPI extension for this route, not an OpenAI parameter. OpenAI documents stream and partial_images for GPT image models. CometAPI asynchronous task mode returns JSON task metadata and uses polling.
Use async task mode with these documented model IDs: gpt-image-2 and doubao-seedream-4-0-250828. For other image models, use synchronous generation or streaming unless async task support is documented for that model.
When a request includes both async: true and stream: true, async task mode takes precedence. The create request returns JSON task metadata instead of an SSE stream.
Model-specific request behavior
response_formatapplies to DALL·E models only; GPT image models return base64 data and ignore it- GPT image models use GPT-only controls such as
output_format,quality,background, andoutput_compression qualityacceptslow,medium,high, andautoon GPT image models;gpt-image-2.5-sunburstandgpt-image-2.5-flarealso acceptxhighandmaxoutput_compressionapplies whenoutput_formatiswebporjpeg; it has no effect onpngpartial_imagesapplies only whenstreamistrue- Follow the OpenAI image generation guide for the latest model-specific options
- Other image model families can return the image in
urlinstead ofb64_json; read the field that each result item contains
Generate a transparent background
Setbackground to transparent to generate an isolated subject with no background fill. This is supported on gpt-image-2.5-sunburst and gpt-image-2.5-flare.
Transparency needs an output format with an alpha channel. Set output_format to png or webp. JPEG has no alpha channel, so a transparent request with output_format: "jpeg" is rejected.
The response returns base64 image data in b64_json with an alpha channel. Decode it to save the file:
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.cometapi.com/v1",
api_key=os.environ["COMETAPI_KEY"],
)
result = client.images.generate(
model="gpt-image-2.5-sunburst",
prompt="A single red apple, centered, isolated subject.",
quality="low",
size="1024x1024",
background="transparent",
output_format="png",
)
with open("apple.png", "wb") as f:
f.write(base64.b64decode(result.data[0].b64_json))
background to opaque to force a solid background, or auto to let the model decide. The response echoes the applied value in the top-level background field.
Body
The image generation model to use. Choose a current model from the Models page.
Quality setting for models that support it. GPT image models accept low, medium, high, and auto. gpt-image-2.5-sunburst and gpt-image-2.5-flare also accept xhigh and max. dall-e-3 accepts standard and hd. See the OpenAI image generation guide for the latest model-specific values.
"low"
Background mode for the generated image. Set transparent to generate an isolated subject with no background fill; this requires output_format set to png or webp, and returns an error with jpeg. Set opaque for a solid background, or auto to let the model decide. Supported on gpt-image-2.5-sunburst and gpt-image-2.5-flare.
transparent, opaque, auto Set this to true to receive server-sent image generation events instead of waiting for the completed JSON response. Streaming responses use text/event-stream and can include final events such as image_generation.completed. When stream and async are both true, async task mode takes precedence and the create request returns JSON instead of a streaming image response.
CometAPI asynchronous task mode. Set this to true to return immediately with data.task_id, then poll GET /v1/images/generations/{task_id} for the final image data. Documented model IDs for this mode: gpt-image-2 and doubao-seedream-4-0-250828. This is a CometAPI extension, not an OpenAI parameter. When async and stream are both true, async takes precedence and returns JSON task metadata instead of an SSE stream.
Response
Image generation result. Synchronous requests return completed image data. Async requests return a task response with data.task_id.
- Completed image response
- Async task response
{
"created": 1781075000,
"background": "opaque",
"output_format": "jpeg",
"quality": "low",
"size": "1024x1024",
"usage": {
"input_tokens": 13,
"input_tokens_details": {
"image_tokens": 0,
"text_tokens": 13
},
"output_tokens": 196,
"output_tokens_details": {
"image_tokens": 196,
"text_tokens": 0
},
"total_tokens": 209
},
"data": [
{
"b64_json": "/9j/4AAQSkZJRgABAQAAAQABAAD..."
}
]
}