Integration Guides

OpenAI-Compatible Image Generation API: How It Works (2026)

ROIBest AI

What is an OpenAI-compatible image generation API?

An OpenAI-compatible image generation API accepts the same request as OpenAI's POST /v1/images/generations endpoint (a JSON body with model and prompt) and returns the same response shape: a data array whose items carry a base64 image in b64_json or, on some providers, a url. If a provider implements that contract, you can keep using the official OpenAI SDK's images.generate() call and only change the base URL, the API key and the model name.

"Compatible" is looser for images than for chat, though. Providers agree on the endpoint and the core fields, then differ on sizes, output format, edits and which extra parameters they honor. This guide covers the contract, how to call it from Python, TypeScript and curl, where well-known providers differ, and a short checklist before you switch. Details were checked against OpenAI's image generation guide and API reference, Google's Gemini OpenAI-compatibility docs and OpenRouter's image docs on December 1, 2026.

The contract: endpoints, request fields and response

OpenAI exposes image generation in two places:

Surface

Endpoint

Use it when

Image API, generations

POST /v1/images/generations

One prompt in, one or more images out

Image API, edits

POST /v1/images/edits

Modify or combine existing images, optionally with a mask

Responses API image tool

POST /v1/responses with tools: [{"type": "image_generation"}]

Multi-turn, conversational image editing

When people say "OpenAI-compatible image API" they almost always mean the first row. The Responses API image tool is rarely implemented by third parties, so don't build on it if you plan to switch providers.

Core request fields for /v1/images/generations:

Field

Meaning

Portability

model

Image model ID

Required everywhere; the value is provider-specific

prompt

Text description

Universal

n

Number of images

Widely supported; some models only allow 1

size

WIDTHxHEIGHT, e.g. 1024x1024, or auto

Supported, but each provider accepts different sizes

response_format

url or b64_json

Legacy on OpenAI (GPT Image models always return base64); still used by several compatible providers

quality, background, output_format, output_compression, moderation

GPT Image output controls

Often ignored outside OpenAI

stream, partial_images

Stream partial images

Rarely supported outside OpenAI

The response looks like this (trimmed):

{
  "created": 1764547200,
  "data": [
    { "b64_json": "iVBORw0KGgo..." }
  ],
  "usage": { "input_tokens": 50, "output_tokens": 4160, "total_tokens": 4210 }
}

Two portability notes. First, OpenAI's current GPT Image models return base64 only; the url response format belongs to retired models. Code that reads data[0].url therefore breaks on OpenAI while working on some other providers. Second, the usage block is OpenAI-specific and may be missing elsewhere, so don't make billing logic depend on it.

Calling a compatible image API with the OpenAI SDK

Python:

import base64
import os
from openai import OpenAI

client = OpenAI(
    base_url=os.environ["IMAGE_API_BASE_URL"],  # e.g. https://your-provider.example/v1
    api_key=os.environ["IMAGE_API_KEY"],
)

result = client.images.generate(
    model=os.environ["IMAGE_MODEL"],  # provider's model ID
    prompt="A watercolor lighthouse at dawn, soft light",
    size="1024x1024",
    n=1,
)

item = result.data[0]
if item.b64_json:
    with open("lighthouse.png", "wb") as f:
        f.write(base64.b64decode(item.b64_json))
elif item.url:
    print("Download from:", item.url)  # provider-hosted URLs often expire

TypeScript:

import OpenAI from "openai";
import { writeFile } from "node:fs/promises";

const client = new OpenAI({
  baseURL: process.env.IMAGE_API_BASE_URL,
  apiKey: process.env.IMAGE_API_KEY,
});

const result = await client.images.generate({
  model: process.env.IMAGE_MODEL!,
  prompt: "A watercolor lighthouse at dawn, soft light",
  size: "1024x1024",
});

const item = result.data?.[0];
if (item?.b64_json) {
  await writeFile("lighthouse.png", Buffer.from(item.b64_json, "base64"));
} else if (item?.url) {
  console.log("Download from:", item.url);
}

curl:

curl -s "$IMAGE_API_BASE_URL/images/generations" \
  -H "Authorization: Bearer $IMAGE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "'"$IMAGE_MODEL"'", "prompt": "A watercolor lighthouse at dawn", "size": "1024x1024"}' \
  | jq -r '.data[0].b64_json' | base64 --decode > lighthouse.png

