Choose an endpoint
Start with
model, prompt, and n: 1. Select size, quality, background, and output_format according to the model. Do not apply one model’s size, count, or format limits to all channels. response_format selects the result representation (URL / Base64); output_format selects the image encoding (PNG / JPEG / WebP).
Python: generate and edit
Prepare the Python SDK environment. SetAPI_BASE_URL (including /v1), API_KEY, and an image model’s API_MODEL. Save as images.py. Run python images.py generate for generation. For editing, also set API_IMAGE_PATH to a local image and run python images.py edit. Editing uploads the file to the configured gateway and its upstream.
The example leaves the response format at the model default: it prefers b64_json, otherwise prints url. It chooses PNG/JPEG/WebP extensions from file signatures, which is not full image validation. It writes to the current directory without overwriting files.
Edit inputs and save results
Use theimage multipart file field. Channels supporting multiple images may require repeated image[] fields. Let the SDK create the boundary; do not label an upload as application/json. Masks commonly use a mask file, but transparent-area semantics, dimensions, and formats depend on the model.
JSON editing formats differ. This reference describes the common images: [{"image_url": "https://..."}] shape when supported; some plugins use other fields. Remote URLs must be reachable by the service processing the request. A local path is not a remote URL. The gateway’s generic Files API is unimplemented, so do not copy an upstream upload-to-Files and file_id workflow unchanged.
URLs may expire: save results promptly without forwarding the gateway Bearer key to a third-party image URL. Decode Base64 to binary bytes. Check HTTP errors and then image payload fields; revised_prompt alone is not an image. When saving multiple images, earlier files may already exist if a later save fails.
Waiting, streaming, and retries
This example uses synchronous results. The task-plugin Images protocol also waits within the request. It can return504 task_timeout, and work can continue after a client disconnects. A timeout does not prove submission failed: inspect request/task logs before generating again.
Direct relay channels may support image SSE, but the plugin Images protocol has no universal streaming mode. Do not assume stream: true works for all image models or parse image events with a Chat text-stream parser.
Reference: OpenAI image generation. This describes the upstream protocol; actual availability depends on the gateway.
简体中文