You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/models/google.md
+76-47Lines changed: 76 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,6 @@ To use `GoogleModel`, you need to either install `pydantic-ai`, or install `pyda
11
11
pip/uv-add "pydantic-ai-slim[google]"
12
12
```
13
13
14
-
---
15
14
16
15
## Configuration
17
16
@@ -27,54 +26,70 @@ Once you have the API key, set it as an environment variable:
27
26
export GOOGLE_API_KEY=your-api-key
28
27
```
29
28
30
-
You can then use `GoogleModel` by explicitly creating a provider:
29
+
You can then use `GoogleModel` by name (where GLA stands for Generative Language API):
30
+
31
+
```python
32
+
from pydantic_ai import Agent
33
+
34
+
agent = Agent('google-gla:gemini-2.5-pro')
35
+
...
36
+
```
37
+
38
+
Or you can explicitly create the provider:
31
39
32
40
```python
33
41
from pydantic_ai import Agent
34
42
from pydantic_ai.models.google import GoogleModel
35
43
from pydantic_ai.providers.google import GoogleProvider
36
44
37
45
provider = GoogleProvider(api_key='your-api-key')
38
-
model = GoogleModel('gemini-1.5-flash', provider=provider)
46
+
model = GoogleModel('gemini-2.5-pro', provider=provider)
39
47
agent = Agent(model)
40
48
...
41
49
```
42
50
43
51
### Vertex AI (Enterprise/Cloud)
44
52
45
-
If you are an enterprise user, you can use the `google-vertex` provider with`GoogleModel` to access Gemini via Vertex AI.
53
+
If you are an enterprise user, you can also use`GoogleModel` to access Gemini via Vertex AI.
46
54
47
55
This interface has a number of advantages over the Generative Language API:
48
56
49
57
1. The VertexAI API comes with more enterprise readiness guarantees.
50
-
2. You can [purchase provisioned throughput](https://cloud.google.com/vertex-ai/generative-ai/docs/provisioned-throughput#purchase-provisioned-throughput) with VertexAI to guarantee capacity.
58
+
2. You can [purchase provisioned throughput](https://cloud.google.com/vertex-ai/generative-ai/docs/provisioned-throughput#purchase-provisioned-throughput) with Vertex AI to guarantee capacity.
51
59
3. If you're running Pydantic AI inside GCP, you don't need to set up authentication, it should "just work".
52
60
4. You can decide which region to use, which might be important from a regulatory perspective, and might improve latency.
53
61
54
-
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.
62
+
You can authenticate using [application default credentials](https://cloud.google.com/docs/authentication/application-default-credentials), a service account, or an [API key](https://cloud.google.com/vertex-ai/generative-ai/docs/start/api-keys?usertype=expressmode).
55
63
56
-
Whichever way you authenticate, you'll need to have VertexAI enabled in your GCP account.
57
-
58
-
To use Vertex AI, you may need to set up [application default credentials](https://cloud.google.com/docs/authentication/application-default-credentials) or use a service account. You can also specify the region.
64
+
Whichever way you authenticate, you'll need to have Vertex AI enabled in your GCP account.
59
65
60
66
#### Application Default Credentials
61
67
62
-
If you have the [`gcloud` CLI](https://cloud.google.com/sdk/gcloud) installed and configured, you can use:
68
+
If you have the [`gcloud` CLI](https://cloud.google.com/sdk/gcloud) installed and configured, you can use `GoogleProvider` in Vertex AI mode by name:
69
+
70
+
```python {test="ci_only"}
71
+
from pydantic_ai import Agent
72
+
73
+
agent = Agent('google-vertex:gemini-2.5-pro')
74
+
...
75
+
```
76
+
77
+
Or you can explicitly create the provider and model:
63
78
64
79
```python {test="ci_only"}
65
80
from pydantic_ai import Agent
66
81
from pydantic_ai.models.google import GoogleModel
67
82
from pydantic_ai.providers.google import GoogleProvider
68
83
69
84
provider = GoogleProvider(vertexai=True)
70
-
model = GoogleModel('gemini-1.5-flash', provider=provider)
85
+
model = GoogleModel('gemini-2.5-pro', provider=provider)
71
86
agent = Agent(model)
72
87
...
73
88
```
74
89
75
90
#### Service Account
76
91
77
-
To use a service account JSON file:
92
+
To use a service account JSON file, explicitly create the provider and model:
To use Vertex AI with an API key, [create a key](https://cloud.google.com/vertex-ai/generative-ai/docs/start/api-keys?usertype=expressmode) and set it as an environment variable:
114
+
115
+
```bash
116
+
export GOOGLE_API_KEY=your-api-key
117
+
```
118
+
119
+
You can then use `GoogleModel` in Vertex AI mode by name:
120
+
121
+
```python {test="ci_only"}
122
+
from pydantic_ai import Agent
123
+
124
+
agent = Agent('google-vertex:gemini-2.5-pro')
125
+
...
126
+
```
97
127
98
-
You can specify the location when using Vertex AI:
128
+
Or you can explicitly create the provider and model:
129
+
130
+
```python {test="skip"}
131
+
from pydantic_ai import Agent
132
+
from pydantic_ai.models.google import GoogleModel
133
+
from pydantic_ai.providers.google import GoogleProvider
model = GoogleModel('gemini-2.5-pro', provider=provider)
107
152
agent = Agent(model)
108
153
...
109
154
```
110
155
111
-
#### Customizing Model
156
+
#### Model Garden
112
157
113
-
You can access models from the [Model Garden](https://cloud.google.com/model-garden?hl=en) that support the generateContent API and are available under your GCP project, including but not limited to Gemini, using one of the following `model_name` patterns:
158
+
You can access models from the [Model Garden](https://cloud.google.com/model-garden?hl=en) that support the `generateContent` API and are available under your GCP project, including but not limited to Gemini, using one of the following `model_name` patterns:
location='us-central1', # the region where the model is available
135
173
)
@@ -138,31 +176,32 @@ agent = Agent(model)
138
176
...
139
177
```
140
178
141
-
## Provider Argument
142
-
143
-
You can supply a custom `GoogleProvider` instance using the `provider` argument to configure advanced client options, such as setting a custom `base_url`.
179
+
## Custom HTTP Client
144
180
145
-
This is useful if you're using a custom-compatible endpoint with the Google Generative Language API.
181
+
You can customize the `GoogleProvider` with a custom`httpx.AsyncClient`:
146
182
147
183
```python
148
-
from google.genai import Client
149
-
from google.genai.types import HttpOptions
184
+
from httpx import AsyncClient
150
185
151
186
from pydantic_ai import Agent
152
187
from pydantic_ai.models.google import GoogleModel
153
188
from pydantic_ai.providers.google import GoogleProvider
model = GoogleModel('gemini-1.5-flash', provider=provider)
161
195
agent = Agent(model)
162
196
...
163
197
```
164
198
165
-
## Model Settings
199
+
200
+
## Document, Image, Audio, and Video Input
201
+
202
+
`GoogleModel` supports multi-modal input, including documents, images, audio, and video. See the [input documentation](../input.md) for details and examples.
203
+
204
+
## Model settings
166
205
167
206
You can customize model behavior using [`GoogleModelSettings`][pydantic_ai.models.google.GoogleModelSettings]:
See the [Gemini API docs](https://ai.google.dev/gemini-api/docs/safety-settings) for more on safety settings, and [thinking config](https://ai.google.dev/gemini-api/docs/thinking).
192
-
193
-
## Document, Image, Audio, and Video Input
194
-
195
-
`GoogleModel` supports multi-modal input, including documents, images, audio, and video. See the [input documentation](../input.md) for details and examples.
196
-
197
-
## Model settings
198
-
199
-
You can use the [`GoogleModelSettings`][pydantic_ai.models.google.GoogleModelSettings] class to customize the model request.
200
-
201
230
### Disable thinking
202
231
203
232
You can disable thinking by setting the `thinking_budget` to `0` on the `google_thinking_config`:
0 commit comments