Image generation capabilities using Google's Imagen models for creating high-quality images from text prompts with configurable settings and output options.
Get image generation model instances for creating images from text descriptions.
/**
* Get an image generation model instance
* @param modelId - Google image model identifier
* @param settings - Optional image generation settings
* @returns ImageModelV2 instance
*/
image(modelId: GoogleGenerativeAIImageModelId, settings?: GoogleGenerativeAIImageSettings): ImageModelV2<
GoogleGenerativeAIImageProviderOptions
>;
/**
* Get an image generation model instance (alias for image)
* @param modelId - Google image model identifier
* @param settings - Optional image generation settings
* @returns ImageModelV2 instance
*/
imageModel(modelId: GoogleGenerativeAIImageModelId, settings?: GoogleGenerativeAIImageSettings): ImageModelV2<
GoogleGenerativeAIImageProviderOptions
>;Usage Examples:
import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
// Basic image generation
const result = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: "A beautiful sunset over mountains with a lake in the foreground",
});
console.log("Generated image URL:", result.image.url);
// Using imageModel method (alias for image)
const imageModel = google.imageModel("imagen-3.0-generate-002");
const result2 = await generateImage({
model: imageModel,
prompt: "A futuristic city with flying cars",
});
// Multiple images generation
const result = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: "A cute robot playing with a ball",
n: 3, // Generate 3 images
});
result.images.forEach((image, index) => {
console.log(`Image ${index + 1}:`, image.url);
});Supported Google image generation model identifiers.
type GoogleGenerativeAIImageModelId =
| "imagen-3.0-generate-002"
| (string & {});Model Information:
Configuration options for image generation behavior and output settings.
interface GoogleGenerativeAIImageProviderOptions {
/** Custom settings for the image model */
maxImagesPerCall?: number;
}
interface GoogleGenerativeAIImageSettings {
/** Override the maximum number of images per call (default: 4) */
maxImagesPerCall?: number;
}Usage Examples:
import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
// Custom image generation settings
const imageModel = google.image("imagen-3.0-generate-002");
const result = await generateImage({
model: imageModel,
prompt: "A fantasy landscape with magical creatures",
n: 2,
providerOptions: {
maxImagesPerCall: 2, // Override default maximum
},
});
// Access generated images
for (const image of result.images) {
console.log("Image URL:", image.url);
console.log("Alt text:", image.alt);
}Generate a single image from a text prompt.
import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
const result = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: "A serene Japanese garden with cherry blossoms and a koi pond",
});
// Access the generated image
console.log("Image URL:", result.image.url);
console.log("Alt description:", result.image.alt);Generate multiple variations of images from the same prompt.
import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
const result = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: "A modern office space with natural lighting",
n: 4, // Generate 4 variations
});
// Process all generated images
result.images.forEach((image, index) => {
console.log(`Variation ${index + 1}:`);
console.log(" URL:", image.url);
console.log(" Description:", image.alt);
});
console.log(`Generated ${result.images.length} images total`);Create detailed prompts for specific image characteristics.
import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
// Detailed artistic prompt
const artisticResult = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: `A photorealistic portrait of a wise old wizard with a long white beard,
wearing deep blue robes with golden embroidery, holding a glowing crystal staff,
standing in an ancient library filled with floating books and magical artifacts,
dramatic lighting, high detail, fantasy art style`,
});
// Architectural prompt
const architecturalResult = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: `Modern minimalist house design with floor-to-ceiling windows,
clean geometric lines, surrounded by lush greenery,
natural materials like wood and stone, bright daylight,
architectural photography style`,
});
// Abstract art prompt
const abstractResult = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: `Abstract digital art with flowing organic shapes in vibrant colors,
gradients of blue and purple merging into golden highlights,
dreamlike and ethereal composition, high resolution`,
});Control the maximum number of images that can be generated in a single call.
import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
// Custom provider with specific image settings
const customGoogle = createGoogleGenerativeAI({
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
});
const result = await generateImage({
model: customGoogle.image("imagen-3.0-generate-002"),
prompt: "A collection of geometric patterns",
n: 3,
providerOptions: {
maxImagesPerCall: 3, // Ensure we can generate 3 images
},
});Handle image generation errors and limitations.
import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
try {
const result = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: "A beautiful landscape painting",
n: 5, // This might exceed maxImagesPerCall
});
console.log(`Successfully generated ${result.images.length} images`);
} catch (error) {
if (error.code === "INVALID_ARGUMENT") {
console.error("Invalid request parameters:", error.message);
} else if (error.code === "QUOTA_EXCEEDED") {
console.error("API quota exceeded:", error.message);
} else {
console.error("Image generation failed:", error.message);
}
}import { google } from "@ai-sdk/google";
import { generateImage } from "ai";
import fs from "fs/promises";
async function generateAndSaveImages() {
try {
// Generate multiple image variations
const result = await generateImage({
model: google.image("imagen-3.0-generate-002"),
prompt: `A cozy reading nook by a window with warm sunlight,
comfortable armchair, stacks of books, steaming coffee cup,
indoor plants, soft textures, inviting atmosphere`,
n: 3,
});
// Process and save each image
for (let i = 0; i < result.images.length; i++) {
const image = result.images[i];
console.log(`Processing image ${i + 1}:`);
console.log(` URL: ${image.url}`);
console.log(` Description: ${image.alt}`);
// Optional: Download and save the image
// const response = await fetch(image.url);
// const buffer = await response.arrayBuffer();
// await fs.writeFile(`reading-nook-${i + 1}.png`, Buffer.from(buffer));
}
return result;
} catch (error) {
console.error("Failed to generate images:", error);
throw error;
}
}