> ## 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.

# Imagen Tool Advanced

> Generate an image with the Imagen 4 model through GeminiTools on Vertex AI and save the result as a PNG.

An Agent using the Gemini image generation tool.

<Warning>
  Google discontinued the source's `imagen-4.0-generate-preview-05-20` endpoint. The source also uses the removed `Agent.run_response` attribute and passes raw image bytes to a base64 decoder. Apply all three edits below before running. See the [Vertex AI release notes](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/release-notes#February_17_2026).
</Warning>

```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

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

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.
"""

# ---------------------------------------------------------------------------
# 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 google-genai openai
    ```
  </Step>

  <Step title="Export environment variables">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export GOOGLE_CLOUD_LOCATION="your_google_cloud_location_here"
      export GOOGLE_CLOUD_PROJECT="your_google_cloud_project_here"
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

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

  <Step title="Authenticate with Google Cloud">
    Sign in with Application Default Credentials:

    ```bash theme={null}
    gcloud auth application-default login
    ```
  </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 the example">
    Save the code above as `imagen_tool_advanced.py`, then run:

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

Full source: [cookbook/90\_models/google/gemini/imagen\_tool\_advanced.py](https://github.com/agno-agi/agno/blob/main/cookbook/90_models/google/gemini/imagen_tool_advanced.py)
