Gemini Interactions - Image Generation
Example showing image generation with the Interactions API.
Example showing image generation with the Interactions API. Uses response_modalities=["text", "image"] to enable image output.
"""
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
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno google-genaiExport your Google API key
export GOOGLE_API_KEY="your_google_api_key_here"Use the stable image model
Before running the source-fidelity code, change gemini-3.1-flash-image-preview to gemini-3.1-flash-image. The preview model has been shut down.
Run the example
Save the code above as image_generation.py, then run:
python image_generation.pyFull source: cookbook/90_models/google/gemini_interactions/image_generation.py