📄 ai-sdk/docs/ai-sdk-core/image-generation

File: image-generation.md | Updated: 11/15/2025

Source: https://ai-sdk.dev/docs/ai-sdk-core/image-generation

AI SDK

Menu

v5 (Latest)

AI SDK 5.x

AI SDK by Vercel

AI SDK 6 Beta

Foundations

Overview

Providers and Models

Prompts

Tools

Streaming

Getting Started

Navigating the Library

Next.js App Router

Next.js Pages Router

Svelte

Vue.js (Nuxt)

Node.js

Expo

Agents

Agents

Building Agents

Workflow Patterns

Loop Control

AI SDK Core

Overview

Generating Text

Generating Structured Data

Tool Calling

Model Context Protocol (MCP) Tools

Prompt Engineering

Settings

Embeddings

Image Generation

Transcription

Speech

Language Model Middleware

Provider & Model Management

Error Handling

Testing

Telemetry

AI SDK UI

Overview

Chatbot

Chatbot Message Persistence

Chatbot Resume Streams

Chatbot Tool Usage

Generative User Interfaces

Completion

Object Generation

Streaming Custom Data

Error Handling

Transport

Reading UIMessage Streams

Message Metadata

Stream Protocols

AI SDK RSC

Advanced

Reference

AI SDK Core

AI SDK UI

AI SDK RSC

Stream Helpers

AI SDK Errors

Migration Guides

Troubleshooting

Copy markdown

Image Generation

==========================================================================================

Image generation is an experimental feature.

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',});

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

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.

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',});
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.

import { experimental_generateImage as generateImage } from 'ai';import { vertex } from '@ai-sdk/google-vertex';
const { image } = await generateImage({  model: vertex.image('imagen-3.0-generate-002'),  prompt: 'Santa Claus driving a Cadillac',  aspectRatio: '16:9',});

Generating Multiple Images

generateImage also supports generating multiple images at once:

