Imagen Tool
Adapt a historical Imagen toolkit example to a Gemini image-generation function tool.
The Gemini Developer API retired the default Imagen 3 model and its Imagen 4 successors; see deprecations. The source also uses the removed Agent.run_response attribute and decodes raw bytes as base64. Use the current adaptation below instead of running the source snapshot unchanged.
"""Example: Using the GeminiTools Toolkit for Image Generation
Make sure you have set the GOOGLE_API_KEY environment variable.
Example prompts to try:
- "Create a surreal painting of a floating city in the clouds at sunset"
- "Generate a photorealistic image of a cozy coffee shop interior"
- "Design a cute cartoon mascot for a tech startup, vector style"
- "Create an artistic portrait of a cyberpunk samurai in a rainy city"
Run `uv pip install google-genai agno` to install the necessary dependencies.
"""
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.models.gemini import GeminiTools
from agno.utils.media import save_base64_data
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=OpenAIChat(id="gpt-5.6-luna"),
tools=[GeminiTools()],
)
agent.print_response(
"Create an artistic portrait of a cyberpunk samurai in a rainy city",
)
response = agent.run_response
if response and response.images:
save_base64_data(str(response.images[0].content), "tmp/cyberpunk_samurai.png")
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passCurrent adaptation
Save this current adaptation as native_image_tool.py. An OpenAI agent calls an Agno function tool that runs a Gemini image agent. ToolResult.images carries the returned image bytes to the outer run.
from pathlib import Path
from agno.agent import Agent
from agno.models.google import Gemini
from agno.models.openai import OpenAIChat
from agno.tools.function import ToolResult
image_agent = Agent(
model=Gemini(
id="gemini-3.1-flash-image",
response_modalities=["Text", "Image"],
)
)
def generate_image(prompt: str) -> ToolResult:
"""Generate an image from a detailed visual description."""
result = image_agent.run(prompt)
if not result.images:
return ToolResult(content="No image was generated.")
return ToolResult(content="Image generated.", images=result.images)
agent = Agent(
model=OpenAIChat(id="gpt-5.4-mini"),
tools=[generate_image],
)
response = agent.run("Generate an image of whales breaching beside a forested fjord.")
print(response.content)
for index, image in enumerate(response.images or []):
if image.content:
suffix = ".jpg" if image.mime_type == "image/jpeg" else ".png"
output = Path("tmp") / f"generated_{index}{suffix}"
output.parent.mkdir(parents=True, exist_ok=True)
output.write_bytes(image.content)
print(output)This uses Gemini's native image-generation API. Changing the model ID on GeminiTools alone is insufficient: that toolkit calls the separate Imagen generate_images endpoint. See native image generation.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno google-genai openaiExport your API keys
export GOOGLE_API_KEY="your_google_api_key_here"
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the current adaptation as native_image_tool.py and any helper beside it, then run:
python native_image_tool.pyFull source: cookbook/90_models/google/gemini/imagen_tool.py