<aside> 💡

The LiteLLM API key also gives you access to image generation models like Gemini 2.5 Flash Image. The easiest way to generate images is using the openai python library, like in the example below.

</aside>

Example using Terminal


curl -s '<https://litellm.sph-prod.ethz.ch/v1/images/generations>' \\
  -H 'Authorization: Bearer YOUR-API-KEY' \\
  -H 'Content-Type: application/json' \\
  -d '{
    "model": "openrouter/google/gemini-2.5-flash-image",
    "prompt": "A cute baby sea otter",
    "n": 1,
    "size": "512x512"
  }'

This returns either returns your image b64 encoded image or as a URL. It is possible to decoded the image and save it using just shell/Powershell. However, the example below provides a more robust Python code which handles both cases.

Examples using Python


Here is an example which handles both a b64 encoded response and an image URL response.


import openai
import base64

# Don't forget to fill out your API key
API_KEY = "YOUR_KEY"

client = openai.OpenAI(api_key=API_KEY,base_url="<https://litellm.sph-prod.ethz.ch/v1>") 

# Specify what you want on your image and the model you want to use
prompt = "A picture of a cat."
model = "openrouter/google/gemini-2.5-flash-image"

# Sending a request to the model
result = client.images.generate(
    model=model,
    prompt=prompt,
    size="512x512",
)

# Access the generated image
image_data = result.data[0]
if image_data.b64_json:
    # Base64 encoded image
    image_bytes = base64.b64decode(image_data.b64_json)
    with open("cat_image.jpg", "wb") as f:
        f.write(image_bytes)
    print(f"Generated base64 image saved to cat_image.jpg")
        
elif image_data.url:
    # Image URL
    print(f"Generated image URL: {image_data.url}")

<aside> 👌

For further reference, check out this page: https://docs.litellm.ai/docs/image_generation

</aside>