Nous.Providers.VertexAI (nous v0.13.3)
View SourceGoogle Vertex AI provider implementation.
Supports Gemini models via the Vertex AI API, which provides enterprise features like VPC-SC, CMEK, IAM, and regional/global endpoints.
Supported Models
| Model | Model ID | Endpoint | API Version |
|---|---|---|---|
| Gemini 3.1 Pro (preview) | gemini-3.1-pro-preview | global only | v1beta1 |
| Gemini 3 Flash (preview) | gemini-3-flash-preview | global only | v1beta1 |
| Gemini 3.1 Flash-Lite | gemini-3.1-flash-lite-preview | global only | v1beta1 |
| Gemini 2.5 Pro | gemini-2.5-pro | regional/global | v1 |
| Gemini 2.5 Flash | gemini-2.5-flash | regional/global | v1 |
| Gemini 2.0 Flash | gemini-2.0-flash | regional/global | v1 |
Preview and experimental models automatically use the v1beta1 API version.
Stable models use v1. This is determined by api_version_for_model/1.
Authentication
Vertex AI uses OAuth2 Bearer tokens (not API keys like Google AI). Token resolution order:
:api_keyoption passed directly (treated as a Bearer access token)- Goth integration — if a Goth process name is configured, fetches tokens automatically
VERTEX_AI_ACCESS_TOKENenvironment variable- Application config:
config :nous, :vertex_ai, api_key: "..."
Using Goth with a Service Account (Recommended)
Goth handles OAuth2 token fetching, caching, and auto-refresh from a GCP service account. Load the service account JSON from an environment variable (no file path dependency):
# Set env vars:
# export GOOGLE_CREDENTIALS='{"type":"service_account","project_id":"...",...}'
# export GOOGLE_CLOUD_PROJECT="your-project-id"
# export GOOGLE_CLOUD_LOCATION="global" # required for Gemini 3.x preview
# mix.exs
{:goth, "~> 1.4"}
# application.ex — start Goth in your supervision tree
credentials = System.get_env("GOOGLE_CREDENTIALS") |> JSON.decode!()
children = [
{Goth, name: MyApp.Goth, source: {:service_account, credentials}}
]Then configure Nous to use it:
# config.exs (recommended for production)
config :nous, :vertex_ai, goth: MyApp.Goth
# Then just use it:
agent = Nous.new("vertex_ai:gemini-3.1-pro-preview")
{:ok, result} = Nous.run(agent, "Hello!")Or pass Goth per-model (useful for multiple projects):
agent = Nous.new("vertex_ai:gemini-3-flash-preview",
default_settings: %{goth: MyApp.Goth}
)Using an Access Token
For quick testing without Goth (tokens expire after ~1 hour):
# export VERTEX_AI_ACCESS_TOKEN="$(gcloud auth print-access-token)"
agent = Nous.new("vertex_ai:gemini-3.1-pro-preview")Or pass it explicitly:
agent = Nous.new("vertex_ai:gemini-3.1-pro-preview",
api_key: System.get_env("VERTEX_AI_ACCESS_TOKEN")
)URL Construction
The base URL is built at request time from environment variables and the model name. The provider selects the correct hostname and API version automatically.
Regional Endpoints (for stable models)
https://{region}-aiplatform.googleapis.com/v1/projects/{project}/locations/{region}Global Endpoint (required for Gemini 3.x preview models)
https://aiplatform.googleapis.com/v1beta1/projects/{project}/locations/globalEnvironment Variables
GOOGLE_CLOUD_PROJECT(orGCLOUD_PROJECT) — GCP project ID (required)GOOGLE_CLOUD_REGION(orGOOGLE_CLOUD_LOCATION) — GCP region orglobal(defaults tous-central1)
Both GOOGLE_CLOUD_REGION and GOOGLE_CLOUD_LOCATION are supported, consistent with
other Google Cloud libraries and tooling. GOOGLE_CLOUD_REGION takes precedence if both
are set.
Explicit Base URL
You can override the auto-constructed URL entirely:
alias Nous.Providers.VertexAI
# Use the endpoint helper to build the URL with correct API version:
url = VertexAI.endpoint("my-project", "global", "gemini-3.1-pro-preview")
# => "https://aiplatform.googleapis.com/v1beta1/projects/my-project/locations/global"
agent = Nous.new("vertex_ai:gemini-3.1-pro-preview", base_url: url)Input Validation
The provider validates GOOGLE_CLOUD_PROJECT and the region at request time and returns
helpful error messages for invalid values (e.g., typos, wrong format) instead of opaque
DNS or HTTP errors.
Configuration
# config.exs
config :nous, :vertex_ai,
goth: MyApp.Goth
# Or with an explicit base_url (overrides env var URL construction):
config :nous, :vertex_ai,
goth: MyApp.Goth,
base_url: "https://aiplatform.googleapis.com/v1beta1/projects/my-project/locations/global"Examples
# Gemini 3.1 Pro on global endpoint (preview, v1beta1)
agent = Nous.new("vertex_ai:gemini-3.1-pro-preview")
# Gemini 3 Flash on global endpoint (preview, v1beta1)
agent = Nous.new("vertex_ai:gemini-3-flash-preview")
# Gemini 2.0 Flash on regional endpoint (stable, v1)
agent = Nous.new("vertex_ai:gemini-2.0-flash")
# With explicit region override
agent = Nous.new("vertex_ai:gemini-3.1-pro-preview",
base_url: VertexAI.endpoint("my-project", "global", "gemini-3.1-pro-preview")
)
Summary
Functions
Get the API key from options, environment, or application config.
Returns the appropriate API version for a model name.
Get the base URL from options, application config, or default.
Count tokens in messages (rough estimate).
Build a Vertex AI endpoint URL from project ID, region, and optional model name.
High-level request with message conversion, telemetry, and error wrapping.
High-level streaming request with message conversion and telemetry.
Functions
Get the API key from options, environment, or application config.
Lookup order:
:api_keyoption passed directly- Environment variable (VERTEX_AI_ACCESS_TOKEN)
- Application config:
config :nous, vertex_ai, api_key: "..."
Returns the appropriate API version for a model name.
Preview and experimental models use v1beta1, stable models use v1.
Get the base URL from options, application config, or default.
Lookup order:
:base_urloption passed directly- Application config:
config :nous, vertex_ai, base_url: "..." - Default:
Count tokens in messages (rough estimate).
Override this in your provider for more accurate counting.
Build a Vertex AI endpoint URL from project ID, region, and optional model name.
Uses v1beta1 API version for preview/experimental models and v1 for stable models.
If no model name is provided, defaults to v1.
When region is "global", uses aiplatform.googleapis.com (no region prefix).
Regional endpoints use {region}-aiplatform.googleapis.com.
Gemini 3.x preview models (gemini-3.1-pro-preview, gemini-3-flash-preview, etc.)
are only available on the global endpoint.
Examples
# Regional endpoint, stable model (v1)
iex> Nous.Providers.VertexAI.endpoint("my-project", "us-central1")
"https://us-central1-aiplatform.googleapis.com/v1/projects/my-project/locations/us-central1"
iex> Nous.Providers.VertexAI.endpoint("my-project", "us-central1", "gemini-2.0-flash")
"https://us-central1-aiplatform.googleapis.com/v1/projects/my-project/locations/us-central1"
# Global endpoint, preview model (v1beta1) — required for Gemini 3.x
iex> Nous.Providers.VertexAI.endpoint("my-project", "global", "gemini-3.1-pro-preview")
"https://aiplatform.googleapis.com/v1beta1/projects/my-project/locations/global"
iex> Nous.Providers.VertexAI.endpoint("my-project", "global", "gemini-3-flash-preview")
"https://aiplatform.googleapis.com/v1beta1/projects/my-project/locations/global"
High-level request with message conversion, telemetry, and error wrapping.
Default implementation that:
- Converts messages to provider format
- Builds request params
- Calls chat/2
- Parses response
- Emits telemetry events
- Wraps errors
High-level streaming request with message conversion and telemetry.