Generate image
Use generateImage when a provider exposes an image model. The Swift-native convenience accepts the prompt directly and builds an ImageGenerationRequest for you.
import SwiftAISDK
@mainstruct GenerateImageExample { static func main() async throws { let provider = try AIProviders.openAI() let model = try provider.imageModel("gpt-image-1")
let result = try await model.generateImage( "A small watercolor robot reading Swift code.", size: "1024x1024", count: 1 )
print(result.urls.first ?? result.base64Images.first ?? "") }}For image editing, pass input files and an optional mask.
let source = ImageInputFile( data: imageData, mediaType: "image/png", fileName: "source.png")
let result = try await model.generateImage( "Replace the background with a quiet desk.", files: [source])Options
Section titled “Options”Image models expose asynchronous tri-state input capabilities. nil means the
provider does not know whether this model accepts the input, so it should not
be treated as a negative capability:
let filesSupported: Bool? = await model.supportsFileInputslet masksSupported: Bool? = await model.supportsMaskInputsProvider model tables supply known values. Azure and generic compatible image
servers report unknown values. AIImageModelMiddleware can override either
flag asynchronously, including overriding a known value to nil.
sizeandaspectRatiodescribe the output shape.seedrequests deterministic output when a provider supports it.countrequests multiple images.filesandmasksupport image editing.providerOptionscarries provider-specific controls.extraBody,headers,abortSignal,retryPolicy, andtelemetryfollow the shared facade behavior.
Results
Section titled “Results”ImageGenerationResult may contain hosted URLs, base64 images, or both:
urlsbase64ImagesusagewarningsproviderMetadatarequestMetadataresponseMetadatacalls, oneImageGenerationCallper underlying model request, preserving call-scoped images, warnings, usage, provider metadata, and response metadata.
The aggregate URL/base64/usage fields remain convenient for callers that do not
need per-request attribution. Use calls when cost or response metadata must
remain associated with an individual provider call.
If the provider completes without any URL or base64 image, generateImage
throws AINoOutputError. For image failures its calls property preserves the
same per-call warnings, usage, provider metadata, and response metadata, so an
empty aggregate result does not discard diagnostic evidence.