Image Generation
The AI SDK provides the generateImage
function to generate images based on a given prompt using an image model.
import { experimental_generateImage as generateImage } from 'ai';import { openai } from '@ai-sdk/openai';
const { image } = await generateImage({ model: openai.image('dall-e-3'), prompt: 'Santa Claus driving a Cadillac', size: '1024x1024',});
You can access the image data using the base64
or uint8Array
properties:
const base64 = image.base64; // base64 image dataconst uint8Array = image.uint8Array; // Uint8Array image data
Generating Multiple Images
generateImage
also supports generating multiple images at once:
const { images } = await generateImage({ model: openai.image('dall-e-3'), prompt: 'Santa Claus driving a Cadillac', n: 4, // number of images to generate});
Provider-specific Settings
Image models often have provider- or even model-specific settings.
You can pass such settings to the generateImage
function
using the providerOptions
parameter. The options for the provider
(openai
in the example below) become request body properties.
const { image } = await generateImage({ model: openai.image('dall-e-3'), prompt: 'Santa Claus driving a Cadillac', size: '1024x1024', providerOptions: { openai: { style: 'vivid', quality: 'hd' }, },});
Abort Signals and Timeouts
generateImage
accepts an optional abortSignal
parameter of
type AbortSignal
that you can use to abort the image generation process or set a timeout.
import { openai } from '@ai-sdk/openai';import { experimental_generateImage as generateImage } from 'ai';
const { image } = await generateImage({ model: openai.image('dall-e-3'), prompt: 'Santa Claus driving a Cadillac', abortSignal: AbortSignal.timeout(1000), // Abort after 1 second});
Custom Headers
generateImage
accepts an optional headers
parameter of type Record<string, string>
that you can use to add custom headers to the image generation request.
import { openai } from '@ai-sdk/openai';import { experimental_generateImage as generateImage } from 'ai';
const { image } = await generateImage({ model: openai.image('dall-e-3'), value: 'sunny day at the beach', headers: { 'X-Custom-Header': 'custom-value' },});