Image generation

Generate and edit images with a shared ImageModel API.

Every image provider conforms to ImageModel and works with generateImage.

let result = try await generateImage(
  model: OpenAIImageModel("gpt-image-2"),
  prompt: "A watercolor fox in a snowy forest",
  size: "1024x1024"
)

try result.image.write(to: outputURL)

result.image is the first image as Data. The complete response is in result.images, and providers that rewrite prompts report them in result.revisedPrompts.

Edit an image

Pass source images through the same call. OpenAI automatically switches to its edits endpoint.

let edited = try await generateImage(
  model: OpenAIImageModel("gpt-image-2"),
  prompt: "Make the sky stormy",
  images: [ImageContent(data: source, mediaType: "image/png")]
)

Options

The shared options are n, size, aspectRatio, seed, maxRetries, and providerOptions. Providers use the settings they support:

let result = try await generateImage(
  model: FalImageModel("fal-ai/flux/schnell"),
  prompt: "An isometric library at night",
  aspectRatio: "16:9",
  seed: 42,
  providerOptions: ["fal": ["num_inference_steps": 4]]
)

Models

ProviderModel typeKey
OpenAIOpenAIImageModelOPENAI_API_KEY
falFalImageModelFAL_API_KEY or FAL_KEY
LumaLumaImageModelLUMA_API_KEY
ReplicateReplicateImageModelREPLICATE_API_TOKEN