google-genai 1.14.0__tar.gz → 1.51.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {google_genai-1.14.0/google_genai.egg-info → google_genai-1.51.0}/PKG-INFO +685 -207
- {google_genai-1.14.0 → google_genai-1.51.0}/README.md +674 -201
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/__init__.py +4 -2
- google_genai-1.51.0/google/genai/_adapters.py +55 -0
- google_genai-1.51.0/google/genai/_api_client.py +1869 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/_api_module.py +1 -1
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/_automatic_function_calling_util.py +49 -8
- google_genai-1.51.0/google/genai/_base_transformers.py +26 -0
- google_genai-1.51.0/google/genai/_common.py +815 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/_extra_utils.py +309 -36
- google_genai-1.51.0/google/genai/_live_converters.py +1467 -0
- google_genai-1.51.0/google/genai/_local_tokenizer_loader.py +214 -0
- google_genai-1.51.0/google/genai/_mcp_utils.py +117 -0
- google_genai-1.51.0/google/genai/_operations_converters.py +394 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/_replay_api_client.py +137 -63
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/_test_api_client.py +1 -1
- google_genai-1.51.0/google/genai/_tokens_converters.py +520 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/_transformers.py +436 -179
- google_genai-1.51.0/google/genai/batches.py +2584 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/caches.py +456 -868
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/chats.py +17 -18
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/client.py +114 -2
- google_genai-1.51.0/google/genai/documents.py +552 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/errors.py +97 -14
- google_genai-1.51.0/google/genai/file_search_stores.py +1312 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/files.py +143 -377
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/live.py +348 -170
- google_genai-1.51.0/google/genai/live_music.py +197 -0
- google_genai-1.51.0/google/genai/local_tokenizer.py +395 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/models.py +3318 -2649
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/operations.py +162 -311
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/pagers.py +21 -5
- google_genai-1.51.0/google/genai/tokens.py +362 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/tunings.py +1072 -353
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/types.py +14060 -7080
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/version.py +2 -2
- {google_genai-1.14.0 → google_genai-1.51.0/google_genai.egg-info}/PKG-INFO +685 -207
- {google_genai-1.14.0 → google_genai-1.51.0}/google_genai.egg-info/SOURCES.txt +12 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google_genai.egg-info/requires.txt +9 -1
- {google_genai-1.14.0 → google_genai-1.51.0}/pyproject.toml +26 -14
- google_genai-1.51.0/setup.cfg +10 -0
- google_genai-1.14.0/google/genai/_api_client.py +0 -1123
- google_genai-1.14.0/google/genai/_common.py +0 -318
- google_genai-1.14.0/google/genai/_live_converters.py +0 -2515
- google_genai-1.14.0/google/genai/batches.py +0 -1151
- google_genai-1.14.0/setup.cfg +0 -4
- {google_genai-1.14.0 → google_genai-1.51.0}/LICENSE +0 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/MANIFEST.in +0 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/_base_url.py +0 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google/genai/py.typed +0 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google_genai.egg-info/dependency_links.txt +0 -0
- {google_genai-1.14.0 → google_genai-1.51.0}/google_genai.egg-info/top_level.txt +0 -0
|
@@ -1,32 +1,37 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: google-genai
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.51.0
|
|
4
4
|
Summary: GenAI Python SDK
|
|
5
5
|
Author-email: Google LLC <googleapis-packages@google.com>
|
|
6
|
-
License: Apache-2.0
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
7
|
Project-URL: Homepage, https://github.com/googleapis/python-genai
|
|
8
8
|
Classifier: Intended Audience :: Developers
|
|
9
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
9
|
Classifier: Operating System :: OS Independent
|
|
11
10
|
Classifier: Programming Language :: Python
|
|
12
11
|
Classifier: Programming Language :: Python :: 3
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
14
12
|
Classifier: Programming Language :: Python :: 3.10
|
|
15
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
16
14
|
Classifier: Programming Language :: Python :: 3.12
|
|
17
15
|
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
17
|
Classifier: Topic :: Internet
|
|
19
18
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
-
Requires-Python: >=3.
|
|
19
|
+
Requires-Python: >=3.10
|
|
21
20
|
Description-Content-Type: text/markdown
|
|
22
21
|
License-File: LICENSE
|
|
23
22
|
Requires-Dist: anyio<5.0.0,>=4.8.0
|
|
24
23
|
Requires-Dist: google-auth<3.0.0,>=2.14.1
|
|
25
24
|
Requires-Dist: httpx<1.0.0,>=0.28.1
|
|
26
|
-
Requires-Dist: pydantic<3.0.0,>=2.
|
|
25
|
+
Requires-Dist: pydantic<3.0.0,>=2.9.0
|
|
27
26
|
Requires-Dist: requests<3.0.0,>=2.28.1
|
|
27
|
+
Requires-Dist: tenacity<9.2.0,>=8.2.3
|
|
28
28
|
Requires-Dist: websockets<15.1.0,>=13.0.0
|
|
29
29
|
Requires-Dist: typing-extensions<5.0.0,>=4.11.0
|
|
30
|
+
Provides-Extra: aiohttp
|
|
31
|
+
Requires-Dist: aiohttp<4.0.0; extra == "aiohttp"
|
|
32
|
+
Provides-Extra: local-tokenizer
|
|
33
|
+
Requires-Dist: sentencepiece>=0.2.0; extra == "local-tokenizer"
|
|
34
|
+
Requires-Dist: protobuf; extra == "local-tokenizer"
|
|
30
35
|
Dynamic: license-file
|
|
31
36
|
|
|
32
37
|
# Google Gen AI SDK
|
|
@@ -40,7 +45,11 @@ Dynamic: license-file
|
|
|
40
45
|
|
|
41
46
|
-----
|
|
42
47
|
|
|
43
|
-
Google Gen AI Python SDK provides an interface for developers to integrate
|
|
48
|
+
Google Gen AI Python SDK provides an interface for developers to integrate
|
|
49
|
+
Google's generative models into their Python applications. It supports the
|
|
50
|
+
[Gemini Developer API](https://ai.google.dev/gemini-api/docs) and
|
|
51
|
+
[Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)
|
|
52
|
+
APIs.
|
|
44
53
|
|
|
45
54
|
## Installation
|
|
46
55
|
|
|
@@ -48,6 +57,12 @@ Google Gen AI Python SDK provides an interface for developers to integrate Googl
|
|
|
48
57
|
pip install google-genai
|
|
49
58
|
```
|
|
50
59
|
|
|
60
|
+
<small>With `uv`:</small>
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
uv pip install google-genai
|
|
64
|
+
```
|
|
65
|
+
|
|
51
66
|
## Imports
|
|
52
67
|
|
|
53
68
|
```python
|
|
@@ -61,11 +76,15 @@ Please run one of the following code blocks to create a client for
|
|
|
61
76
|
different services ([Gemini Developer API](https://ai.google.dev/gemini-api/docs) or [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)).
|
|
62
77
|
|
|
63
78
|
```python
|
|
79
|
+
from google import genai
|
|
80
|
+
|
|
64
81
|
# Only run this block for Gemini Developer API
|
|
65
82
|
client = genai.Client(api_key='GEMINI_API_KEY')
|
|
66
83
|
```
|
|
67
84
|
|
|
68
85
|
```python
|
|
86
|
+
from google import genai
|
|
87
|
+
|
|
69
88
|
# Only run this block for Vertex AI API
|
|
70
89
|
client = genai.Client(
|
|
71
90
|
vertexai=True, project='your-project-id', location='us-central1'
|
|
@@ -78,14 +97,17 @@ You can create a client by configuring the necessary environment variables.
|
|
|
78
97
|
Configuration setup instructions depends on whether you're using the Gemini
|
|
79
98
|
Developer API or the Gemini API in Vertex AI.
|
|
80
99
|
|
|
81
|
-
**Gemini Developer API:** Set `
|
|
100
|
+
**Gemini Developer API:** Set the `GEMINI_API_KEY` or `GOOGLE_API_KEY`.
|
|
101
|
+
It will automatically be picked up by the client. It's recommended that you
|
|
102
|
+
set only one of those variables, but if both are set, `GOOGLE_API_KEY` takes
|
|
103
|
+
precedence.
|
|
82
104
|
|
|
83
105
|
```bash
|
|
84
|
-
export
|
|
106
|
+
export GEMINI_API_KEY='your-api-key'
|
|
85
107
|
```
|
|
86
108
|
|
|
87
|
-
**Gemini API on Vertex AI:** Set `GOOGLE_GENAI_USE_VERTEXAI`,
|
|
88
|
-
and `GOOGLE_CLOUD_LOCATION`, as shown below:
|
|
109
|
+
**Gemini API on Vertex AI:** Set `GOOGLE_GENAI_USE_VERTEXAI`,
|
|
110
|
+
`GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION`, as shown below:
|
|
89
111
|
|
|
90
112
|
```bash
|
|
91
113
|
export GOOGLE_GENAI_USE_VERTEXAI=true
|
|
@@ -94,9 +116,88 @@ export GOOGLE_CLOUD_LOCATION='us-central1'
|
|
|
94
116
|
```
|
|
95
117
|
|
|
96
118
|
```python
|
|
119
|
+
from google import genai
|
|
120
|
+
|
|
97
121
|
client = genai.Client()
|
|
98
122
|
```
|
|
99
123
|
|
|
124
|
+
## Close a client
|
|
125
|
+
|
|
126
|
+
Explicitly close the sync client to ensure that resources, such as the
|
|
127
|
+
underlying HTTP connections, are properly cleaned up and closed.
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from google.genai import Client
|
|
131
|
+
|
|
132
|
+
client = Client()
|
|
133
|
+
response_1 = client.models.generate_content(
|
|
134
|
+
model=MODEL_ID,
|
|
135
|
+
contents='Hello',
|
|
136
|
+
)
|
|
137
|
+
response_2 = client.models.generate_content(
|
|
138
|
+
model=MODEL_ID,
|
|
139
|
+
contents='Ask a question',
|
|
140
|
+
)
|
|
141
|
+
# Close the sync client to release resources.
|
|
142
|
+
client.close()
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
To explicitly close the async client:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from google.genai import Client
|
|
149
|
+
|
|
150
|
+
aclient = Client(
|
|
151
|
+
vertexai=True, project='my-project-id', location='us-central1'
|
|
152
|
+
).aio
|
|
153
|
+
response_1 = await aclient.models.generate_content(
|
|
154
|
+
model=MODEL_ID,
|
|
155
|
+
contents='Hello',
|
|
156
|
+
)
|
|
157
|
+
response_2 = await aclient.models.generate_content(
|
|
158
|
+
model=MODEL_ID,
|
|
159
|
+
contents='Ask a question',
|
|
160
|
+
)
|
|
161
|
+
# Close the async client to release resources.
|
|
162
|
+
await aclient.aclose()
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Client context managers
|
|
166
|
+
|
|
167
|
+
By using the sync client context manager, it will close the underlying
|
|
168
|
+
sync client when exiting the with block.
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
from google.genai import Client
|
|
172
|
+
|
|
173
|
+
with Client() as client:
|
|
174
|
+
response_1 = client.models.generate_content(
|
|
175
|
+
model=MODEL_ID,
|
|
176
|
+
contents='Hello',
|
|
177
|
+
)
|
|
178
|
+
response_2 = client.models.generate_content(
|
|
179
|
+
model=MODEL_ID,
|
|
180
|
+
contents='Ask a question',
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
By using the async client context manager, it will close the underlying
|
|
185
|
+
async client when exiting the with block.
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from google.genai import Client
|
|
189
|
+
|
|
190
|
+
async with Client().aio as aclient:
|
|
191
|
+
response_1 = await aclient.models.generate_content(
|
|
192
|
+
model=MODEL_ID,
|
|
193
|
+
contents='Hello',
|
|
194
|
+
)
|
|
195
|
+
response_2 = await aclient.models.generate_content(
|
|
196
|
+
model=MODEL_ID,
|
|
197
|
+
contents='Ask a question',
|
|
198
|
+
)
|
|
199
|
+
```
|
|
200
|
+
|
|
100
201
|
### API Selection
|
|
101
202
|
|
|
102
203
|
By default, the SDK uses the beta API endpoints provided by Google to support
|
|
@@ -107,6 +208,9 @@ To set the API version use `http_options`. For example, to set the API version
|
|
|
107
208
|
to `v1` for Vertex AI:
|
|
108
209
|
|
|
109
210
|
```python
|
|
211
|
+
from google import genai
|
|
212
|
+
from google.genai import types
|
|
213
|
+
|
|
110
214
|
client = genai.Client(
|
|
111
215
|
vertexai=True,
|
|
112
216
|
project='your-project-id',
|
|
@@ -118,12 +222,72 @@ client = genai.Client(
|
|
|
118
222
|
To set the API version to `v1alpha` for the Gemini Developer API:
|
|
119
223
|
|
|
120
224
|
```python
|
|
225
|
+
from google import genai
|
|
226
|
+
from google.genai import types
|
|
227
|
+
|
|
121
228
|
client = genai.Client(
|
|
122
229
|
api_key='GEMINI_API_KEY',
|
|
123
230
|
http_options=types.HttpOptions(api_version='v1alpha')
|
|
124
231
|
)
|
|
125
232
|
```
|
|
126
233
|
|
|
234
|
+
### Faster async client option: Aiohttp
|
|
235
|
+
|
|
236
|
+
By default we use httpx for both sync and async client implementations. In order
|
|
237
|
+
to have faster performance, you may install `google-genai[aiohttp]`. In Gen AI
|
|
238
|
+
SDK we configure `trust_env=True` to match with the default behavior of httpx.
|
|
239
|
+
Additional args of `aiohttp.ClientSession.request()` ([see _RequestOptions args](https://github.com/aio-libs/aiohttp/blob/v3.12.13/aiohttp/client.py#L170)) can be passed
|
|
240
|
+
through the following way:
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
http_options = types.HttpOptions(
|
|
244
|
+
async_client_args={'cookies': ..., 'ssl': ...},
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
client=Client(..., http_options=http_options)
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### Proxy
|
|
251
|
+
|
|
252
|
+
Both httpx and aiohttp libraries use `urllib.request.getproxies` from
|
|
253
|
+
environment variables. Before client initialization, you may set proxy (and
|
|
254
|
+
optional SSL_CERT_FILE) by setting the environment variables:
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
export HTTPS_PROXY='http://username:password@proxy_uri:port'
|
|
258
|
+
export SSL_CERT_FILE='client.pem'
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
If you need `socks5` proxy, httpx [supports](https://www.python-httpx.org/advanced/proxies/#socks) `socks5` proxy if you pass it via
|
|
262
|
+
args to `httpx.Client()`. You may install `httpx[socks]` to use it.
|
|
263
|
+
Then, you can pass it through the following way:
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
http_options = types.HttpOptions(
|
|
267
|
+
client_args={'proxy': 'socks5://user:pass@host:port'},
|
|
268
|
+
async_client_args={'proxy': 'socks5://user:pass@host:port'},
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
client=Client(..., http_options=http_options)
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### Custom base url
|
|
275
|
+
|
|
276
|
+
In some cases you might need a custom base url (for example, API gateway proxy
|
|
277
|
+
server) and bypass some authentication checks for project, location, or API key.
|
|
278
|
+
You may pass the custom base url like this:
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
base_url = 'https://test-api-gateway-proxy.com'
|
|
282
|
+
client = Client(
|
|
283
|
+
vertexai=True, # Currently only vertexai=True is supported
|
|
284
|
+
http_options={
|
|
285
|
+
'base_url': base_url,
|
|
286
|
+
'headers': {'Authorization': 'Bearer test_token'},
|
|
287
|
+
},
|
|
288
|
+
)
|
|
289
|
+
```
|
|
290
|
+
|
|
127
291
|
## Types
|
|
128
292
|
|
|
129
293
|
Parameter types can be specified as either dictionaries(`TypedDict`) or
|
|
@@ -132,19 +296,42 @@ Pydantic model types are available in the `types` module.
|
|
|
132
296
|
|
|
133
297
|
## Models
|
|
134
298
|
|
|
135
|
-
The `client.models`
|
|
299
|
+
The `client.models` module exposes model inferencing and model getters.
|
|
300
|
+
See the 'Create a client' section above to initialize a client.
|
|
136
301
|
|
|
137
302
|
### Generate Content
|
|
138
303
|
|
|
139
|
-
#### with text content
|
|
304
|
+
#### with text content input (text output)
|
|
140
305
|
|
|
141
306
|
```python
|
|
142
307
|
response = client.models.generate_content(
|
|
143
|
-
model='gemini-2.
|
|
308
|
+
model='gemini-2.5-flash', contents='Why is the sky blue?'
|
|
144
309
|
)
|
|
145
310
|
print(response.text)
|
|
146
311
|
```
|
|
147
312
|
|
|
313
|
+
#### with text content input (image output)
|
|
314
|
+
|
|
315
|
+
```python
|
|
316
|
+
from google.genai import types
|
|
317
|
+
|
|
318
|
+
response = client.models.generate_content(
|
|
319
|
+
model='gemini-2.5-flash-image',
|
|
320
|
+
contents='A cartoon infographic for flying sneakers',
|
|
321
|
+
config=types.GenerateContentConfig(
|
|
322
|
+
response_modalities=["IMAGE"],
|
|
323
|
+
image_config=types.ImageConfig(
|
|
324
|
+
aspect_ratio="9:16",
|
|
325
|
+
),
|
|
326
|
+
),
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
for part in response.parts:
|
|
330
|
+
if part.inline_data:
|
|
331
|
+
generated_image = part.as_image()
|
|
332
|
+
generated_image.show()
|
|
333
|
+
```
|
|
334
|
+
|
|
148
335
|
#### with uploaded file (Gemini Developer API only)
|
|
149
336
|
download the file in console.
|
|
150
337
|
|
|
@@ -157,7 +344,7 @@ python code.
|
|
|
157
344
|
```python
|
|
158
345
|
file = client.files.upload(file='a11.txt')
|
|
159
346
|
response = client.models.generate_content(
|
|
160
|
-
model='gemini-2.
|
|
347
|
+
model='gemini-2.5-flash',
|
|
161
348
|
contents=['Could you summarize this file?', file]
|
|
162
349
|
)
|
|
163
350
|
print(response.text)
|
|
@@ -174,9 +361,11 @@ This is the canonical way to provide contents, SDK will not do any conversion.
|
|
|
174
361
|
##### Provide a `types.Content` instance
|
|
175
362
|
|
|
176
363
|
```python
|
|
364
|
+
from google.genai import types
|
|
365
|
+
|
|
177
366
|
contents = types.Content(
|
|
178
|
-
|
|
179
|
-
|
|
367
|
+
role='user',
|
|
368
|
+
parts=[types.Part.from_text(text='Why is the sky blue?')]
|
|
180
369
|
)
|
|
181
370
|
```
|
|
182
371
|
|
|
@@ -184,10 +373,10 @@ SDK converts this to
|
|
|
184
373
|
|
|
185
374
|
```python
|
|
186
375
|
[
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
376
|
+
types.Content(
|
|
377
|
+
role='user',
|
|
378
|
+
parts=[types.Part.from_text(text='Why is the sky blue?')]
|
|
379
|
+
)
|
|
191
380
|
]
|
|
192
381
|
```
|
|
193
382
|
|
|
@@ -201,11 +390,11 @@ The SDK will assume this is a text part, and it converts this into the following
|
|
|
201
390
|
|
|
202
391
|
```python
|
|
203
392
|
[
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
393
|
+
types.UserContent(
|
|
394
|
+
parts=[
|
|
395
|
+
types.Part.from_text(text='Why is the sky blue?')
|
|
396
|
+
]
|
|
397
|
+
)
|
|
209
398
|
]
|
|
210
399
|
```
|
|
211
400
|
|
|
@@ -223,12 +412,12 @@ like the following:
|
|
|
223
412
|
|
|
224
413
|
```python
|
|
225
414
|
[
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
415
|
+
types.UserContent(
|
|
416
|
+
parts=[
|
|
417
|
+
types.Part.from_text(text='Why is the sky blue?'),
|
|
418
|
+
types.Part.from_text(text='Why is the cloud white?'),
|
|
419
|
+
]
|
|
420
|
+
)
|
|
232
421
|
]
|
|
233
422
|
```
|
|
234
423
|
|
|
@@ -238,9 +427,11 @@ Where a `types.UserContent` is a subclass of `types.Content`, the
|
|
|
238
427
|
##### Provide a function call part
|
|
239
428
|
|
|
240
429
|
```python
|
|
430
|
+
from google.genai import types
|
|
431
|
+
|
|
241
432
|
contents = types.Part.from_function_call(
|
|
242
|
-
|
|
243
|
-
|
|
433
|
+
name='get_weather_by_location',
|
|
434
|
+
args={'location': 'Boston'}
|
|
244
435
|
)
|
|
245
436
|
```
|
|
246
437
|
|
|
@@ -248,14 +439,14 @@ The SDK converts a function call part to a content with a `model` role:
|
|
|
248
439
|
|
|
249
440
|
```python
|
|
250
441
|
[
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
442
|
+
types.ModelContent(
|
|
443
|
+
parts=[
|
|
444
|
+
types.Part.from_function_call(
|
|
445
|
+
name='get_weather_by_location',
|
|
446
|
+
args={'location': 'Boston'}
|
|
447
|
+
)
|
|
448
|
+
]
|
|
449
|
+
)
|
|
259
450
|
]
|
|
260
451
|
```
|
|
261
452
|
|
|
@@ -265,15 +456,17 @@ Where a `types.ModelContent` is a subclass of `types.Content`, the
|
|
|
265
456
|
##### Provide a list of function call parts
|
|
266
457
|
|
|
267
458
|
```python
|
|
459
|
+
from google.genai import types
|
|
460
|
+
|
|
268
461
|
contents = [
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
462
|
+
types.Part.from_function_call(
|
|
463
|
+
name='get_weather_by_location',
|
|
464
|
+
args={'location': 'Boston'}
|
|
465
|
+
),
|
|
466
|
+
types.Part.from_function_call(
|
|
467
|
+
name='get_weather_by_location',
|
|
468
|
+
args={'location': 'New York'}
|
|
469
|
+
),
|
|
277
470
|
]
|
|
278
471
|
```
|
|
279
472
|
|
|
@@ -281,18 +474,18 @@ The SDK converts a list of function call parts to the a content with a `model` r
|
|
|
281
474
|
|
|
282
475
|
```python
|
|
283
476
|
[
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
477
|
+
types.ModelContent(
|
|
478
|
+
parts=[
|
|
479
|
+
types.Part.from_function_call(
|
|
480
|
+
name='get_weather_by_location',
|
|
481
|
+
args={'location': 'Boston'}
|
|
482
|
+
),
|
|
483
|
+
types.Part.from_function_call(
|
|
484
|
+
name='get_weather_by_location',
|
|
485
|
+
args={'location': 'New York'}
|
|
486
|
+
)
|
|
487
|
+
]
|
|
488
|
+
)
|
|
296
489
|
]
|
|
297
490
|
```
|
|
298
491
|
|
|
@@ -302,9 +495,11 @@ Where a `types.ModelContent` is a subclass of `types.Content`, the
|
|
|
302
495
|
##### Provide a non function call part
|
|
303
496
|
|
|
304
497
|
```python
|
|
498
|
+
from google.genai import types
|
|
499
|
+
|
|
305
500
|
contents = types.Part.from_uri(
|
|
306
|
-
|
|
307
|
-
|
|
501
|
+
file_uri: 'gs://generativeai-downloads/images/scones.jpg',
|
|
502
|
+
mime_type: 'image/jpeg',
|
|
308
503
|
)
|
|
309
504
|
```
|
|
310
505
|
|
|
@@ -312,24 +507,26 @@ The SDK converts all non function call parts into a content with a `user` role.
|
|
|
312
507
|
|
|
313
508
|
```python
|
|
314
509
|
[
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
510
|
+
types.UserContent(parts=[
|
|
511
|
+
types.Part.from_uri(
|
|
512
|
+
file_uri: 'gs://generativeai-downloads/images/scones.jpg',
|
|
513
|
+
mime_type: 'image/jpeg',
|
|
514
|
+
)
|
|
515
|
+
])
|
|
321
516
|
]
|
|
322
517
|
```
|
|
323
518
|
|
|
324
519
|
##### Provide a list of non function call parts
|
|
325
520
|
|
|
326
521
|
```python
|
|
522
|
+
from google.genai import types
|
|
523
|
+
|
|
327
524
|
contents = [
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
525
|
+
types.Part.from_text('What is this image about?'),
|
|
526
|
+
types.Part.from_uri(
|
|
527
|
+
file_uri: 'gs://generativeai-downloads/images/scones.jpg',
|
|
528
|
+
mime_type: 'image/jpeg',
|
|
529
|
+
)
|
|
333
530
|
]
|
|
334
531
|
```
|
|
335
532
|
|
|
@@ -337,15 +534,15 @@ The SDK will convert the list of parts into a content with a `user` role
|
|
|
337
534
|
|
|
338
535
|
```python
|
|
339
536
|
[
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
537
|
+
types.UserContent(
|
|
538
|
+
parts=[
|
|
539
|
+
types.Part.from_text('What is this image about?'),
|
|
540
|
+
types.Part.from_uri(
|
|
541
|
+
file_uri: 'gs://generativeai-downloads/images/scones.jpg',
|
|
542
|
+
mime_type: 'image/jpeg',
|
|
543
|
+
)
|
|
544
|
+
]
|
|
545
|
+
)
|
|
349
546
|
]
|
|
350
547
|
```
|
|
351
548
|
|
|
@@ -363,11 +560,17 @@ If you put a list within a list, the inner list can only contain
|
|
|
363
560
|
### System Instructions and Other Configs
|
|
364
561
|
|
|
365
562
|
The output of the model can be influenced by several optional settings
|
|
366
|
-
available in generate_content's config parameter. For example,
|
|
367
|
-
|
|
368
|
-
|
|
563
|
+
available in generate_content's config parameter. For example, increasing
|
|
564
|
+
`max_output_tokens` is essential for longer model responses. To make a model more
|
|
565
|
+
deterministic, lowering the `temperature` parameter reduces randomness, with
|
|
566
|
+
values near 0 minimizing variability. Capabilities and parameter defaults for
|
|
567
|
+
each model is shown in the
|
|
568
|
+
[Vertex AI docs](https://cloud.google.com/vertex-ai/generative-ai/docs/models/gemini/2-5-flash)
|
|
569
|
+
and [Gemini API docs](https://ai.google.dev/gemini-api/docs/models) respectively.
|
|
369
570
|
|
|
370
571
|
```python
|
|
572
|
+
from google.genai import types
|
|
573
|
+
|
|
371
574
|
response = client.models.generate_content(
|
|
372
575
|
model='gemini-2.0-flash-001',
|
|
373
576
|
contents='high',
|
|
@@ -386,6 +589,8 @@ All API methods support Pydantic types for parameters as well as
|
|
|
386
589
|
dictionaries. You can get the type from `google.genai.types`.
|
|
387
590
|
|
|
388
591
|
```python
|
|
592
|
+
from google.genai import types
|
|
593
|
+
|
|
389
594
|
response = client.models.generate_content(
|
|
390
595
|
model='gemini-2.0-flash-001',
|
|
391
596
|
contents=types.Part.from_text(text='Why is the sky blue?'),
|
|
@@ -422,7 +627,7 @@ pager.next_page()
|
|
|
422
627
|
print(pager[0])
|
|
423
628
|
```
|
|
424
629
|
|
|
425
|
-
####
|
|
630
|
+
#### List Base Models (Asynchronous)
|
|
426
631
|
|
|
427
632
|
```python
|
|
428
633
|
async for job in await client.aio.models.list():
|
|
@@ -440,8 +645,10 @@ print(async_pager[0])
|
|
|
440
645
|
### Safety Settings
|
|
441
646
|
|
|
442
647
|
```python
|
|
648
|
+
from google.genai import types
|
|
649
|
+
|
|
443
650
|
response = client.models.generate_content(
|
|
444
|
-
model='gemini-2.
|
|
651
|
+
model='gemini-2.5-flash',
|
|
445
652
|
contents='Say something bad.',
|
|
446
653
|
config=types.GenerateContentConfig(
|
|
447
654
|
safety_settings=[
|
|
@@ -463,17 +670,19 @@ You can pass a Python function directly and it will be automatically
|
|
|
463
670
|
called and responded by default.
|
|
464
671
|
|
|
465
672
|
```python
|
|
673
|
+
from google.genai import types
|
|
674
|
+
|
|
466
675
|
def get_current_weather(location: str) -> str:
|
|
467
676
|
"""Returns the current weather.
|
|
468
677
|
|
|
469
678
|
Args:
|
|
470
|
-
|
|
679
|
+
location: The city and state, e.g. San Francisco, CA
|
|
471
680
|
"""
|
|
472
681
|
return 'sunny'
|
|
473
682
|
|
|
474
683
|
|
|
475
684
|
response = client.models.generate_content(
|
|
476
|
-
model='gemini-2.
|
|
685
|
+
model='gemini-2.5-flash',
|
|
477
686
|
contents='What is the weather like in Boston?',
|
|
478
687
|
config=types.GenerateContentConfig(tools=[get_current_weather]),
|
|
479
688
|
)
|
|
@@ -486,15 +695,17 @@ automatic function calling, you can disable automatic function calling
|
|
|
486
695
|
as follows:
|
|
487
696
|
|
|
488
697
|
```python
|
|
698
|
+
from google.genai import types
|
|
699
|
+
|
|
489
700
|
response = client.models.generate_content(
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
701
|
+
model='gemini-2.5-flash',
|
|
702
|
+
contents='What is the weather like in Boston?',
|
|
703
|
+
config=types.GenerateContentConfig(
|
|
704
|
+
tools=[get_current_weather],
|
|
705
|
+
automatic_function_calling=types.AutomaticFunctionCallingConfig(
|
|
706
|
+
disable=True
|
|
707
|
+
),
|
|
496
708
|
),
|
|
497
|
-
),
|
|
498
709
|
)
|
|
499
710
|
```
|
|
500
711
|
|
|
@@ -514,25 +725,27 @@ The following example shows how to declare a function and pass it as a tool.
|
|
|
514
725
|
Then you will receive a function call part in the response.
|
|
515
726
|
|
|
516
727
|
```python
|
|
728
|
+
from google.genai import types
|
|
729
|
+
|
|
517
730
|
function = types.FunctionDeclaration(
|
|
518
731
|
name='get_current_weather',
|
|
519
732
|
description='Get the current weather in a given location',
|
|
520
|
-
|
|
521
|
-
type
|
|
522
|
-
properties
|
|
523
|
-
'location':
|
|
524
|
-
type
|
|
525
|
-
description
|
|
526
|
-
|
|
733
|
+
parameters_json_schema={
|
|
734
|
+
'type': 'object',
|
|
735
|
+
'properties': {
|
|
736
|
+
'location': {
|
|
737
|
+
'type': 'string',
|
|
738
|
+
'description': 'The city and state, e.g. San Francisco, CA',
|
|
739
|
+
}
|
|
527
740
|
},
|
|
528
|
-
required
|
|
529
|
-
|
|
741
|
+
'required': ['location'],
|
|
742
|
+
},
|
|
530
743
|
)
|
|
531
744
|
|
|
532
745
|
tool = types.Tool(function_declarations=[function])
|
|
533
746
|
|
|
534
747
|
response = client.models.generate_content(
|
|
535
|
-
model='gemini-2.
|
|
748
|
+
model='gemini-2.5-flash',
|
|
536
749
|
contents='What is the weather like in Boston?',
|
|
537
750
|
config=types.GenerateContentConfig(tools=[tool]),
|
|
538
751
|
)
|
|
@@ -546,6 +759,8 @@ the model.
|
|
|
546
759
|
The following example shows how to do it for a simple function invocation.
|
|
547
760
|
|
|
548
761
|
```python
|
|
762
|
+
from google.genai import types
|
|
763
|
+
|
|
549
764
|
user_prompt_content = types.Content(
|
|
550
765
|
role='user',
|
|
551
766
|
parts=[types.Part.from_text(text='What is the weather like in Boston?')],
|
|
@@ -574,7 +789,7 @@ function_response_content = types.Content(
|
|
|
574
789
|
)
|
|
575
790
|
|
|
576
791
|
response = client.models.generate_content(
|
|
577
|
-
model='gemini-2.
|
|
792
|
+
model='gemini-2.5-flash',
|
|
578
793
|
contents=[
|
|
579
794
|
user_prompt_content,
|
|
580
795
|
function_call_content,
|
|
@@ -598,16 +813,18 @@ maximum remote call for automatic function calling (default to 10 times).
|
|
|
598
813
|
If you'd like to disable automatic function calling in `ANY` mode:
|
|
599
814
|
|
|
600
815
|
```python
|
|
816
|
+
from google.genai import types
|
|
817
|
+
|
|
601
818
|
def get_current_weather(location: str) -> str:
|
|
602
819
|
"""Returns the current weather.
|
|
603
820
|
|
|
604
821
|
Args:
|
|
605
|
-
|
|
822
|
+
location: The city and state, e.g. San Francisco, CA
|
|
606
823
|
"""
|
|
607
824
|
return "sunny"
|
|
608
825
|
|
|
609
826
|
response = client.models.generate_content(
|
|
610
|
-
model="gemini-2.
|
|
827
|
+
model="gemini-2.5-flash",
|
|
611
828
|
contents="What is the weather like in Boston?",
|
|
612
829
|
config=types.GenerateContentConfig(
|
|
613
830
|
tools=[get_current_weather],
|
|
@@ -626,16 +843,18 @@ configure the maximum remote calls to be `x + 1`.
|
|
|
626
843
|
Assuming you prefer `1` turn for automatic function calling.
|
|
627
844
|
|
|
628
845
|
```python
|
|
846
|
+
from google.genai import types
|
|
847
|
+
|
|
629
848
|
def get_current_weather(location: str) -> str:
|
|
630
849
|
"""Returns the current weather.
|
|
631
850
|
|
|
632
851
|
Args:
|
|
633
|
-
|
|
852
|
+
location: The city and state, e.g. San Francisco, CA
|
|
634
853
|
"""
|
|
635
854
|
return "sunny"
|
|
636
855
|
|
|
637
856
|
response = client.models.generate_content(
|
|
638
|
-
model="gemini-2.
|
|
857
|
+
model="gemini-2.5-flash",
|
|
639
858
|
contents="What is the weather like in Boston?",
|
|
640
859
|
config=types.GenerateContentConfig(
|
|
641
860
|
tools=[get_current_weather],
|
|
@@ -648,18 +867,100 @@ response = client.models.generate_content(
|
|
|
648
867
|
),
|
|
649
868
|
)
|
|
650
869
|
```
|
|
870
|
+
|
|
871
|
+
#### Model Context Protocol (MCP) support (experimental)
|
|
872
|
+
|
|
873
|
+
Built-in [MCP](https://modelcontextprotocol.io/introduction) support is an
|
|
874
|
+
experimental feature. You can pass a local MCP server as a tool directly.
|
|
875
|
+
|
|
876
|
+
```python
|
|
877
|
+
import os
|
|
878
|
+
import asyncio
|
|
879
|
+
from datetime import datetime
|
|
880
|
+
from mcp import ClientSession, StdioServerParameters
|
|
881
|
+
from mcp.client.stdio import stdio_client
|
|
882
|
+
from google import genai
|
|
883
|
+
|
|
884
|
+
client = genai.Client()
|
|
885
|
+
|
|
886
|
+
# Create server parameters for stdio connection
|
|
887
|
+
server_params = StdioServerParameters(
|
|
888
|
+
command="npx", # Executable
|
|
889
|
+
args=["-y", "@philschmid/weather-mcp"], # MCP Server
|
|
890
|
+
env=None, # Optional environment variables
|
|
891
|
+
)
|
|
892
|
+
|
|
893
|
+
async def run():
|
|
894
|
+
async with stdio_client(server_params) as (read, write):
|
|
895
|
+
async with ClientSession(read, write) as session:
|
|
896
|
+
# Prompt to get the weather for the current day in London.
|
|
897
|
+
prompt = f"What is the weather in London in {datetime.now().strftime('%Y-%m-%d')}?"
|
|
898
|
+
|
|
899
|
+
# Initialize the connection between client and server
|
|
900
|
+
await session.initialize()
|
|
901
|
+
|
|
902
|
+
# Send request to the model with MCP function declarations
|
|
903
|
+
response = await client.aio.models.generate_content(
|
|
904
|
+
model="gemini-2.5-flash",
|
|
905
|
+
contents=prompt,
|
|
906
|
+
config=genai.types.GenerateContentConfig(
|
|
907
|
+
temperature=0,
|
|
908
|
+
tools=[session], # uses the session, will automatically call the tool using automatic function calling
|
|
909
|
+
),
|
|
910
|
+
)
|
|
911
|
+
print(response.text)
|
|
912
|
+
|
|
913
|
+
# Start the asyncio event loop and run the main function
|
|
914
|
+
asyncio.run(run())
|
|
915
|
+
```
|
|
916
|
+
|
|
651
917
|
### JSON Response Schema
|
|
652
918
|
|
|
653
919
|
However you define your schema, don't duplicate it in your input prompt,
|
|
654
920
|
including by giving examples of expected JSON output. If you do, the generated
|
|
655
921
|
output might be lower in quality.
|
|
656
922
|
|
|
923
|
+
#### JSON Schema support
|
|
924
|
+
Schemas can be provided as standard JSON schema.
|
|
925
|
+
```python
|
|
926
|
+
user_profile = {
|
|
927
|
+
'properties': {
|
|
928
|
+
'age': {
|
|
929
|
+
'anyOf': [
|
|
930
|
+
{'maximum': 20, 'minimum': 0, 'type': 'integer'},
|
|
931
|
+
{'type': 'null'},
|
|
932
|
+
],
|
|
933
|
+
'title': 'Age',
|
|
934
|
+
},
|
|
935
|
+
'username': {
|
|
936
|
+
'description': "User's unique name",
|
|
937
|
+
'title': 'Username',
|
|
938
|
+
'type': 'string',
|
|
939
|
+
},
|
|
940
|
+
},
|
|
941
|
+
'required': ['username', 'age'],
|
|
942
|
+
'title': 'User Schema',
|
|
943
|
+
'type': 'object',
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
response = client.models.generate_content(
|
|
947
|
+
model='gemini-2.5-flash',
|
|
948
|
+
contents='Give me a random user profile.',
|
|
949
|
+
config={
|
|
950
|
+
'response_mime_type': 'application/json',
|
|
951
|
+
'response_json_schema': user_profile
|
|
952
|
+
},
|
|
953
|
+
)
|
|
954
|
+
print(response.parsed)
|
|
955
|
+
```
|
|
956
|
+
|
|
657
957
|
#### Pydantic Model Schema support
|
|
658
958
|
|
|
659
959
|
Schemas can be provided as Pydantic Models.
|
|
660
960
|
|
|
661
961
|
```python
|
|
662
962
|
from pydantic import BaseModel
|
|
963
|
+
from google.genai import types
|
|
663
964
|
|
|
664
965
|
|
|
665
966
|
class CountryInfo(BaseModel):
|
|
@@ -673,7 +974,7 @@ class CountryInfo(BaseModel):
|
|
|
673
974
|
|
|
674
975
|
|
|
675
976
|
response = client.models.generate_content(
|
|
676
|
-
model='gemini-2.
|
|
977
|
+
model='gemini-2.5-flash',
|
|
677
978
|
contents='Give me information for the United States.',
|
|
678
979
|
config=types.GenerateContentConfig(
|
|
679
980
|
response_mime_type='application/json',
|
|
@@ -684,8 +985,10 @@ print(response.text)
|
|
|
684
985
|
```
|
|
685
986
|
|
|
686
987
|
```python
|
|
988
|
+
from google.genai import types
|
|
989
|
+
|
|
687
990
|
response = client.models.generate_content(
|
|
688
|
-
model='gemini-2.
|
|
991
|
+
model='gemini-2.5-flash',
|
|
689
992
|
contents='Give me information for the United States.',
|
|
690
993
|
config=types.GenerateContentConfig(
|
|
691
994
|
response_mime_type='application/json',
|
|
@@ -723,56 +1026,62 @@ You can set response_mime_type to 'text/x.enum' to return one of those enum
|
|
|
723
1026
|
values as the response.
|
|
724
1027
|
|
|
725
1028
|
```python
|
|
1029
|
+
from enum import Enum
|
|
1030
|
+
|
|
726
1031
|
class InstrumentEnum(Enum):
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
1032
|
+
PERCUSSION = 'Percussion'
|
|
1033
|
+
STRING = 'String'
|
|
1034
|
+
WOODWIND = 'Woodwind'
|
|
1035
|
+
BRASS = 'Brass'
|
|
1036
|
+
KEYBOARD = 'Keyboard'
|
|
732
1037
|
|
|
733
1038
|
response = client.models.generate_content(
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
1039
|
+
model='gemini-2.5-flash',
|
|
1040
|
+
contents='What instrument plays multiple notes at once?',
|
|
1041
|
+
config={
|
|
1042
|
+
'response_mime_type': 'text/x.enum',
|
|
1043
|
+
'response_schema': InstrumentEnum,
|
|
1044
|
+
},
|
|
1045
|
+
)
|
|
741
1046
|
print(response.text)
|
|
742
1047
|
```
|
|
743
1048
|
|
|
744
1049
|
#### JSON Response
|
|
745
1050
|
|
|
746
|
-
You can also set response_mime_type to 'application/json', the response will be
|
|
1051
|
+
You can also set response_mime_type to 'application/json', the response will be
|
|
1052
|
+
identical but in quotes.
|
|
747
1053
|
|
|
748
1054
|
```python
|
|
749
1055
|
from enum import Enum
|
|
750
1056
|
|
|
751
1057
|
class InstrumentEnum(Enum):
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
1058
|
+
PERCUSSION = 'Percussion'
|
|
1059
|
+
STRING = 'String'
|
|
1060
|
+
WOODWIND = 'Woodwind'
|
|
1061
|
+
BRASS = 'Brass'
|
|
1062
|
+
KEYBOARD = 'Keyboard'
|
|
757
1063
|
|
|
758
1064
|
response = client.models.generate_content(
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
1065
|
+
model='gemini-2.5-flash',
|
|
1066
|
+
contents='What instrument plays multiple notes at once?',
|
|
1067
|
+
config={
|
|
1068
|
+
'response_mime_type': 'application/json',
|
|
1069
|
+
'response_schema': InstrumentEnum,
|
|
1070
|
+
},
|
|
1071
|
+
)
|
|
766
1072
|
print(response.text)
|
|
767
1073
|
```
|
|
768
1074
|
|
|
769
|
-
### Streaming
|
|
1075
|
+
### Generate Content (Synchronous Streaming)
|
|
1076
|
+
|
|
1077
|
+
Generate content in a streaming format so that the model outputs streams back
|
|
1078
|
+
to you, rather than being returned as one chunk.
|
|
770
1079
|
|
|
771
1080
|
#### Streaming for text content
|
|
772
1081
|
|
|
773
1082
|
```python
|
|
774
1083
|
for chunk in client.models.generate_content_stream(
|
|
775
|
-
model='gemini-2.
|
|
1084
|
+
model='gemini-2.5-flash', contents='Tell me a story in 300 words.'
|
|
776
1085
|
):
|
|
777
1086
|
print(chunk.text, end='')
|
|
778
1087
|
```
|
|
@@ -783,8 +1092,10 @@ If your image is stored in [Google Cloud Storage](https://cloud.google.com/stora
|
|
|
783
1092
|
you can use the `from_uri` class method to create a `Part` object.
|
|
784
1093
|
|
|
785
1094
|
```python
|
|
1095
|
+
from google.genai import types
|
|
1096
|
+
|
|
786
1097
|
for chunk in client.models.generate_content_stream(
|
|
787
|
-
model='gemini-2.
|
|
1098
|
+
model='gemini-2.5-flash',
|
|
788
1099
|
contents=[
|
|
789
1100
|
'What is this image about?',
|
|
790
1101
|
types.Part.from_uri(
|
|
@@ -800,13 +1111,15 @@ If your image is stored in your local file system, you can read it in as bytes
|
|
|
800
1111
|
data and use the `from_bytes` class method to create a `Part` object.
|
|
801
1112
|
|
|
802
1113
|
```python
|
|
1114
|
+
from google.genai import types
|
|
1115
|
+
|
|
803
1116
|
YOUR_IMAGE_PATH = 'your_image_path'
|
|
804
1117
|
YOUR_IMAGE_MIME_TYPE = 'your_image_mime_type'
|
|
805
1118
|
with open(YOUR_IMAGE_PATH, 'rb') as f:
|
|
806
1119
|
image_bytes = f.read()
|
|
807
1120
|
|
|
808
1121
|
for chunk in client.models.generate_content_stream(
|
|
809
|
-
model='gemini-2.
|
|
1122
|
+
model='gemini-2.5-flash',
|
|
810
1123
|
contents=[
|
|
811
1124
|
'What is this image about?',
|
|
812
1125
|
types.Part.from_bytes(data=image_bytes, mime_type=YOUR_IMAGE_MIME_TYPE),
|
|
@@ -815,27 +1128,27 @@ for chunk in client.models.generate_content_stream(
|
|
|
815
1128
|
print(chunk.text, end='')
|
|
816
1129
|
```
|
|
817
1130
|
|
|
818
|
-
###
|
|
1131
|
+
### Generate Content (Asynchronous Non Streaming)
|
|
819
1132
|
|
|
820
1133
|
`client.aio` exposes all the analogous [`async` methods](https://docs.python.org/3/library/asyncio.html)
|
|
821
|
-
that are available on `client
|
|
1134
|
+
that are available on `client`. Note that it applies to all the modules.
|
|
822
1135
|
|
|
823
1136
|
For example, `client.aio.models.generate_content` is the `async` version
|
|
824
1137
|
of `client.models.generate_content`
|
|
825
1138
|
|
|
826
1139
|
```python
|
|
827
1140
|
response = await client.aio.models.generate_content(
|
|
828
|
-
model='gemini-2.
|
|
1141
|
+
model='gemini-2.5-flash', contents='Tell me a story in 300 words.'
|
|
829
1142
|
)
|
|
830
1143
|
|
|
831
1144
|
print(response.text)
|
|
832
1145
|
```
|
|
833
1146
|
|
|
834
|
-
### Streaming
|
|
1147
|
+
### Generate Content (Asynchronous Streaming)
|
|
835
1148
|
|
|
836
1149
|
```python
|
|
837
1150
|
async for chunk in await client.aio.models.generate_content_stream(
|
|
838
|
-
model='gemini-2.
|
|
1151
|
+
model='gemini-2.5-flash', contents='Tell me a story in 300 words.'
|
|
839
1152
|
):
|
|
840
1153
|
print(chunk.text, end='')
|
|
841
1154
|
```
|
|
@@ -844,7 +1157,7 @@ async for chunk in await client.aio.models.generate_content_stream(
|
|
|
844
1157
|
|
|
845
1158
|
```python
|
|
846
1159
|
response = client.models.count_tokens(
|
|
847
|
-
model='gemini-2.
|
|
1160
|
+
model='gemini-2.5-flash',
|
|
848
1161
|
contents='why is the sky blue?',
|
|
849
1162
|
)
|
|
850
1163
|
print(response)
|
|
@@ -856,7 +1169,7 @@ Compute tokens is only supported in Vertex AI.
|
|
|
856
1169
|
|
|
857
1170
|
```python
|
|
858
1171
|
response = client.models.compute_tokens(
|
|
859
|
-
model='gemini-2.
|
|
1172
|
+
model='gemini-2.5-flash',
|
|
860
1173
|
contents='why is the sky blue?',
|
|
861
1174
|
)
|
|
862
1175
|
print(response)
|
|
@@ -866,26 +1179,42 @@ print(response)
|
|
|
866
1179
|
|
|
867
1180
|
```python
|
|
868
1181
|
response = await client.aio.models.count_tokens(
|
|
869
|
-
model='gemini-2.
|
|
1182
|
+
model='gemini-2.5-flash',
|
|
870
1183
|
contents='why is the sky blue?',
|
|
871
1184
|
)
|
|
872
1185
|
print(response)
|
|
873
1186
|
```
|
|
874
1187
|
|
|
1188
|
+
#### Local Count Tokens
|
|
1189
|
+
|
|
1190
|
+
```python
|
|
1191
|
+
tokenizer = genai.LocalTokenizer(model_name='gemini-2.5-flash')
|
|
1192
|
+
result = tokenizer.count_tokens("What is your name?")
|
|
1193
|
+
```
|
|
1194
|
+
|
|
1195
|
+
#### Local Compute Tokens
|
|
1196
|
+
|
|
1197
|
+
```python
|
|
1198
|
+
tokenizer = genai.LocalTokenizer(model_name='gemini-2.5-flash')
|
|
1199
|
+
result = tokenizer.compute_tokens("What is your name?")
|
|
1200
|
+
```
|
|
1201
|
+
|
|
875
1202
|
### Embed Content
|
|
876
1203
|
|
|
877
1204
|
```python
|
|
878
1205
|
response = client.models.embed_content(
|
|
879
|
-
model='
|
|
1206
|
+
model='gemini-embedding-001',
|
|
880
1207
|
contents='why is the sky blue?',
|
|
881
1208
|
)
|
|
882
1209
|
print(response)
|
|
883
1210
|
```
|
|
884
1211
|
|
|
885
1212
|
```python
|
|
1213
|
+
from google.genai import types
|
|
1214
|
+
|
|
886
1215
|
# multiple contents with config
|
|
887
1216
|
response = client.models.embed_content(
|
|
888
|
-
model='
|
|
1217
|
+
model='gemini-embedding-001',
|
|
889
1218
|
contents=['why is the sky blue?', 'What is your age?'],
|
|
890
1219
|
config=types.EmbedContentConfig(output_dimensionality=10),
|
|
891
1220
|
)
|
|
@@ -900,9 +1229,11 @@ print(response)
|
|
|
900
1229
|
Support for generate images in Gemini Developer API is behind an allowlist
|
|
901
1230
|
|
|
902
1231
|
```python
|
|
1232
|
+
from google.genai import types
|
|
1233
|
+
|
|
903
1234
|
# Generate Image
|
|
904
1235
|
response1 = client.models.generate_images(
|
|
905
|
-
model='imagen-
|
|
1236
|
+
model='imagen-4.0-generate-001',
|
|
906
1237
|
prompt='An umbrella in the foreground, and a rainy night sky in the background',
|
|
907
1238
|
config=types.GenerateImagesConfig(
|
|
908
1239
|
number_of_images=1,
|
|
@@ -918,9 +1249,11 @@ response1.generated_images[0].image.show()
|
|
|
918
1249
|
Upscale image is only supported in Vertex AI.
|
|
919
1250
|
|
|
920
1251
|
```python
|
|
1252
|
+
from google.genai import types
|
|
1253
|
+
|
|
921
1254
|
# Upscale the generated image from above
|
|
922
1255
|
response2 = client.models.upscale_image(
|
|
923
|
-
model='imagen-
|
|
1256
|
+
model='imagen-4.0-upscale-preview',
|
|
924
1257
|
image=response1.generated_images[0].image,
|
|
925
1258
|
upscale_factor='x2',
|
|
926
1259
|
config=types.UpscaleImageConfig(
|
|
@@ -939,6 +1272,7 @@ Edit image is only supported in Vertex AI.
|
|
|
939
1272
|
|
|
940
1273
|
```python
|
|
941
1274
|
# Edit the generated image from above
|
|
1275
|
+
from google.genai import types
|
|
942
1276
|
from google.genai.types import RawReferenceImage, MaskReferenceImage
|
|
943
1277
|
|
|
944
1278
|
raw_ref_image = RawReferenceImage(
|
|
@@ -971,18 +1305,19 @@ response3.generated_images[0].image.show()
|
|
|
971
1305
|
|
|
972
1306
|
### Veo
|
|
973
1307
|
|
|
974
|
-
|
|
1308
|
+
Support for generating videos is considered public preview
|
|
975
1309
|
|
|
976
|
-
|
|
1310
|
+
#### Generate Videos (Text to Video)
|
|
977
1311
|
|
|
978
1312
|
```python
|
|
1313
|
+
from google.genai import types
|
|
1314
|
+
|
|
979
1315
|
# Create operation
|
|
980
1316
|
operation = client.models.generate_videos(
|
|
981
|
-
model='veo-
|
|
1317
|
+
model='veo-3.1-generate-preview',
|
|
982
1318
|
prompt='A neon hologram of a cat driving at top speed',
|
|
983
1319
|
config=types.GenerateVideosConfig(
|
|
984
1320
|
number_of_videos=1,
|
|
985
|
-
fps=24,
|
|
986
1321
|
duration_seconds=5,
|
|
987
1322
|
enhance_prompt=True,
|
|
988
1323
|
),
|
|
@@ -993,51 +1328,124 @@ while not operation.done:
|
|
|
993
1328
|
time.sleep(20)
|
|
994
1329
|
operation = client.operations.get(operation)
|
|
995
1330
|
|
|
996
|
-
video = operation.
|
|
1331
|
+
video = operation.response.generated_videos[0].video
|
|
1332
|
+
video.show()
|
|
1333
|
+
```
|
|
1334
|
+
|
|
1335
|
+
#### Generate Videos (Image to Video)
|
|
1336
|
+
|
|
1337
|
+
```python
|
|
1338
|
+
from google.genai import types
|
|
1339
|
+
|
|
1340
|
+
# Read local image (uses mimetypes.guess_type to infer mime type)
|
|
1341
|
+
image = types.Image.from_file("local/path/file.png")
|
|
1342
|
+
|
|
1343
|
+
# Create operation
|
|
1344
|
+
operation = client.models.generate_videos(
|
|
1345
|
+
model='veo-3.1-generate-preview',
|
|
1346
|
+
# Prompt is optional if image is provided
|
|
1347
|
+
prompt='Night sky',
|
|
1348
|
+
image=image,
|
|
1349
|
+
config=types.GenerateVideosConfig(
|
|
1350
|
+
number_of_videos=1,
|
|
1351
|
+
duration_seconds=5,
|
|
1352
|
+
enhance_prompt=True,
|
|
1353
|
+
# Can also pass an Image into last_frame for frame interpolation
|
|
1354
|
+
),
|
|
1355
|
+
)
|
|
1356
|
+
|
|
1357
|
+
# Poll operation
|
|
1358
|
+
while not operation.done:
|
|
1359
|
+
time.sleep(20)
|
|
1360
|
+
operation = client.operations.get(operation)
|
|
1361
|
+
|
|
1362
|
+
video = operation.response.generated_videos[0].video
|
|
1363
|
+
video.show()
|
|
1364
|
+
```
|
|
1365
|
+
|
|
1366
|
+
#### Generate Videos (Video to Video)
|
|
1367
|
+
|
|
1368
|
+
Currently, only Gemini Developer API supports video extension on Veo 3.1 for
|
|
1369
|
+
previously generated videos. Vertex supports video extension on Veo 2.0.
|
|
1370
|
+
|
|
1371
|
+
```python
|
|
1372
|
+
from google.genai import types
|
|
1373
|
+
|
|
1374
|
+
# Read local video (uses mimetypes.guess_type to infer mime type)
|
|
1375
|
+
video = types.Video.from_file("local/path/video.mp4")
|
|
1376
|
+
|
|
1377
|
+
# Create operation
|
|
1378
|
+
operation = client.models.generate_videos(
|
|
1379
|
+
model='veo-3.1-generate-preview',
|
|
1380
|
+
# Prompt is optional if Video is provided
|
|
1381
|
+
prompt='Night sky',
|
|
1382
|
+
# Input video must be in GCS for Vertex or a URI for Gemini
|
|
1383
|
+
video=types.Video(
|
|
1384
|
+
uri="gs://bucket-name/inputs/videos/cat_driving.mp4",
|
|
1385
|
+
),
|
|
1386
|
+
config=types.GenerateVideosConfig(
|
|
1387
|
+
number_of_videos=1,
|
|
1388
|
+
duration_seconds=5,
|
|
1389
|
+
enhance_prompt=True,
|
|
1390
|
+
),
|
|
1391
|
+
)
|
|
1392
|
+
|
|
1393
|
+
# Poll operation
|
|
1394
|
+
while not operation.done:
|
|
1395
|
+
time.sleep(20)
|
|
1396
|
+
operation = client.operations.get(operation)
|
|
1397
|
+
|
|
1398
|
+
video = operation.response.generated_videos[0].video
|
|
997
1399
|
video.show()
|
|
998
1400
|
```
|
|
999
1401
|
|
|
1000
1402
|
## Chats
|
|
1001
1403
|
|
|
1002
|
-
Create a chat session to start a multi-turn conversations with the model.
|
|
1404
|
+
Create a chat session to start a multi-turn conversations with the model. Then,
|
|
1405
|
+
use `chat.send_message` function multiple times within the same chat session so
|
|
1406
|
+
that it can reflect on its previous responses (i.e., engage in an ongoing
|
|
1407
|
+
conversation). See the 'Create a client' section above to initialize a client.
|
|
1003
1408
|
|
|
1004
|
-
### Send Message
|
|
1409
|
+
### Send Message (Synchronous Non-Streaming)
|
|
1005
1410
|
|
|
1006
1411
|
```python
|
|
1007
|
-
chat = client.chats.create(model='gemini-2.
|
|
1412
|
+
chat = client.chats.create(model='gemini-2.5-flash')
|
|
1008
1413
|
response = chat.send_message('tell me a story')
|
|
1009
1414
|
print(response.text)
|
|
1415
|
+
response = chat.send_message('summarize the story you told me in 1 sentence')
|
|
1416
|
+
print(response.text)
|
|
1010
1417
|
```
|
|
1011
1418
|
|
|
1012
|
-
### Streaming
|
|
1419
|
+
### Send Message (Synchronous Streaming)
|
|
1013
1420
|
|
|
1014
1421
|
```python
|
|
1015
|
-
chat = client.chats.create(model='gemini-2.
|
|
1422
|
+
chat = client.chats.create(model='gemini-2.5-flash')
|
|
1016
1423
|
for chunk in chat.send_message_stream('tell me a story'):
|
|
1017
1424
|
print(chunk.text)
|
|
1018
1425
|
```
|
|
1019
1426
|
|
|
1020
|
-
###
|
|
1427
|
+
### Send Message (Asynchronous Non-Streaming)
|
|
1021
1428
|
|
|
1022
1429
|
```python
|
|
1023
|
-
chat = client.aio.chats.create(model='gemini-2.
|
|
1430
|
+
chat = client.aio.chats.create(model='gemini-2.5-flash')
|
|
1024
1431
|
response = await chat.send_message('tell me a story')
|
|
1025
1432
|
print(response.text)
|
|
1026
1433
|
```
|
|
1027
1434
|
|
|
1028
|
-
###
|
|
1435
|
+
### Send Message (Asynchronous Streaming)
|
|
1029
1436
|
|
|
1030
1437
|
```python
|
|
1031
|
-
chat = client.aio.chats.create(model='gemini-2.
|
|
1438
|
+
chat = client.aio.chats.create(model='gemini-2.5-flash')
|
|
1032
1439
|
async for chunk in await chat.send_message_stream('tell me a story'):
|
|
1033
1440
|
print(chunk.text)
|
|
1034
1441
|
```
|
|
1035
1442
|
|
|
1036
1443
|
## Files
|
|
1037
1444
|
|
|
1038
|
-
Files are only supported in Gemini Developer API.
|
|
1445
|
+
Files are only supported in Gemini Developer API. See the 'Create a client'
|
|
1446
|
+
section above to initialize a client.
|
|
1039
1447
|
|
|
1040
|
-
```
|
|
1448
|
+
```sh
|
|
1041
1449
|
!gsutil cp gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf .
|
|
1042
1450
|
!gsutil cp gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf .
|
|
1043
1451
|
```
|
|
@@ -1069,11 +1477,14 @@ client.files.delete(name=file3.name)
|
|
|
1069
1477
|
|
|
1070
1478
|
## Caches
|
|
1071
1479
|
|
|
1072
|
-
`client.caches` contains the control plane APIs for cached content
|
|
1480
|
+
`client.caches` contains the control plane APIs for cached content. See the
|
|
1481
|
+
'Create a client' section above to initialize a client.
|
|
1073
1482
|
|
|
1074
1483
|
### Create
|
|
1075
1484
|
|
|
1076
1485
|
```python
|
|
1486
|
+
from google.genai import types
|
|
1487
|
+
|
|
1077
1488
|
if client.vertexai:
|
|
1078
1489
|
file_uris = [
|
|
1079
1490
|
'gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf',
|
|
@@ -1083,7 +1494,7 @@ else:
|
|
|
1083
1494
|
file_uris = [file1.uri, file2.uri]
|
|
1084
1495
|
|
|
1085
1496
|
cached_content = client.caches.create(
|
|
1086
|
-
model='gemini-2.
|
|
1497
|
+
model='gemini-2.5-flash',
|
|
1087
1498
|
config=types.CreateCachedContentConfig(
|
|
1088
1499
|
contents=[
|
|
1089
1500
|
types.Content(
|
|
@@ -1115,8 +1526,10 @@ cached_content = client.caches.get(name=cached_content.name)
|
|
|
1115
1526
|
### Generate Content with Caches
|
|
1116
1527
|
|
|
1117
1528
|
```python
|
|
1529
|
+
from google.genai import types
|
|
1530
|
+
|
|
1118
1531
|
response = client.models.generate_content(
|
|
1119
|
-
model='gemini-2.
|
|
1532
|
+
model='gemini-2.5-flash',
|
|
1120
1533
|
contents='Summarize the pdfs',
|
|
1121
1534
|
config=types.GenerateContentConfig(
|
|
1122
1535
|
cached_content=cached_content.name,
|
|
@@ -1128,33 +1541,26 @@ print(response.text)
|
|
|
1128
1541
|
## Tunings
|
|
1129
1542
|
|
|
1130
1543
|
`client.tunings` contains tuning job APIs and supports supervised fine
|
|
1131
|
-
tuning through `tune`.
|
|
1544
|
+
tuning through `tune`. Only supported in Vertex AI. See the 'Create a client'
|
|
1545
|
+
section above to initialize a client.
|
|
1132
1546
|
|
|
1133
1547
|
### Tune
|
|
1134
1548
|
|
|
1135
|
-
- Vertex AI supports tuning from GCS source
|
|
1136
|
-
- Gemini Developer API supports tuning from inline examples
|
|
1549
|
+
- Vertex AI supports tuning from GCS source or from a [Vertex AI Multimodal Dataset](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/multimodal/datasets)
|
|
1137
1550
|
|
|
1138
1551
|
```python
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
training_dataset = types.TuningDataset(
|
|
1147
|
-
examples=[
|
|
1148
|
-
types.TuningExample(
|
|
1149
|
-
text_input=f'Input text {i}',
|
|
1150
|
-
output=f'Output text {i}',
|
|
1151
|
-
)
|
|
1152
|
-
for i in range(5)
|
|
1153
|
-
],
|
|
1154
|
-
)
|
|
1552
|
+
from google.genai import types
|
|
1553
|
+
|
|
1554
|
+
model = 'gemini-2.5-flash'
|
|
1555
|
+
training_dataset = types.TuningDataset(
|
|
1556
|
+
# or gcs_uri=my_vertex_multimodal_dataset
|
|
1557
|
+
gcs_uri='gs://your-gcs-bucket/your-tuning-data.jsonl',
|
|
1558
|
+
)
|
|
1155
1559
|
```
|
|
1156
1560
|
|
|
1157
1561
|
```python
|
|
1562
|
+
from google.genai import types
|
|
1563
|
+
|
|
1158
1564
|
tuning_job = client.tunings.tune(
|
|
1159
1565
|
base_model=model,
|
|
1160
1566
|
training_dataset=training_dataset,
|
|
@@ -1175,14 +1581,15 @@ print(tuning_job)
|
|
|
1175
1581
|
```python
|
|
1176
1582
|
import time
|
|
1177
1583
|
|
|
1178
|
-
|
|
1584
|
+
completed_states = set(
|
|
1179
1585
|
[
|
|
1180
|
-
'
|
|
1181
|
-
'
|
|
1586
|
+
'JOB_STATE_SUCCEEDED',
|
|
1587
|
+
'JOB_STATE_FAILED',
|
|
1588
|
+
'JOB_STATE_CANCELLED',
|
|
1182
1589
|
]
|
|
1183
1590
|
)
|
|
1184
1591
|
|
|
1185
|
-
while tuning_job.state in
|
|
1592
|
+
while tuning_job.state not in completed_states:
|
|
1186
1593
|
print(tuning_job.state)
|
|
1187
1594
|
tuning_job = client.tunings.get(name=tuning_job.name)
|
|
1188
1595
|
time.sleep(10)
|
|
@@ -1241,6 +1648,8 @@ print(async_pager[0])
|
|
|
1241
1648
|
### Update Tuned Model
|
|
1242
1649
|
|
|
1243
1650
|
```python
|
|
1651
|
+
from google.genai import types
|
|
1652
|
+
|
|
1244
1653
|
model = pager[0]
|
|
1245
1654
|
|
|
1246
1655
|
model = client.models.update(
|
|
@@ -1286,20 +1695,68 @@ print(async_pager[0])
|
|
|
1286
1695
|
|
|
1287
1696
|
## Batch Prediction
|
|
1288
1697
|
|
|
1289
|
-
Only supported in Vertex AI.
|
|
1698
|
+
Only supported in Vertex AI. See the 'Create a client' section above to
|
|
1699
|
+
initialize a client.
|
|
1290
1700
|
|
|
1291
1701
|
### Create
|
|
1292
1702
|
|
|
1703
|
+
Vertex AI:
|
|
1704
|
+
|
|
1293
1705
|
```python
|
|
1294
1706
|
# Specify model and source file only, destination and job display name will be auto-populated
|
|
1295
1707
|
job = client.batches.create(
|
|
1296
|
-
model='gemini-2.
|
|
1297
|
-
src='bq://my-project.my-dataset.my-table',
|
|
1708
|
+
model='gemini-2.5-flash',
|
|
1709
|
+
src='bq://my-project.my-dataset.my-table', # or "gs://path/to/input/data"
|
|
1710
|
+
)
|
|
1711
|
+
|
|
1712
|
+
print(job)
|
|
1713
|
+
```
|
|
1714
|
+
|
|
1715
|
+
Gemini Developer API:
|
|
1716
|
+
|
|
1717
|
+
```python
|
|
1718
|
+
# Create a batch job with inlined requests
|
|
1719
|
+
batch_job = client.batches.create(
|
|
1720
|
+
model="gemini-2.5-flash",
|
|
1721
|
+
src=[{
|
|
1722
|
+
"contents": [{
|
|
1723
|
+
"parts": [{
|
|
1724
|
+
"text": "Hello!",
|
|
1725
|
+
}],
|
|
1726
|
+
"role": "user",
|
|
1727
|
+
}],
|
|
1728
|
+
"config": {"response_modalities": ["text"]},
|
|
1729
|
+
}],
|
|
1298
1730
|
)
|
|
1299
1731
|
|
|
1300
1732
|
job
|
|
1301
1733
|
```
|
|
1302
1734
|
|
|
1735
|
+
In order to create a batch job with file name. Need to upload a json file.
|
|
1736
|
+
For example myrequests.json:
|
|
1737
|
+
|
|
1738
|
+
```
|
|
1739
|
+
{"key":"request_1", "request": {"contents": [{"parts": [{"text":
|
|
1740
|
+
"Explain how AI works in a few words"}]}], "generation_config": {"response_modalities": ["TEXT"]}}}
|
|
1741
|
+
{"key":"request_2", "request": {"contents": [{"parts": [{"text": "Explain how Crypto works in a few words"}]}]}}
|
|
1742
|
+
```
|
|
1743
|
+
Then upload the file.
|
|
1744
|
+
|
|
1745
|
+
```python
|
|
1746
|
+
# Upload the file
|
|
1747
|
+
file = client.files.upload(
|
|
1748
|
+
file='myrequests.json',
|
|
1749
|
+
config=types.UploadFileConfig(display_name='test-json')
|
|
1750
|
+
)
|
|
1751
|
+
|
|
1752
|
+
# Create a batch job with file name
|
|
1753
|
+
batch_job = client.batches.create(
|
|
1754
|
+
model="gemini-2.0-flash",
|
|
1755
|
+
src="files/test-json",
|
|
1756
|
+
)
|
|
1757
|
+
```
|
|
1758
|
+
|
|
1759
|
+
|
|
1303
1760
|
```python
|
|
1304
1761
|
# Get a job by name
|
|
1305
1762
|
job = client.batches.get(name=job.name)
|
|
@@ -1376,11 +1833,32 @@ To handle errors raised by the model service, the SDK provides this [APIError](h
|
|
|
1376
1833
|
from google.genai import errors
|
|
1377
1834
|
|
|
1378
1835
|
try:
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1836
|
+
client.models.generate_content(
|
|
1837
|
+
model="invalid-model-name",
|
|
1838
|
+
contents="What is your name?",
|
|
1839
|
+
)
|
|
1383
1840
|
except errors.APIError as e:
|
|
1384
|
-
|
|
1385
|
-
|
|
1841
|
+
print(e.code) # 404
|
|
1842
|
+
print(e.message)
|
|
1843
|
+
```
|
|
1844
|
+
|
|
1845
|
+
## Extra Request Body
|
|
1846
|
+
|
|
1847
|
+
The `extra_body` field in `HttpOptions` accepts a dictionary of additional JSON
|
|
1848
|
+
properties to include in the request body. This can be used to access new or
|
|
1849
|
+
experimental backend features that are not yet formally supported in the SDK.
|
|
1850
|
+
The structure of the dictionary must match the backend API's request structure.
|
|
1851
|
+
|
|
1852
|
+
- VertexAI backend API docs: https://cloud.google.com/vertex-ai/docs/reference/rest
|
|
1853
|
+
- GeminiAPI backend API docs: https://ai.google.dev/api/rest
|
|
1854
|
+
|
|
1855
|
+
```python
|
|
1856
|
+
response = client.models.generate_content(
|
|
1857
|
+
model="gemini-2.5-pro",
|
|
1858
|
+
contents="What is the weather in Boston? and how about Sunnyvale?",
|
|
1859
|
+
config=types.GenerateContentConfig(
|
|
1860
|
+
tools=[get_current_weather],
|
|
1861
|
+
http_options=types.HttpOptions(extra_body={'tool_config': {'function_calling_config': {'mode': 'COMPOSITIONAL'}}}),
|
|
1862
|
+
),
|
|
1863
|
+
)
|
|
1386
1864
|
```
|