Skip to main content
Example showing image generation with the Interactions API. Uses response_modalities=[“text”, “image”] to enable image output.
image_generation.py
"""
Gemini Interactions - Image Generation
=======================================

Example showing image generation with the Interactions API.
Uses response_modalities=["text", "image"] to enable image output.

Note: Image generation requires a model that supports image output,
such as gemini-3.1-flash-image-preview.
"""

from agno.agent import Agent
from agno.models.google import GeminiInteractions

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

agent = Agent(
    model=GeminiInteractions(
        id="gemini-3.1-flash-image-preview",
        response_modalities=["text", "image"],
    ),
    markdown=True,
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    response = agent.run("Generate an image of a sunset over mountains")

    if response.images:
        for i, img in enumerate(response.images):
            filepath = f"generated_image_{i}.png"
            content = img.get_content_bytes()
            if content:
                with open(filepath, "wb") as f:
                    f.write(content)
                print(f"Saved image to {filepath}")
    else:
        print("No images generated")

    if response.content:
        print(f"Response: {response.content}")

Run the Example

1

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
2

Install dependencies

uv pip install -U agno google-genai
3

Export your Google API key

export GOOGLE_API_KEY="your_google_api_key_here"
$Env:GOOGLE_API_KEY="your_google_api_key_here"
4

Run the example

Save the code above as image_generation.py, then run:
python image_generation.py
Full source: cookbook/90_models/google/gemini_interactions/image_generation.py