Skip to main content

Image Generation

The AI SDK provides the generateImage function to generate images based on a given prompt using an image model.
You can access the image data using the base64 or uint8Array properties:

Settings

Size and Aspect Ratio

Depending on the model, you can either specify the size or the aspect ratio.

Size

The size is specified as a string in the format {width}x{height}. Models only support a few sizes, and the supported sizes are different for each model and provider.

Aspect Ratio

The aspect ratio is specified as a string in the format {width}:{height}. Models only support a few aspect ratios, and the supported aspect ratios are different for each model and provider.

Generating Multiple Images

generateImage also supports generating multiple images at once:
generateImage will automatically call the model as often as needed (in parallel) to generate the requested number of images.
Each image model has an internal limit on how many images it can generate in a single API call. The AI SDK manages this automatically by batching requests appropriately when you request multiple images using the n parameter. By default, the SDK uses provider-documented limits (for example, DALL-E 3 can only generate 1 image per call, while DALL-E 2 supports up to 10). If needed, you can override this behavior using the maxImagesPerCall setting:

Providing a Seed

Some image models support seeds for reproducible generation:

Provider Options

Image model settings can be configured using providerOptions for provider-specific parameters:

Image Editing

Image-to-Image

Some models support image-to-image generation, where you provide a source image along with a prompt:

Inpainting with Masks

You can also provide a mask to specify which parts of the image should be regenerated:

Response Information

The generateImage function returns comprehensive response information:

Retries

The generateImage function accepts an optional maxRetries parameter that you can use to set the maximum number of retries. It defaults to 2 retries (3 attempts in total). You can set it to 0 to disable retries.

Abort Signals and Timeouts

The generateImage function accepts an optional abortSignal parameter of type AbortSignal that you can use to abort the generation process or set a timeout.

Custom Headers

The generateImage function accepts an optional headers parameter that you can use to add custom headers to the request.

Image Providers & Models

Several providers offer image generation models: