> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/BerriAI/litellm/llms.txt
> Use this file to discover all available pages before exploring further.

# image_generation()

> Complete API reference for the image_generation() function

## Overview

Generate images from text prompts using any of LiteLLM's supported image generation providers. Returns responses in OpenAI format.

## Function Signature

```python theme={null}
def image_generation(
    prompt: str,
    model: Optional[str] = None,
    n: Optional[int] = None,
    quality: Optional[Union[str, ImageGenerationRequestQuality]] = 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

### Required Parameters

<ParamField path="prompt" type="string" required>
  Text description of the desired image(s).

  ```python theme={null}
  prompt="A serene landscape with mountains and a lake at sunset"
  ```
</ParamField>

### Optional Parameters

<ParamField path="model" type="string">
  The model to use for image generation.

  Examples:

  * `dall-e-3` (OpenAI)
  * `dall-e-2` (OpenAI)
  * `stability-ai/stable-diffusion-xl-base-1.0` (Bedrock)
  * `imagegeneration@006` (Vertex AI)
</ParamField>

<ParamField path="n" type="int" default="1">
  Number of images to generate.

  Note: dall-e-3 only supports n=1
</ParamField>

<ParamField path="size" type="string">
  Size of generated images.

  For DALL-E 3:

  * `"1024x1024"` (default)
  * `"1792x1024"`
  * `"1024x1792"`

  For DALL-E 2:

  * `"256x256"`
  * `"512x512"`
  * `"1024x1024"`
</ParamField>

<ParamField path="quality" type="string" default="standard">
  Quality of the image (DALL-E 3 only).

  Options:

  * `"standard"`: Standard quality
  * `"hd"`: Higher quality, more detailed
</ParamField>

<ParamField path="style" type="string" default="vivid">
  Style of generated images (DALL-E 3 only).

  Options:

  * `"vivid"`: Hyper-real and dramatic images
  * `"natural"`: More natural, less hyper-real
</ParamField>

<ParamField path="response_format" type="string" default="url">
  Format of the returned image.

  Options:

  * `"url"`: Returns a URL to the image
  * `"b64_json"`: Returns base64 encoded JSON
</ParamField>

<ParamField path="user" type="string">
  Unique identifier for your end-user.
</ParamField>

<ParamField path="timeout" type="float" default="600">
  Request timeout in seconds (default 10 minutes).
</ParamField>

### API Configuration

<ParamField path="api_key" type="string">
  API key for the provider.
</ParamField>

<ParamField path="api_base" type="string">
  Base URL for the API endpoint.
</ParamField>

<ParamField path="api_version" type="string">
  API version to use.
</ParamField>

<ParamField path="custom_llm_provider" type="string">
  Override provider detection.
</ParamField>

## Response

### ImageResponse

<ResponseField name="created" type="int">
  Unix timestamp of when the image was created.
</ResponseField>

<ResponseField name="data" type="List[ImageObject]">
  List of generated image objects.

  <Expandable title="ImageObject">
    <ResponseField name="url" type="string" optional>
      URL of the generated image (if response\_format="url").
    </ResponseField>

    <ResponseField name="b64_json" type="string" optional>
      Base64 encoded image (if response\_format="b64\_json").
    </ResponseField>

    <ResponseField name="revised_prompt" type="string" optional>
      The revised prompt used by DALL-E 3 (if applicable).
    </ResponseField>
  </Expandable>
</ResponseField>

## Usage Examples

### Basic Image Generation

```python theme={null}
import litellm

response = litellm.image_generation(
    prompt="A cute baby sea otter",
    model="dall-e-3"
)

print(response.data[0].url)
```

### High Quality Image

```python theme={null}
import litellm

response = litellm.image_generation(
    prompt="A futuristic cityscape at night",
    model="dall-e-3",
    quality="hd",
    size="1792x1024",
    style="vivid"
)

print(response.data[0].url)
print(response.data[0].revised_prompt)  # See how DALL-E revised your prompt
```

### Multiple Images (DALL-E 2)

```python theme={null}
import litellm

