Overview
ThegenerateImage() function generates images from text prompts using image generation models. It supports generating multiple images, different sizes, and both base64 and URL outputs.
Function signature
Parameters
ImageModel
required
The image generation model instance to use.
string
required
The text description of the image to generate. Must not be empty.
number
Number of images to generate. Defaults to 1.
string
Image dimensions. Format and available sizes depend on the provider.Common sizes:
'1024x1024'(square)'1792x1024'(landscape)'1024x1792'(portrait)
ImageProviderOptions
Provider-specific options, namespaced by provider name (e.g.
{ openai: { quality: 'hd' } }).Return value
Returns aPromise<ImageGenerateResult> with the following properties:
GeneratedImage[]
Array of generated images. Length matches the
n parameter.Examples
Basic image generation
Generate multiple images
Custom size
Get base64 image data
Check revised prompt
Save image to file
Generate with style instructions
Error handling
Generate portrait and landscape variants
Model Support
Different providers expose image generation models throughimageModel():
Important notes
Check
revisedPrompt when you want to inspect how the provider expanded or filtered your original prompt.Provider-specific options
OpenAI
Common use cases
- Content Creation: Generate illustrations for articles and blog posts
- Marketing: Create social media graphics and ad visuals
- Product Design: Visualize product concepts and variations
- Game Development: Generate concept art and textures
- Education: Create visual aids and diagrams
- Personal Projects: Generate artwork for presentations or creative projects
Best practices
- Be Specific: Detailed prompts produce better results
- Iterate: Generate multiple variations to find the best result
- Use Style Keywords: Include art style, medium, and technique terms
- Check Revised Prompts: Inspect provider prompt rewrites when outputs differ from your original request
- Handle Errors: Prompts may be rejected for safety reasons
Errors
ThrowsValidationError if:
- Prompt is empty
ProviderErrorif the provider returns an error during image generation- Model encounters an error during generation
- Prompt violates content policy