SwiftyAISwiftyAI

Search documentation

Find a docs page by title or section

3

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 fieldMeaning
dataBinary image bytes when the provider returns downloadable content
base64Encoded image data when the provider returns base64
urlHosted image URL when the provider stores the result
mediaTypeOutput type such as PNG, JPEG, or WebP
revisedPromptProvider-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?.data

The 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.

OptionUse
size / aspectRatioMatch app surfaces such as icons, banners, cards, or storyboards
qualityTrade cost/latency for fidelity when the provider supports it
format / compressionControl storage and upload shape
backgroundRequest transparent or opaque outputs where supported
headers / retryPolicyAdd 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.