Type safety for provider options using satisfies
Each provider exports a type for its options: `OpenAILanguageModelResponsesOptions` from '@ai-sdk/openai' and `AnthropicLanguageModelOptions` from '@ai-sdk/anthropic'. Use these types with `satisfies` to get autocomplete and catch typos at build time.
generateImage function basic usage
The generateImage function from the 'ai' package generates images based on a text prompt using an image model. Basic usage: await generateImage({ model: imageModel, prompt: 'description' }). The response includes an image object with the generated image data.
Access image data from generateImage response
The image returned from generateImage can be accessed in two formats: image.base64 provides base64-encoded image data, and image.uint8Array provides Uint8Array binary data.
Specify image size in generateImage
Image size is specified as a string in the format '{width}x{height}', for example '1024x1024'. Models support only specific sizes that vary by model and provider. Pass the size parameter to generateImage: { size: '1024x1024' }.
Specify aspect ratio in generateImage
Aspect ratio is specified as a string in the format '{width}:{height}', for example '16:9'. Models support only specific aspect ratios that vary by model and provider. Pass the aspectRatio parameter to generateImage: { aspectRatio: '16:9' }.
Generate multiple images with generateImage
To generate multiple images at once, use the n parameter: await generateImage({ model, prompt, n: 4 }). This returns an images array instead of a single image object. The AI SDK automatically batches requests as needed since image models have internal limits on images per API call.
Override default batch size for multiple images
Use the maxImagesPerCall parameter to override the SDK's default batch size when generating multiple images. For example: await generateImage({ model, prompt, maxImagesPerCall: 5, n: 10 }) will make 2 API calls of 5 images each instead of using the default batch size.
Provide seed for reproducible image generation
Pass a seed parameter to generateImage to control output: await generateImage({ model, prompt, seed: 1234567890 }). If the model supports seeds, the same seed will produce the same image output.
Provider-specific settings in generateImage
Use the providerOptions parameter to pass provider-specific settings. For example: providerOptions: { openai: { style: 'vivid', quality: 'hd' } satisfies OpenAIImageModelGenerationOptions }. These options become request body properties for the specified provider.
Abort image generation with timeout
Use the abortSignal parameter to abort image generation or set a timeout: await generateImage({ model, prompt, abortSignal: AbortSignal.timeout(1000) }) aborts after 1 second.
Custom headers in generateImage request
Pass custom headers to the image generation request using the headers parameter: await generateImage({ model, prompt, headers: { 'X-Custom-Header': 'custom-value' } }).
Access warnings from generateImage response
Image generation responses may include a warnings property that contains warnings from the model, such as unsupported parameters. Access with: const { image, warnings } = await generateImage({ model, prompt }).
Access provider metadata from generateImage
The generateImage response includes a providerMetadata property containing additional metadata from the provider. The outer key is the provider name (e.g., 'openai'), and the inner values are provider-specific metadata. An 'images' key is always present as an array matching the length of the top-level images array. Example: const revisedPrompt = providerMetadata.openai.images[0]?.revisedPrompt;
NoImageGeneratedError handling
When generateImage fails to generate a valid image, it throws a NoImageGeneratedError. This error can be caught with: if (NoImageGeneratedError.isInstance(error)). The error preserves responses (metadata about model responses including timestamp, model, headers) and cause (the root cause for detailed error handling).
Wrap image models with middleware
Use wrapImageModel and ImageModelV4Middleware to enhance image models, such as setting default values or implementing logging. Example: const model = wrapImageModel({ model: imageModel, middleware: { specificationVersion: 'v3', transformParams: async ({ params }) => ({ ...params, size: params.size ?? '1024x1024' }) } }).