Image Generation Tools

Generate images using OpenAI tools.

Similar to providing multimodal inputs, you can also get multimodal outputs from an agent.

Image Generation using a tool

The following example demonstrates how to generate an image using an OpenAI tool with an agent.

image_agent.py
import base64

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
from agno.tools.openai import OpenAITools
from agno.utils.media import save_base64_data

agent = Agent(
    model=OpenAIResponses(id="gpt-5.2"),
    db=SqliteDb(db_file="tmp/test.db"),
    tools=[OpenAITools(image_model="gpt-image-2")],
    add_history_to_context=True,
    markdown=True,
)

response = agent.run(
    "Generate a photorealistic image of a cozy coffee shop interior",
)

if response.images and response.images[0].content:
    image_base64 = base64.b64encode(response.images[0].content).decode("utf-8")
    save_base64_data(image_base64, "tmp/coffee_shop.png")

With send_media_to_model=True (the default), the generated image is available to this image-capable model in the same run. It can describe the image after generating it. The database and add_history_to_context=True also make it available for follow-up turns in this session.

Audio and video require their own model/adapter input support; the OpenAIResponses adapter shown here does not forward those modalities as model inputs. Turning off send_media_to_model preserves output artifacts but stops forwarding their media content to the model.

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai sqlalchemy

Export your OpenAI API key

  export OPENAI_API_KEY="your_openai_api_key_here"

Run Agent

python image_agent.py