Image Generation with AI SDK
AI Gateway supports image generation through the AI SDK using two approaches: multimodal LLMs that can generate images alongside text, and dedicated image generation models.
You can view all available models that support image generation by using the Image filter at the AI Gateway Models page.
These models can generate both text and images in their responses. They use generateText or streamText functions with special configuration to enable image outputs.
Google's Nano Banana model offers fast, efficient image generation alongside text responses. Images are returned as content parts in result.files.
To save generated images to disk, see Save generated images.
import { generateText } from 'ai';
import 'dotenv/config';
async function main() {
const result = await generateText({
model: 'google/gemini-2.5-flash-image',
prompt: `Render two different images of a snowy plover at dusk looking out at San Francisco Bay`,
});
if (result.text) {
console.log(result.text);
}
console.log('Usage:', JSON.stringify(result.usage, null, 2));
}
main().catch(console.error);import { streamText, type GeneratedFile } from 'ai';
import 'dotenv/config';
async function main() {
const result = streamText({
model: 'google/gemini-2.5-flash-image',
prompt: `Render two images of a golden-crowned kinglet perched on a frost-covered pine branch`,
});
for await (const delta of result.fullStream) {
if (delta.type === 'text-delta') {
process.stdout.write(delta.text);
}
}
console.log('\nUsage:', JSON.stringify(await result.usage, null, 2));
}
main().catch(console.error);Google's Nano Banana Pro model offers state-of-the-art image generation and editing capabilities with higher quality outputs. Images are returned as content parts in result.files.
To save generated images to disk, see Save generated images.
import { generateText } from 'ai';
import 'dotenv/config';
async function main() {
const result = await generateText({
model: 'google/gemini-3-pro-image',
prompt: `Create a detailed illustration of a turquoise-throated puffleg hummingbird resting on a branch covered with dew at sunrise`,
});
if (result.text) {
console.log(result.text);
}
console.log('Usage:', JSON.stringify(result.usage, null, 2));
}
main().catch(console.error);import { streamText, type GeneratedFile } from 'ai';
import 'dotenv/config';
async function main() {
const result = streamText({
model: 'google/gemini-3-pro-image',
prompt: `Generate an artistic rendering of a pond tortoise sleeping on a log in a misty lake at sunset`,
});
for await (const delta of result.fullStream) {
if (delta.type === 'text-delta') {
process.stdout.write(delta.text);
}
}
console.log('\n\nUsage:', JSON.stringify(await result.usage, null, 2));
}
main().catch(console.error);OpenAI's GPT-5 model variants and a few others support multi-modal image generation through a provider-defined tool. The image generation uses gpt-image-1 behind the scenes. Images are returned as tool results in result.staticToolResults (for generateText) or as tool-result events (for streamText).
Learn more about the OpenAI Image Generation Tool in the AI SDK documentation.
import { generateText } from 'ai';
import 'dotenv/config';
import { openai } from '@ai-sdk/openai';
async function main() {
const result = await generateText({
model: 'openai/gpt-5.1-instant',
prompt: `Generate an image of a black shiba inu dog eating a cake in a green grass field`,
tools: {
image_generation: openai.tools.imageGeneration({
outputFormat: 'webp',
quality: 'high',
}),
},
});
// Extract generated images from tool results
for (const toolResult of result.staticToolResults) {
if (toolResult.toolName === 'image_generation') {
const base64Image = toolResult.output.result;
console.log(
'Generated image (base64):',
base64Image.substring(0, 50) + '...',
);
}
}
console.log('Usage:', JSON.stringify(result.usage, null, 2));
}
main().catch(console.error);import { streamText } from 'ai';
import 'dotenv/config';
import { openai } from '@ai-sdk/openai';
async function main() {
const result = streamText({
model: 'openai/gpt-5.1-instant',
prompt: `Generate an image of a corgi puppy playing with colorful balloons in a sunny garden`,
tools: {
image_generation: openai.tools.imageGeneration({
outputFormat: 'webp',
quality: 'high',
}),
},
});
for await (const part of result.fullStream) {
if (part.type === 'tool-result' && !part.dynamic) {
if (part.toolName === 'image_generation') {
const base64Image = part.output.result;
console.log(
'Generated image (base64):',
base64Image.substring(0, 50) + '...',
);
}
}
}
console.log('Usage:', JSON.stringify(await result.usage, null, 2));
}
main().catch(console.error);OpenAI images are returned as base64-encoded strings. To save them to disk, you'll need to decode the base64 string first. For multimodal models that return images in result.files, see Save generated images.
After generating images with multimodal LLMs, you can save them to your local filesystem. Images are available in result.files and include a uint8Array property that you can write directly to disk.
import { generateText } from 'ai';
import fs from 'node:fs';
import path from 'node:path';
import 'dotenv/config';
async function main() {
const result = await generateText({
model: 'google/gemini-2.5-flash-image',
prompt: `Render two different images of a snowy plover at dusk looking out at San Francisco Bay`,
});
// Save generated images to local filesystem
const imageFiles = result.files.filter((f) =>
f.mediaType?.startsWith('image/'),
);
if (imageFiles.length > 0) {
// Create output directory if it doesn't exist
const outputDir = 'output';
fs.mkdirSync(outputDir, { recursive: true });
const timestamp = Date.now();
for (const [index, file] of imageFiles.entries()) {
const extension = file.mediaType?.split('/')[1] || 'png';
const filename = `image-${timestamp}-${index}.${extension}`;
const filepath = path.join(outputDir, filename);
await fs.promises.writeFile(filepath, file.uint8Array);
console.log(`Saved image to ${filepath}`);
}
}
}
main().catch(console.error);These models are specialized for image generation and use the experimental_generateImage function.
Google's Imagen models provide high-quality image generation with fine-grained control over output parameters. Multiple Imagen models are available, including but not limited to:
google/imagen-4.0-ultra-generate-001google/imagen-4.0-generate-001
To save generated images to disk, see Save generated images from dedicated models.
import { experimental_generateImage as generateImage } from 'ai';
import 'dotenv/config';
async function main() {
const result = await generateImage({
model: 'google/imagen-4.0-ultra-generate-001',
prompt: `A majestic Bengal tiger drinking water from a crystal-clear mountain stream at golden hour`,
n: 2,
aspectRatio: '16:9',
});
console.log(`Generated ${result.images.length} image(s)`);
}
main().catch(console.error);Black Forest Labs' Flux models offer advanced image generation with support for various aspect ratios and capabilities. Multiple Flux models are available, including but not limited to:
bfl/flux-2-probfl/flux-kontext-maxbfl/flux-kontext-probfl/flux-pro-1.0-fillbfl/flux-pro-1.1
To save generated images to disk, see Save generated images from dedicated models.
import { experimental_generateImage as generateImage } from 'ai';
import 'dotenv/config';
async function main() {
const result = await generateImage({
model: 'bfl/flux-2-pro',
prompt: `A vibrant coral reef ecosystem with tropical fish swimming around colorful sea anemones`,
aspectRatio: '4:3',
});
console.log(`Generated ${result.images.length} image(s)`);
}
main().catch(console.error);All generated images from dedicated image models are returned as objects containing:
base64: The image as a base64-encoded stringmediaType: The MIME type (e.g.,image/png,image/jpeg,image/webp)
You can save images to disk, display them in your application, or process them further as needed.
import { experimental_generateImage as generateImage } from 'ai';
import fs from 'node:fs';
import path from 'node:path';
import 'dotenv/config';
async function main() {
const result = await generateImage({
model: 'google/imagen-4.0-ultra-generate-001',
prompt: `A peaceful Japanese zen garden with raked sand patterns and carefully placed stones`,
n: 2,
aspectRatio: '16:9',
});
const outputDir = 'output';
fs.mkdirSync(outputDir, { recursive: true });
const timestamp = Date.now();
// Process and save images
for (const [index, image] of result.images.entries()) {
const extension = image.mediaType?.split('/')[1] || 'png';
const filename = `image-${timestamp}-${index}.${extension}`;
const filepath = path.join(outputDir, filename);
// Decode base64 and write to file
const buffer = Buffer.from(image.base64, 'base64');
await fs.promises.writeFile(filepath, buffer);
console.log(`Saved image to ${filepath}`);
}
console.log(
'Provider metadata:',
JSON.stringify(result.providerMetadata, null, 2),
);
}
main().catch(console.error);Was this helpful?