Skip to main content

Overview

The image_generation() function provides a unified interface to generate images from text prompts across multiple providers including OpenAI DALL-E, Azure, Bedrock, Vertex AI, and more.

Basic Usage

from litellm import image_generation

response = image_generation(
    model="dall-e-3",
    prompt="A serene mountain landscape at sunset"
)

print(response.data[0].url)

Function Signature

def image_generation(
    prompt: str,
    model: Optional[str] = None,
    n: Optional[int] = None,
    quality: Optional[str] = None,
    response_format: Optional[str] = None,
    size: Optional[str] = None,
    style: Optional[str] = None,
    user: Optional[str] = None,
    timeout: float = 600,
    api_key: Optional[str] = None,
    api_base: Optional[str] = None,
    api_version: Optional[str] = None,
    custom_llm_provider: Optional[str] = None,
    **kwargs
) -> ImageResponse

Parameters

prompt
string
required
The text description of the image you want to generate.
prompt="A futuristic cityscape with flying cars"
model
string
The image generation model to use. If not specified, defaults to dall-e-2.Examples: dall-e-2, dall-e-3, stable-diffusion-xl, amazon.titan-image-generator-v1
n
int
Number of images to generate. Default: 1 (DALL-E 3 only supports n=1)
quality
string
Image quality. Options: "standard" or "hd" (DALL-E 3 only)
size
string
Image dimensions. Options depend on model:
  • DALL-E 2: "256x256", "512x512", "1024x1024"
  • DALL-E 3: "1024x1024", "1024x1792", "1792x1024"
style
string
Image style (DALL-E 3 only). Options: "vivid" or "natural"
response_format
string
Format of the response. Options: "url" (default) or "b64_json"
timeout
float
Request timeout in seconds. Default: 600 (10 minutes)

Response Format

class ImageResponse:
    created: int
    data: List[ImageObject]

class ImageObject:
    url: Optional[str]           # Image URL (if response_format="url")
    b64_json: Optional[str]      # Base64 encoded image (if response_format="b64_json")
    revised_prompt: Optional[str] # DALL-E 3 revised prompt

Examples

Basic Image Generation

from litellm import image_generation

response = image_generation(
    model="dall-e-3",
    prompt="A cute baby sea otter wearing a beret",
    size="1024x1024",
    quality="standard"
)

image_url = response.data[0].url
print(f"Generated image: {image_url}")

# DALL-E 3 often revises prompts
if response.data[0].revised_prompt:
    print(f"Revised prompt: {response.data[0].revised_prompt}")

Multiple Images (DALL-E 2)

from litellm import image_generation

response = image_generation(
    model="dall-e-2",
    prompt="A modern office workspace",
    n=3,  # Generate 3 images
    size="512x512"
)

for i, image in enumerate(response.data):
    print(f"Image {i + 1}: {image.url}")

High Quality Images

from litellm import image_generation

response = image_generation(
    model="dall-e-3",
    prompt="A photorealistic portrait of a wise old wizard",
    quality="hd",  # High definition
    size="1024x1024",
    style="natural"  # Natural style (vs vivid)
)

print(response.data[0].url)

Base64 Response Format

from litellm import image_generation
import base64
from PIL import Image
import io

response = image_generation(
    model="dall-e-3",
    prompt="A cyberpunk street scene at night",
    response_format="b64_json"  # Get base64 instead of URL
)

# Decode and save image
image_data = base64.b64decode(response.data[0].b64_json)
image = Image.open(io.BytesIO(image_data))
image.save("generated_image.png")

Different Providers

from litellm import image_generation

response = image_generation(
    model="dall-e-3",
    prompt="A sunset over mountains",
    size="1024x1024"
)

Async Image Generation

import asyncio
from litellm import aimage_generation

async def generate():
    response = await aimage_generation(
        model="dall-e-3",
        prompt="A futuristic robot",
        size="1024x1024"
    )
    return response.data[0].url

url = asyncio.run(generate())
print(url)

Concurrent Image Generation

import asyncio
from litellm import aimage_generation

async def generate_multiple():
    prompts = [
        "A peaceful garden",
        "A bustling city",
        "A mystical forest"
    ]
    
    tasks = [
        aimage_generation(model="dall-e-3", prompt=p)
        for p in prompts
    ]
    
    responses = await asyncio.gather(*tasks)
    
    for prompt, response in zip(prompts, responses):
        print(f"{prompt}: {response.data[0].url}")

asyncio.run(generate_multiple())

Advanced Usage

Download and Save Images

from litellm import image_generation
import requests
from PIL import Image
from io import BytesIO

response = image_generation(
    model="dall-e-3",
    prompt="A majestic eagle in flight",
    size="1024x1024"
)

# Download image
image_url = response.data[0].url
image_response = requests.get(image_url)
image = Image.open(BytesIO(image_response.content))

# Save image
image.save("eagle.png")
print("Image saved as eagle.png")

Batch Processing

from litellm import image_generation
import time

prompts = [
    "A red sports car",
    "A blue mountain bike",
    "A yellow sailboat"
]

for i, prompt in enumerate(prompts):
    print(f"Generating image {i + 1}...")
    
    response = image_generation(
        model="dall-e-2",
        prompt=prompt,
        size="512x512"
    )
    
    print(f"Image {i + 1}: {response.data[0].url}")
    
    # Rate limiting
    if i < len(prompts) - 1:
        time.sleep(2)