response = litellm.image_generation(
    prompt="A white siamese cat",
    model="dall-e-2",
    n=4,
    size="512x512"
)

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

### Async Image Generation

```python theme={null}
import litellm
import asyncio

async def main():
    response = await litellm.aimage_generation(
        prompt="A serene mountain landscape",
        model="dall-e-3"
    )
    print(response.data[0].url)

asyncio.run(main())
```

### Base64 Response

```python theme={null}
import litellm
import base64
from PIL import Image
from io import BytesIO

response = litellm.image_generation(
    prompt="A colorful abstract painting",
    model="dall-e-2",
    response_format="b64_json"
)

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

## Provider Examples

### OpenAI DALL-E

```python theme={null}
import litellm

# DALL-E 3
response = litellm.image_generation(
    prompt="A photorealistic cat",
    model="dall-e-3",
    size="1024x1024",
    quality="hd"
)

# DALL-E 2
response = litellm.image_generation(
    prompt="A photorealistic cat",
    model="dall-e-2",
    n=2,
    size="512x512"
)
```

### Azure OpenAI

```python theme={null}
import litellm

response = litellm.image_generation(
    prompt="A beautiful sunset",
    model="azure/dall-e-3",
    api_key="your-azure-key",
    api_base="https://your-endpoint.openai.azure.com/",
    api_version="2024-02-01"
)
```

### AWS Bedrock

```python theme={null}
import litellm

response = litellm.image_generation(
    prompt="A majestic eagle in flight",
    model="bedrock/stability.stable-diffusion-xl-v1",
    # Bedrock-specific params
    width=1024,
    height=1024,
    cfg_scale=7.0,
    steps=50
)
```

### Vertex AI

```python theme={null}
import litellm

response = litellm.image_generation(
    prompt="A cyberpunk city",
    model="vertex_ai/imagegeneration@006",
    # Vertex-specific params
    number_of_images=1,
    aspect_ratio="1:1"
)
```

### Replicate

```python theme={null}
import litellm

response = litellm.image_generation(
    prompt="A fantasy castle",
    model="replicate/stability-ai/sdxl",
    custom_llm_provider="replicate"
)
```

## Saving Images

### Save from URL

```python theme={null}
import litellm
import requests
from PIL import Image
from io import BytesIO

response = litellm.image_generation(
    prompt="A peaceful garden",
    model="dall-e-3"
)

# Download and save
image_url = response.data[0].url
image_response = requests.get(image_url)
image = Image.open(BytesIO(image_response.content))
image.save("generated_image.png")
```

### Save from Base64

```python theme={null}
import litellm
import base64

response = litellm.image_generation(
    prompt="An abstract artwork",
    model="dall-e-2",
    response_format="b64_json"
)

# Save directly
with open("image.png", "wb") as f:
    f.write(base64.b64decode(response.data[0].b64_json))
```

## Error Handling

```python theme={null}
import litellm
from litellm import (
    BadRequestError,
    AuthenticationError,
    RateLimitError,
    ContentPolicyViolationError
)

try:
    response = litellm.image_generation(
        prompt="A beautiful landscape",
        model="dall-e-3"
    )
except ContentPolicyViolationError as e:
    print(f"Content policy violation: {e}")
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
except RateLimitError as e:
    print(f"Rate limit exceeded: {e}")
except BadRequestError as e:
    print(f"Bad request: {e}")
except Exception as e:
    print(f"An error occurred: {e}")
```

## Supported Providers

LiteLLM supports image generation from:

* **OpenAI**: DALL-E 2, DALL-E 3
* **Azure OpenAI**: DALL-E 2, DALL-E 3
* **AWS Bedrock**: Stable Diffusion, Titan Image Generator
* **Google Vertex AI**: Imagen 2, Imagen 3
* **Replicate**: Stable Diffusion XL, Flux, and more
* **Together AI**: Various Stable Diffusion models

See [Image Generation Providers](https://docs.litellm.ai/docs/providers/) for the complete list.

## Related

* [aimage\_generation()](/api/image-generation) - Async version
* [Completion API](/api/completion)
* [Image Generation Guide](https://docs.litellm.ai/docs/image_generation)
