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.5-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
1
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
2
Install dependencies
uv pip install -U agno google-genai
3
Export your Google API key
export GOOGLE_API_KEY="your_google_api_key_here"
$Env:GOOGLE_API_KEY="your_google_api_key_here"
4
Run the example
Save the code above as
vertexai_with_credentials.py, then run:python vertexai_with_credentials.py