Image Tool with OpenAI
Use an OpenAI agent to call a Gemini image-generation agent through a custom tool.
Use a custom tool to call Gemini’s current image-generation path. The OpenAI agent chooses the prompt; the Gemini agent returns image bytes through ToolResult.
Current example
Install the dependencies in your Python environment:
uv pip install -U agno google-genai openai pillowSet both provider keys:
export GOOGLE_API_KEY="your-google-api-key"
export OPENAI_API_KEY="your-openai-api-key"Save this as imagen_tool.py:
from io import BytesIO
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
from PIL import Image as PILImage
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:
raise RuntimeError("Gemini returned no image")
return ToolResult(content=str(result.content or "Image generated"), images=result.images)
agent = Agent(
model=OpenAIChat(id="gpt-5.2"),
tools=[generate_image],
instructions="Use generate_image for image requests.",
)
response = agent.run("Create a painting of a floating city in the clouds at sunset.")
if not response.images:
raise RuntimeError("No generated image was returned")
output_dir = Path("tmp/generated-images")
output_dir.mkdir(parents=True, exist_ok=True)
for index, image in enumerate(response.images):
if not image.content:
raise RuntimeError("Generated image has no bytes")
output_path = output_dir / f"image_{index}.png"
with PILImage.open(BytesIO(image.content)) as decoded:
decoded.save(output_path)
print(output_path)Run python imagen_tool.py. This saves the generated images as PNG files. The nested Gemini call uses generate_content; substituting a Gemini model ID into GeminiTools.generate_image() would still call the Imagen generate_images endpoint.
Historical Imagen example
The original example below is retained for comparison and cannot run as written. It uses a retired Imagen model, the removed Agent.run_response accessor, and passes raw bytes to a base64 decoder. Use the complete current example above. Vertex Imagen 4 was discontinued on June 30, 2026; Gemini API Imagen models have a separate retirement schedule.
"""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"
"""
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
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
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")