Style Variations

from litellm import image_generation

prompt = "A portrait of a scientist"

# Vivid style (more hyper-real and dramatic)
vivid_response = image_generation(
    model="dall-e-3",
    prompt=prompt,
    style="vivid"
)

# Natural style (more natural, less hyper-real)
natural_response = image_generation(
    model="dall-e-3",
    prompt=prompt,
    style="natural"
)

print(f"Vivid style: {vivid_response.data[0].url}")
print(f"Natural style: {natural_response.data[0].url}")

Different Aspect Ratios (DALL-E 3)

from litellm import image_generation

prompt = "A panoramic view of a beach"

# Square
square = image_generation(
    model="dall-e-3",
    prompt=prompt,
    size="1024x1024"
)

# Wide (landscape)
wide = image_generation(
    model="dall-e-3",
    prompt=prompt,
    size="1792x1024"
)

# Tall (portrait)
tall = image_generation(
    model="dall-e-3",
    prompt=prompt,
    size="1024x1792"
)

print(f"Square: {square.data[0].url}")
print(f"Wide: {wide.data[0].url}")
print(f"Tall: {tall.data[0].url}")

Error Handling

from litellm import image_generation
from litellm.exceptions import (
    AuthenticationError,
    BadRequestError,
    RateLimitError,
    ContentPolicyViolationError
)

try:
    response = image_generation(
        model="dall-e-3",
        prompt="A beautiful landscape"
    )
    print(response.data[0].url)
    
except AuthenticationError:
    print("Invalid API key")
except ContentPolicyViolationError:
    print("Prompt violates content policy")
except RateLimitError:
    print("Rate limit exceeded")
except BadRequestError as e:
    print(f"Invalid request: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Use Cases

Marketing Materials

from litellm import image_generation

products = [
    "eco-friendly water bottle",
    "wireless bluetooth headphones",
    "ergonomic office chair"
]

for product in products:
    response = image_generation(
        model="dall-e-3",
        prompt=f"Professional product photography of a {product}, white background, studio lighting",
        quality="hd",
        size="1024x1024"
    )
    print(f"{product}: {response.data[0].url}")

Social Media Content

from litellm import image_generation

# Instagram post (square)
instagram = image_generation(
    model="dall-e-3",
    prompt="Vibrant abstract art with bright colors and geometric shapes",
    size="1024x1024",
    style="vivid"
)

# Instagram story (portrait)
story = image_generation(
    model="dall-e-3",
    prompt="Motivational quote background with gradient colors",
    size="1024x1792"
)

print(f"Instagram post: {instagram.data[0].url}")
print(f"Instagram story: {story.data[0].url}")

Creative Variations

from litellm import image_generation

base_prompt = "A coffee shop interior"
styles = [
    "modern minimalist",
    "cozy rustic",
    "futuristic sci-fi",
    "vintage 1920s"
]

for style in styles:
    response = image_generation(
        model="dall-e-2",
        prompt=f"{base_prompt}, {style} style",
        size="512x512"
    )
    print(f"{style}: {response.data[0].url}")

Best Practices

  1. Be specific: Detailed prompts produce better results
  2. Specify style: Include artistic style, mood, lighting preferences
  3. Handle content policy: Catch ContentPolicyViolationError for rejected prompts
  4. Consider costs: DALL-E 3 HD images cost more than standard
  5. Save images: Download and store images as URLs expire
  6. Rate limits: Respect provider rate limits, especially with batch processing
  7. Aspect ratio: Choose size based on intended use case

Prompt Tips

Good Prompts

# Specific and detailed
"A photorealistic portrait of an elderly woman with silver hair, \
wearing round glasses, gentle smile, warm lighting, soft focus background"

# Include artistic style
"A landscape painting in the style of Claude Monet, \
water lilies on a pond, impressionist brushstrokes, pastel colors"

# Specify mood and atmosphere
"A dark, moody photograph of a foggy forest at dusk, \
mysterious atmosphere, muted colors, cinematic lighting"

Avoid

# Too vague
"A person"  # Not specific enough

# Trademarked content
"Mickey Mouse character"  # May be rejected

# Copyrighted styles
"In the style of [living artist]"  # May be rejected

Troubleshooting

Content Policy Violations

If your prompt is rejected:
  • Make prompts more general and less specific about people
  • Avoid trademarked characters or brands
  • Remove potentially offensive language
  • Try rephrasing sensitive topics

Image Quality Issues

  • Use DALL-E 3 with quality="hd" for best quality
  • Be more specific in prompts about desired details
  • Specify lighting, composition, and style preferences

Rate Limits

import time
from litellm import image_generation
from litellm.exceptions import RateLimitError

def generate_with_retry(prompt: str, max_retries: int = 3):
    for attempt in range(max_retries):
        try:
            return image_generation(model="dall-e-3", prompt=prompt)
        except RateLimitError:
            if attempt < max_retries - 1:
                wait_time = 2 ** attempt  # Exponential backoff
                print(f"Rate limited. Waiting {wait_time}s...")
                time.sleep(wait_time)
            else:
                raise

Build docs developers (and LLMs) love