Image Generation
generateImage calls an image-capable provider and returns an ImageResponse. Each generated image can contain data, base64, a hosted url, a mediaType, and an optional revised prompt.
| Result field | Meaning |
|---|---|
data | Binary image bytes when the provider returns downloadable content |
base64 | Encoded image data when the provider returns base64 |
url | Hosted image URL when the provider stores the result |
mediaType | Output type such as PNG, JPEG, or WebP |
revisedPrompt | Provider-adjusted prompt when available |
Generate An Image
let imageModel = OpenAICompatibleProvider(
baseURL: "https://api.openai.com/v1",
apiKey: ProcessInfo.processInfo.environment["OPENAI_API_KEY"]!,
model: "gpt-image-1"
)
let response = try await generateImage(
model: imageModel,
prompt: "A clean app icon for a Swift package named SwiftyAI.",
options: ImageGenerationOptions(
count: 1,
size: .square1024,
quality: .high,
format: .png
)
)
let firstImage = response.images.first
let imageData = firstImage?.dataThe provider chooses whether it returns bytes, base64, or a URL.
Registry Form
After app startup configuration, you can use model strings:
let response = try await generateImage(
model: "openai/gpt-image-1",
prompt: "A product screenshot background for a SwiftyAI demo.",
options: ImageGenerationOptions(
size: .landscape1536x1024,
background: .opaque
)
)Gemini Images
Gemini media models use the same SDK function:
let gemini = GeminiProvider(
apiKey: ProcessInfo.processInfo.environment["GEMINI_API_KEY"]!,
model: "gemini-image-model"
)
let response = try await generateImage(
model: gemini,
prompt: "A simple diagram showing prompt, model, and response."
)Use the model name your provider account supports.
Options
ImageGenerationOptions supports count, size, quality, format, background, compression, aspectRatio, personGeneration, per-request headers, and retryPolicy.
| Option | Use |
|---|---|
size / aspectRatio | Match app surfaces such as icons, banners, cards, or storyboards |
quality | Trade cost/latency for fidelity when the provider supports it |
format / compression | Control storage and upload shape |
background | Request transparent or opaque outputs where supported |
headers / retryPolicy | Add request metadata and recover from transient provider failures |
Some options are provider-specific. Passing an option that a provider does not support may be ignored or rejected by that provider.
Image Middleware
Image generation has its own middleware API:
let taggedImageModel = wrapImageModel(
imageModel,
middleware: [
ImageModelMiddleware { request, next in
var options = request.options
options.headers["X-Feature"] = "image-preview"
return try await next.generate(
prompt: request.prompt,
options: options
)
}
]
)ImageGenerationRequest contains the prompt and options. ImageModelContinuation calls the next middleware or provider. The wrapper returns an AIImageModel.