Handling both b64_json and url is the single most useful thing you can do for portability. The base URL rules are the same as for chat: it normally ends in /v1 and the SDK appends /images/generations. See the four ways a base URL goes wrong if you get a 404.

How common providers differ

Provider

Image endpoint

What to know

OpenAI

/v1/images/generations, /v1/images/edits

GPT Image models; base64 only; supports quality, background, output format, compression, streaming. You may need API Organization Verification before using GPT Image models.

Google Gemini (OpenAI compatibility)

images.generate() against https://generativelanguage.googleapis.com/v1beta/openai/

Supported fields are prompt, model, n, size and response_format; other fields are silently ignored unless passed via extra_body.

OpenRouter

POST /api/v1/images (not /images/generations)

Its own Images API; response_format: "url" returns a signed URL that expires after 24 hours, with base64 as a fallback. Some multimodal models generate images through chat completions instead.

LiteLLM proxy (self-hosted gateway)

/v1/images/generations

Routes OpenAI-format image requests to many backends; supported fields depend on the backend it maps to.

LocalAI (self-hosted)

/v1/images/generations

Runs local diffusion backends behind an OpenAI-style route; sizes and quality depend on the model you load.

The OpenRouter row is the clearest example of "compatible but not drop-in": the official SDK's images.generate() posts to /images/generations, so against OpenRouter you call its /api/v1/images endpoint with a plain HTTP client instead. A gateway such as LiteLLM, or the OpenAI API proxy you already use for chat, can hide these differences behind one stable route.

Edits, masks and other gaps

Generation is the well-trodden path. Edits are where compatibility thins out:

  • Edits endpoint: OpenAI's /v1/images/edits takes one or more reference images plus an optional mask. Many compatible providers do not implement it, or accept only a single image.
  • Masks: OpenAI requires the mask to match the source image's format and size and to contain an alpha channel. Providers that support inpainting may use different conventions, so test with a known mask first.
  • Content moderation: every provider applies its own policy. A prompt that passes one provider may be refused by another, and the error format differs. Treat a refusal as a normal response your app must display, not as a transient error to retry.
  • Latency: OpenAI notes complex prompts can take up to 2 minutes. Set client timeouts well above your chat defaults.

These are the image-specific cases of the general pattern described in OpenAI compatibility issues: the route exists, but not every field means the same thing.

Checklist before switching image providers

  1. Confirm the exact endpoint path. /v1/images/generations is the norm, but not universal.
  2. List the provider's image model IDs; chat model IDs will not work on the image route.
  3. Test every size you use. Unsupported sizes return 400 on some providers and are silently changed on others.
  4. Handle both b64_json and url, and download URL results immediately because they usually expire.
  5. Check whether n > 1 works, or loop single requests instead.
  6. Drop OpenAI-only fields (quality, background, output_compression) unless the provider documents them.
  7. Raise timeouts and add retries only for 429 and 5xx, never for policy refusals.
  8. Compare price per image at your real size and quality, not the headline price.

FAQ

Is DALL·E still available through the OpenAI API?

OpenAI's current image guide centers on GPT Image models, and its API reference describes response_format and style as legacy parameters for retired image models. Plan new integrations on GPT Image models and confirm availability on OpenAI's deprecations page.

Can I use the official OpenAI SDK for a non-OpenAI image model?

Yes, if the provider implements /v1/images/generations. Set base_url (Python) or baseURL (TypeScript), the provider's key and its image model ID. If the provider uses a different path, as OpenRouter does, call it with a plain HTTP client.

Why do I get b64_json from one provider and url from another?

OpenAI's GPT Image models always return base64. Other providers default to a URL or let you choose with response_format. Write code that accepts both.

Why does my request work on OpenAI but return 400 elsewhere?

Usually an unsupported size, an OpenAI-only field the provider rejects instead of ignoring, or a chat model ID sent to the image route. Strip the request down to model, prompt and size, then add fields back one at a time.

Does an OpenAI-compatible chat API automatically support images?

No. Many providers implement /v1/chat/completions only. Check the provider's docs for an image route, or list its models and look for image output models. For the broader picture, see what an OpenAI-compatible API covers.