> ## Documentation Index
> Fetch the complete documentation index at: https://phidatainc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Image Generation Agent

> Generate and save an image with OpenAITools and GPT Image 2.

The source-fidelity code uses `OpenAIChat` and `gpt-image-1` despite its Responses category. Replace them with `OpenAIResponses` and `gpt-image-2` before running the example.

```python image_generation_agent.py theme={null}
"""Example: Using the OpenAITools Toolkit for Image Generation

This script demonstrates how to use the `OpenAITools` toolkit, which includes a tool for generating images using OpenAI's DALL-E within an Agno Agent.

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"
- "Create an artistic portrait of a cyberpunk samurai"

Run `uv pip install openai agno` to install the necessary dependencies.
"""

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.openai import OpenAITools
from agno.utils.media import save_base64_data

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

agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    tools=[OpenAITools(image_model="gpt-image-1")],
    markdown=True,
)

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

if response.images and response.images[0].content:
    save_base64_data(str(response.images[0].content), "tmp/coffee_shop.png")

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Use the Responses API">
    Replace the `OpenAIChat` import and constructor with `OpenAIResponses(id="gpt-5.2")`.
  </Step>

  <Step title="Encode the image bytes">
    Add `import base64`. Encode `response.images[0].content` with `base64.b64encode(...).decode("utf-8")`, then pass that string to `save_base64_data`.
  </Step>

  <Step title="Update the image model">
    When saving the code, replace `gpt-image-1` with `gpt-image-2`.
  </Step>

  <Step title="Run the example">
    Save the code above as `image_generation_agent.py`, then run:

    ```bash theme={null}
    python image_generation_agent.py
    ```
  </Step>
</Steps>

Full source: [cookbook/90\_models/openai/responses/image\_generation\_agent.py](https://github.com/agno-agi/agno/blob/main/cookbook/90_models/openai/responses/image_generation_agent.py)
