Google Vertex AI With Credentials

Authenticate Gemini on Vertex AI with explicit service account credentials.

vertexai_with_credentials.py
"""
Google Vertexai With Credentials
================================

Cookbook example for `google/gemini/vertexai_with_credentials.py`.
"""

from agno.agent import Agent
from agno.models.google import Gemini

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

# To use Vertex AI with explicit credentials, you can pass a
# google.oauth2.service_account.Credentials object to the Gemini class.

# 1. Load your service account credentials (example using a JSON file)
# from google.oauth2 import service_account
# credentials = service_account.Credentials.from_service_account_file('path/to/your/service-account.json')

# For demonstration, we'll assume credentials is provided
credentials = None  # Replace with your actual credentials object

# 2. Initialize the Gemini model with the credentials parameter
model = Gemini(
    id="gemini-3.7-flash",
    vertexai=True,
    project_id="your-google-cloud-project-id",
    location="us-central1",
    credentials=credentials,
)

# 3. Create the Agent
agent = Agent(model=model, markdown=True)

# 4. Use the Agent
agent.print_response(
    "Explain how explicit credentials help in production environments."
)

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

if __name__ == "__main__":
    pass

Run the Example

Set up your virtual environment

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

Install dependencies

uv pip install -U agno google-genai

Provide service account credentials

Load a google.oauth2.service_account.Credentials object and replace the placeholder project ID before running the example.

Use a supported model location

Replace location="us-central1" with location="global" for the displayed Gemini 3.7 Flash model. See the supported endpoints. Uncomment the service-account loading lines, set the actual file path, and remove credentials = None so it does not overwrite the loaded credentials.

Run the example

Save the code above as vertexai_with_credentials.py, then run:

python vertexai_with_credentials.py

Full source: cookbook/90_models/google/gemini/vertexai_with_credentials.py