import { experimental_generateImage as generateImage } from 'ai';import { openai } from '@ai-sdk/openai';
const { images } = await generateImage({  model: openai.image('dall-e-2'),  prompt: 'Santa Claus driving a Cadillac',  n: 4, // number of images to generate});

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 when generating your image. This is particularly useful when working with new or custom models where the default batch size might not be optimal:

const { images } = await generateImage({  model: openai.image('dall-e-2'),  prompt: 'Santa Claus driving a Cadillac',  maxImagesPerCall: 5, // Override the default batch size  n: 10, // Will make 2 calls of 5 images each});

Providing a Seed

You can provide a seed to the generateImage function to control the output of the image generation process. If supported by the model, the same seed will always produce the same image.

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',  seed: 1234567890,});

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.

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',  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'),  prompt: 'Santa Claus driving a Cadillac',  headers: { 'X-Custom-Header': 'custom-value' },});

Warnings

If the model returns warnings, e.g. for unsupported parameters, they will be available in the warnings property of the response.

const { image, warnings } = await generateImage({  model: openai.image('dall-e-3'),  prompt: 'Santa Claus driving a Cadillac',});

Additional provider-specific meta data

Some providers expose additional meta data for the result overall or per image.

const prompt = 'Santa Claus driving a Cadillac';
const { image, providerMetadata } = await generateImage({  model: openai.image('dall-e-3'),  prompt,});
const revisedPrompt = providerMetadata.openai.images[0]?.revisedPrompt;
console.log({  prompt,  revisedPrompt,});

The outer key of the returned providerMetadata is the provider name. The inner values are the metadata. An images key is always present in the metadata and is an array with the same length as the top level images key.

Error Handling

When generateImage cannot generate a valid image, it throws a AI_NoImageGeneratedError .

This error occurs when the AI provider fails to generate an image. It can arise due to the following reasons:

  • The model failed to generate a response
  • The model generated a response that could not be parsed

The error preserves the following information to help you log the issue:

  • responses: Metadata about the image model responses, including timestamp, model, and headers.

  • cause: The cause of the error. You can use this for more detailed error handling

    import { generateImage, NoImageGeneratedError } from 'ai'; try { await generateImage({ model, prompt });} catch (error) { if (NoImageGeneratedError.isInstance(error)) { console.log('NoImageGeneratedError'); console.log('Cause:', error.cause); console.log('Responses:', error.responses); }}

Generating Images with Language Models


Some language models such as Google gemini-2.5-flash-image-preview support multi-modal outputs including images. With such models, you can access the generated images using the files property of the response.

import { google } from '@ai-sdk/google';import { generateText } from 'ai';
const result = await generateText({  model: google('gemini-2.5-flash-image-preview'),  prompt: 'Generate an image of a comic cat',});
for (const file of result.files) {  if (file.mediaType.startsWith('image/')) {    // The file object provides multiple data formats:    // Access images as base64 string, Uint8Array binary data, or check type    // - file.base64: string (data URL format)    // - file.uint8Array: Uint8Array (binary data)    // - file.mediaType: string (e.g. "image/png")  }}

Image Models


| Provider | Model | Support sizes (width x height) or aspect ratios (width : height) | | --- | --- | --- | | xAI Grok | grok-2-image | 1024x768 (default) | | OpenAI | gpt-image-1 | 1024x1024, 1536x1024, 1024x1536 | | OpenAI | dall-e-3 | 1024x1024, 1792x1024, 1024x1792 | | OpenAI | dall-e-2 | 256x256, 512x512, 1024x1024 | | Amazon Bedrock | amazon.nova-canvas-v1:0 | 320-4096 (multiples of 16), 1:4 to 4:1, max 4.2M pixels | | Fal | fal-ai/flux/dev | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Fal | fal-ai/flux-lora | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Fal | fal-ai/fast-sdxl | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Fal | fal-ai/flux-pro/v1.1-ultra | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Fal | fal-ai/ideogram/v2 | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Fal | fal-ai/recraft-v3 | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Fal | fal-ai/stable-diffusion-3.5-large | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Fal | fal-ai/hyper-sdxl | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | DeepInfra | stabilityai/sd3.5 | 1:1, 16:9, 1:9, 3:2, 2:3, 4:5, 5:4, 9:16, 9:21 | | DeepInfra | black-forest-labs/FLUX-1.1-pro | 256-1440 (multiples of 32) | | DeepInfra | black-forest-labs/FLUX-1-schnell | 256-1440 (multiples of 32) | | DeepInfra | black-forest-labs/FLUX-1-dev | 256-1440 (multiples of 32) | | DeepInfra | black-forest-labs/FLUX-pro | 256-1440 (multiples of 32) | | DeepInfra | stabilityai/sd3.5-medium | 1:1, 16:9, 1:9, 3:2, 2:3, 4:5, 5:4, 9:16, 9:21 | | DeepInfra | stabilityai/sdxl-turbo | 1:1, 16:9, 1:9, 3:2, 2:3, 4:5, 5:4, 9:16, 9:21 | | Replicate | black-forest-labs/flux-schnell | 1:1, 2:3, 3:2, 4:5, 5:4, 16:9, 9:16, 9:21, 21:9 | | Replicate | recraft-ai/recraft-v3 | 1024x1024, 1365x1024, 1024x1365, 1536x1024, 1024x1536, 1820x1024, 1024x1820, 1024x2048, 2048x1024, 1434x1024, 1024x1434, 1024x1280, 1280x1024, 1024x1707, 1707x1024 | | Google | imagen-3.0-generate-002 | 1:1, 3:4, 4:3, 9:16, 16:9 | | Google Vertex | imagen-3.0-generate-002 | 1:1, 3:4, 4:3, 9:16, 16:9 | | Google Vertex | imagen-3.0-fast-generate-001 | 1:1, 3:4, 4:3, 9:16, 16:9 | | Fireworks | accounts/fireworks/models/flux-1-dev-fp8 | 1:1, 2:3, 3:2, 4:5, 5:4, 16:9, 9:16, 9:21, 21:9 | | Fireworks | accounts/fireworks/models/flux-1-schnell-fp8 | 1:1, 2:3, 3:2, 4:5, 5:4, 16:9, 9:16, 9:21, 21:9 | | Fireworks | accounts/fireworks/models/playground-v2-5-1024px-aesthetic | 640x1536, 768x1344, 832x1216, 896x1152, 1024x1024, 1152x896, 1216x832, 1344x768, 1536x640 | | Fireworks | accounts/fireworks/models/japanese-stable-diffusion-xl | 640x1536, 768x1344, 832x1216, 896x1152, 1024x1024, 1152x896, 1216x832, 1344x768, 1536x640 | | Fireworks | accounts/fireworks/models/playground-v2-1024px-aesthetic | 640x1536, 768x1344, 832x1216, 896x1152, 1024x1024, 1152x896, 1216x832, 1344x768, 1536x640 | | Fireworks | accounts/fireworks/models/SSD-1B | 640x1536, 768x1344, 832x1216, 896x1152, 1024x1024, 1152x896, 1216x832, 1344x768, 1536x640 | | Fireworks | accounts/fireworks/models/stable-diffusion-xl-1024-v1-0 | 640x1536, 768x1344, 832x1216, 896x1152, 1024x1024, 1152x896, 1216x832, 1344x768, 1536x640 | | Luma | photon-1 | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Luma | photon-flash-1 | 1:1, 3:4, 4:3, 9:16, 16:9, 9:21, 21:9 | | Together.ai | stabilityai/stable-diffusion-xl-base-1.0 | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-dev | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-dev-lora | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-schnell | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-canny | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-depth | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-redux | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1.1-pro | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-pro | 512x512, 768x768, 1024x1024 | | Together.ai | black-forest-labs/FLUX.1-schnell-Free | 512x512, 768x768, 1024x1024 |

Above are a small subset of the image models supported by the AI SDK providers. For more, see the respective provider documentation.

On this page

Image Generation

Settings

Size and Aspect Ratio

Size

Aspect Ratio

Generating Multiple Images

Providing a Seed

Provider-specific Settings

Abort Signals and Timeouts

Custom Headers

Warnings

Additional provider-specific meta data

Error Handling

Generating Images with Language Models

Image Models

Deploy and Scale AI Apps with Vercel.

Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.

Trusted by industry leaders:

  • OpenAI
  • Photoroom
  • leonardo-ai Logoleonardo-ai Logo
  • zapier Logozapier Logo

Talk to an expert