generateImage
Generate or edit images.
Generates images from a prompt, or edits an existing one when you pass image
data. n asks for several at once; when it exceeds what the provider allows
per call, the request is split into batches automatically.
let result = try await generateImage(
model: OpenAIImageModel("gpt-image-2"),
prompt: "A red bicycle against a white wall",
size: "1024x1024"
)
let png = result.images[0].bytesSignature
func generateImage(
model: any ImageModel,
prompt: String,
images: [ImageContent] = [],
n: Int = 1,
size: String? = nil,
aspectRatio: String? = nil,
seed: Int? = nil,
providerOptions: JSONValue? = nil,
maxImagesPerCall: Int? = nil,
maxRetries: Int = 2
) async throws -> GenerateImageResultDefined in Sources/AI/Core/ImageGeneration.swift.
Parameters
| Parameter | Type | Default |
|---|---|---|
model | any ImageModel | required |
prompt | String | required |
images | [ImageContent] | [] |
n | Int | 1 |
size | String? | nil |
aspectRatio | String? | nil |
seed | Int? | nil |
providerOptions | JSONValue? | nil |
maxImagesPerCall | Int? | nil |
maxRetries | Int | 2 |
Returns
GenerateImageResult with images as GeneratedFile values, exposing
base64 and bytes, plus providerMetadata.