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

# Advanced Imagen Tool with Vertex AI

> Generate images with GeminiTools on Vertex AI using an Imagen model.

## Code

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

An Agent using the Gemini image generation tool.

Make sure to set the Vertex AI credentials. Here's the authentication guide: https://cloud.google.com/sdk/docs/initializing

Run `uv pip install google-genai agno` to install the required packages.
"""

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(
            image_generation_model="imagen-4.0-generate-preview-05-20", vertexai=True
        )
    ],
)

agent.print_response(
    "Cinematic a visual shot using a stabilized drone flying dynamically alongside a pod of immense baleen whales as they breach spectacularly in deep offshore waters. The camera maintains a close, dramatic perspective as these colossal creatures launch themselves skyward from the dark blue ocean, creating enormous splashes and showering cascades of water droplets that catch the sunlight. In the background, misty, fjord-like coastlines with dense coniferous forests provide context. The focus expertly tracks the whales, capturing their surprising agility, immense power, and inherent grace. The color palette features the deep blues and greens of the ocean, the brilliant white spray, the dark grey skin of the whales, and the muted tones of the distant wild coastline, conveying the thrilling magnificence of marine megafauna."
)

response = agent.run_response
if response and response.images:
    save_base64_data(str(response.images[0].content), "tmp/baleen_whale.png")

"""
Example prompts to try:
- A horizontally oriented rectangular stamp features the Mission District's vibrant culture, portrayed in shades of warm terracotta orange using an etching style. The scene might depict a sun-drenched street like Valencia or Mission Street, lined with a mix of Victorian buildings and newer structures.
- Painterly landscape featuring a simple, isolated wooden cabin nestled amongst tall pine trees on the shore of a calm, reflective lake.
- Filmed cinematically from the driver's seat, offering a clear profile view of the young passenger on the front seat with striking red hair.
- A pile of books seen from above. The topmost book contains a watercolor illustration of a bird. VERTEX AI is written in bold letters on the book.
"""
```

<Warning>
  The source uses the retired `imagen-4.0-generate-preview-05-20` model, the removed `Agent.run_response` accessor, and raw image bytes where `save_base64_data` expects base64. Apply all three edits below before running. Google will shut down Imagen 4 on August 17, 2026. Migrate to the [Gemini image-generation API](https://ai.google.dev/gemini-api/docs/generate-content/image-generation), which uses `generate_content`, before that date.
</Warning>

## Usage

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

  <Step title="Set up Vertex AI authentication">
    Follow the [authentication guide](https://cloud.google.com/sdk/docs/initializing) to set up Vertex AI credentials.

    ```bash theme={null}
    gcloud auth application-default login
    ```
  </Step>

  <Step title="Set environment variables">
    ```bash theme={null}
    export OPENAI_API_KEY=xxx
    export GOOGLE_CLOUD_PROJECT=your-project-id
    export GOOGLE_CLOUD_LOCATION=us-central1
    ```
  </Step>

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

  <Step title="Update the Imagen model">
    Replace `imagen-4.0-generate-preview-05-20` with `imagen-4.0-generate-001` in the saved file.
  </Step>

  <Step title="Use the v2 run output accessor">
    Replace `response = agent.run_response` with `response = agent.get_last_run_output()` in the saved file.
  </Step>

  <Step title="Encode the image bytes">
    Add `import base64`, then replace `save_base64_data(str(response.images[0].content), "tmp/baleen_whale.png")` with `save_base64_data(base64.b64encode(response.images[0].content).decode("ascii"), "tmp/baleen_whale.png")` in the saved file.
  </Step>

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

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