Gemini
PydanticAI supports Google's Gemini models through two different APIs:
- Generative Language API (
generativelanguage.googleapis.com
) - Vertex AI API (
*-aiplatform.googleapis.com
)
Gemini via Generative Language API
Install
To use GeminiModel
models, you just need to install pydantic-ai
or pydantic-ai-slim
, no extra dependencies are required.
Configuration
GeminiModel
lets you use Google's Gemini models through their Generative Language API, generativelanguage.googleapis.com
.
GeminiModelName
contains a list of available Gemini models that can be used through this interface.
To use GeminiModel
, go to aistudio.google.com and select "Create API key".
Environment variable
Once you have the API key, you can set it as an environment variable:
export GEMINI_API_KEY=your-api-key
You can then use GeminiModel
by name:
from pydantic_ai import Agent
agent = Agent('google-gla:gemini-2.0-flash')
...
Note
The google-gla
provider prefix represents the Google Generative Language API for GeminiModel
s.
google-vertex
is used with Vertex AI.
Or initialise the model directly with just the model name and provider:
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
model = GeminiModel('gemini-2.0-flash', provider='google-gla')
agent = Agent(model)
...
provider
argument
You can provide a custom Provider
via the provider
argument:
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_gla import GoogleGLAProvider
model = GeminiModel(
'gemini-2.0-flash', provider=GoogleGLAProvider(api_key='your-api-key')
)
agent = Agent(model)
...
You can also customize the GoogleGLAProvider
with a custom http_client
:
from httpx import AsyncClient
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_gla import GoogleGLAProvider
custom_http_client = AsyncClient(timeout=30)
model = GeminiModel(
'gemini-2.0-flash',
provider=GoogleGLAProvider(api_key='your-api-key', http_client=custom_http_client),
)
agent = Agent(model)
...
Gemini via VertexAI
If you are an enterprise user, you should use the google-vertex
provider with GeminiModel
which uses the *-aiplatform.googleapis.com
API.
GeminiModelName
contains a list of available Gemini models that can be used through this interface.
Install
To use the google-vertex
provider with GeminiModel
, you need to either install pydantic-ai
, or install pydantic-ai-slim
with the vertexai
optional group:
pip install "pydantic-ai-slim[vertexai]"
uv add "pydantic-ai-slim[vertexai]"
Configuration
This interface has a number of advantages over generativelanguage.googleapis.com
documented above:
- The VertexAI API comes with more enterprise readiness guarantees.
- You can purchase provisioned throughput with VertexAI to guarantee capacity.
- If you're running PydanticAI inside GCP, you don't need to set up authentication, it should "just work".
- You can decide which region to use, which might be important from a regulatory perspective, and might improve latency.
The big disadvantage is that for local development you may need to create and configure a "service account", which can be challenging to get right.
Whichever way you authenticate, you'll need to have VertexAI enabled in your GCP account.
Application default credentials
Luckily if you're running PydanticAI inside GCP, or you have the gcloud
CLI installed and configured, you should be able to use VertexAIModel
without any additional setup.
To use VertexAIModel
, with application default credentials configured (e.g. with gcloud
), you can simply use:
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
model = GeminiModel('gemini-2.0-flash', provider='google-vertex')
agent = Agent(model)
...
Internally this uses google.auth.default()
from the google-auth
package to obtain credentials.
Won't fail until agent.run()
Because google.auth.default()
requires network requests and can be slow, it's not run until you call agent.run()
.
You may also need to pass the project_id
argument to GoogleVertexProvider
if application default credentials don't set a project, if you pass project_id
and it conflicts with the project set by application default credentials, an error is raised.
Service account
If instead of application default credentials, you want to authenticate with a service account, you'll need to create a service account, add it to your GCP project (note: this step is necessary even if you created the service account within the project), give that service account the "Vertex AI Service Agent" role, and download the service account JSON file.
Once you have the JSON file, you can use it thus:
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_vertex import GoogleVertexProvider
model = GeminiModel(
'gemini-2.0-flash',
provider=GoogleVertexProvider(service_account_file='path/to/service-account.json'),
)
agent = Agent(model)
...
Alternatively, if you already have the service account information in memory, you can pass it as a dictionary:
import json
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_vertex import GoogleVertexProvider
service_account_info = json.loads(
'{"type": "service_account", "project_id": "my-project-id"}'
)
model = GeminiModel(
'gemini-2.0-flash',
provider=GoogleVertexProvider(service_account_info=service_account_info),
)
agent = Agent(model)
...
Customising region
Whichever way you authenticate, you can specify which region requests will be sent to via the region
argument.
Using a region close to your application can improve latency and might be important from a regulatory perspective.
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_vertex import GoogleVertexProvider
model = GeminiModel(
'gemini-2.0-flash', provider=GoogleVertexProvider(region='asia-east1')
)
agent = Agent(model)
...
You can also customize the GoogleVertexProvider
with a custom http_client
:
from httpx import AsyncClient
from pydantic_ai import Agent
from pydantic_ai.models.gemini import GeminiModel
from pydantic_ai.providers.google_vertex import GoogleVertexProvider
custom_http_client = AsyncClient(timeout=30)
model = GeminiModel(
'gemini-2.0-flash',
provider=GoogleVertexProvider(region='asia-east1', http_client=custom_http_client),
)
agent = Agent(model)
...