google-genai 1.33.0__py3-none-any.whl → 1.53.0__py3-none-any.whl

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.
@@ -1,35 +1,37 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: google-genai
3
- Version: 1.33.0
3
+ Version: 1.53.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.9
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
- Requires-Dist: google-auth<3.0.0,>=2.14.1
23
+ Requires-Dist: google-auth[requests]<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.0.0
25
+ Requires-Dist: pydantic<3.0.0,>=2.9.0
27
26
  Requires-Dist: requests<3.0.0,>=2.28.1
28
27
  Requires-Dist: tenacity<9.2.0,>=8.2.3
29
28
  Requires-Dist: websockets<15.1.0,>=13.0.0
30
29
  Requires-Dist: typing-extensions<5.0.0,>=4.11.0
31
30
  Provides-Extra: aiohttp
32
- Requires-Dist: aiohttp<4.0.0; extra == "aiohttp"
31
+ Requires-Dist: aiohttp<3.13.3; extra == "aiohttp"
32
+ Provides-Extra: local-tokenizer
33
+ Requires-Dist: sentencepiece>=0.2.0; extra == "local-tokenizer"
34
+ Requires-Dist: protobuf; extra == "local-tokenizer"
33
35
  Dynamic: license-file
34
36
 
35
37
  # Google Gen AI SDK
@@ -49,12 +51,28 @@ Google's generative models into their Python applications. It supports the
49
51
  [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview)
50
52
  APIs.
51
53
 
54
+ ## Code Generation
55
+
56
+ Generative models are often unaware of recent API and SDK updates and may suggest outdated or legacy code.
57
+
58
+ We recommend using our Code Generation instructions [codegen_instructions.md](https://raw.githubusercontent.com/googleapis/python-genai/refs/heads/main/codegen_instructions.md) when generating Google Gen AI SDK code to guide your model towards using the more recent SDK features.
59
+
60
+ Copy and paste the instructions from [this file](https://raw.githubusercontent.com/googleapis/python-genai/refs/heads/main/codegen_instructions.md)
61
+ into your development environment to provide the model with the necessary
62
+ context
63
+
52
64
  ## Installation
53
65
 
54
66
  ```sh
55
67
  pip install google-genai
56
68
  ```
57
69
 
70
+ <small>With `uv`:</small>
71
+
72
+ ```sh
73
+ uv pip install google-genai
74
+ ```
75
+
58
76
  ## Imports
59
77
 
60
78
  ```python
@@ -83,6 +101,44 @@ client = genai.Client(
83
101
  )
84
102
  ```
85
103
 
104
+ ## Using types
105
+
106
+ All API methods support Pydantic types and dictionaries, which you can access
107
+ from `google.genai.types`. You can import the types module with the following:
108
+
109
+ ```python
110
+ from google.genai import types
111
+ ```
112
+
113
+ Below is an example `generate_content()` call using types from the types module:
114
+
115
+ ```python
116
+ response = client.models.generate_content(
117
+ model='gemini-2.0-flash-001',
118
+ contents=types.Part.from_text(text='Why is the sky blue?'),
119
+ config=types.GenerateContentConfig(
120
+ temperature=0,
121
+ top_p=0.95,
122
+ top_k=20,
123
+ ),
124
+ )
125
+ ```
126
+
127
+ Alternatively, you can accomplish the same request using dictionaries instead of
128
+ types:
129
+
130
+ ```python
131
+ response = client.models.generate_content(
132
+ model='gemini-2.0-flash-001',
133
+ contents={'text': 'Why is the sky blue?'},
134
+ config={
135
+ 'temperature': 0,
136
+ 'top_p': 0.95,
137
+ 'top_k': 20,
138
+ },
139
+ )
140
+ ```
141
+
86
142
  **(Optional) Using environment variables:**
87
143
 
88
144
  You can create a client by configuring the necessary environment variables.
@@ -113,6 +169,83 @@ from google import genai
113
169
  client = genai.Client()
114
170
  ```
115
171
 
172
+ ## Close a client
173
+
174
+ Explicitly close the sync client to ensure that resources, such as the
175
+ underlying HTTP connections, are properly cleaned up and closed.
176
+
177
+ ```python
178
+ from google.genai import Client
179
+
180
+ client = Client()
181
+ response_1 = client.models.generate_content(
182
+ model=MODEL_ID,
183
+ contents='Hello',
184
+ )
185
+ response_2 = client.models.generate_content(
186
+ model=MODEL_ID,
187
+ contents='Ask a question',
188
+ )
189
+ # Close the sync client to release resources.
190
+ client.close()
191
+ ```
192
+
193
+ To explicitly close the async client:
194
+
195
+ ```python
196
+ from google.genai import Client
197
+
198
+ aclient = Client(
199
+ vertexai=True, project='my-project-id', location='us-central1'
200
+ ).aio
201
+ response_1 = await aclient.models.generate_content(
202
+ model=MODEL_ID,
203
+ contents='Hello',
204
+ )
205
+ response_2 = await aclient.models.generate_content(
206
+ model=MODEL_ID,
207
+ contents='Ask a question',
208
+ )
209
+ # Close the async client to release resources.
210
+ await aclient.aclose()
211
+ ```
212
+
213
+ ## Client context managers
214
+
215
+ By using the sync client context manager, it will close the underlying
216
+ sync client when exiting the with block.
217
+
218
+ ```python
219
+ from google.genai import Client
220
+
221
+ with Client() as client:
222
+ response_1 = client.models.generate_content(
223
+ model=MODEL_ID,
224
+ contents='Hello',
225
+ )
226
+ response_2 = client.models.generate_content(
227
+ model=MODEL_ID,
228
+ contents='Ask a question',
229
+ )
230
+ ```
231
+
232
+ By using the async client context manager, it will close the underlying
233
+ async client when exiting the with block.
234
+
235
+ ```python
236
+ from google.genai import Client
237
+
238
+ async with Client().aio as aclient:
239
+ response_1 = await aclient.models.generate_content(
240
+ model=MODEL_ID,
241
+ contents='Hello',
242
+ )
243
+ response_2 = await aclient.models.generate_content(
244
+ model=MODEL_ID,
245
+ contents='Ask a question',
246
+ )
247
+ ```
248
+
116
249
  ### API Selection
117
250
 
118
251
  By default, the SDK uses the beta API endpoints provided by Google to support
@@ -155,7 +288,6 @@ Additional args of `aiohttp.ClientSession.request()` ([see _RequestOptions args]
155
288
  through the following way:
156
289
 
157
290
  ```python
158
-
159
291
  http_options = types.HttpOptions(
160
292
  async_client_args={'cookies': ..., 'ssl': ...},
161
293
  )
@@ -169,7 +301,6 @@ Both httpx and aiohttp libraries use `urllib.request.getproxies` from
169
301
  environment variables. Before client initialization, you may set proxy (and
170
302
  optional SSL_CERT_FILE) by setting the environment variables:
171
303
 
172
-
173
304
  ```bash
174
305
  export HTTPS_PROXY='http://username:password@proxy_uri:port'
175
306
  export SSL_CERT_FILE='client.pem'
@@ -180,7 +311,6 @@ args to `httpx.Client()`. You may install `httpx[socks]` to use it.
180
311
  Then, you can pass it through the following way:
181
312
 
182
313
  ```python
183
-
184
314
  http_options = types.HttpOptions(
185
315
  client_args={'proxy': 'socks5://user:pass@host:port'},
186
316
  async_client_args={'proxy': 'socks5://user:pass@host:port'},
@@ -189,6 +319,23 @@ http_options = types.HttpOptions(
189
319
  client=Client(..., http_options=http_options)
190
320
  ```
191
321
 
322
+ ### Custom base url
323
+
324
+ In some cases you might need a custom base url (for example, API gateway proxy
325
+ server) and bypass some authentication checks for project, location, or API key.
326
+ You may pass the custom base url like this:
327
+
328
+ ```python
329
+ base_url = 'https://test-api-gateway-proxy.com'
330
+ client = Client(
331
+ vertexai=True, # Currently only vertexai=True is supported
332
+ http_options={
333
+ 'base_url': base_url,
334
+ 'headers': {'Authorization': 'Bearer test_token'},
335
+ },
336
+ )
337
+ ```
338
+
192
339
  ## Types
193
340
 
194
341
  Parameter types can be specified as either dictionaries(`TypedDict`) or
@@ -202,15 +349,37 @@ See the 'Create a client' section above to initialize a client.
202
349
 
203
350
  ### Generate Content
204
351
 
205
- #### with text content
352
+ #### with text content input (text output)
206
353
 
207
354
  ```python
208
355
  response = client.models.generate_content(
209
- model='gemini-2.0-flash-001', contents='Why is the sky blue?'
356
+ model='gemini-2.5-flash', contents='Why is the sky blue?'
210
357
  )
211
358
  print(response.text)
212
359
  ```
213
360
 
361
+ #### with text content input (image output)
362
+
363
+ ```python
364
+ from google.genai import types
365
+
366
+ response = client.models.generate_content(
367
+ model='gemini-2.5-flash-image',
368
+ contents='A cartoon infographic for flying sneakers',
369
+ config=types.GenerateContentConfig(
370
+ response_modalities=["IMAGE"],
371
+ image_config=types.ImageConfig(
372
+ aspect_ratio="9:16",
373
+ ),
374
+ ),
375
+ )
376
+
377
+ for part in response.parts:
378
+ if part.inline_data:
379
+ generated_image = part.as_image()
380
+ generated_image.show()
381
+ ```
382
+
214
383
  #### with uploaded file (Gemini Developer API only)
215
384
  download the file in console.
216
385
 
@@ -223,7 +392,7 @@ python code.
223
392
  ```python
224
393
  file = client.files.upload(file='a11.txt')
225
394
  response = client.models.generate_content(
226
- model='gemini-2.0-flash-001',
395
+ model='gemini-2.5-flash',
227
396
  contents=['Could you summarize this file?', file]
228
397
  )
229
398
  print(response.text)
@@ -243,8 +412,8 @@ This is the canonical way to provide contents, SDK will not do any conversion.
243
412
  from google.genai import types
244
413
 
245
414
  contents = types.Content(
246
- role='user',
247
- parts=[types.Part.from_text(text='Why is the sky blue?')]
415
+ role='user',
416
+ parts=[types.Part.from_text(text='Why is the sky blue?')]
248
417
  )
249
418
  ```
250
419
 
@@ -252,10 +421,10 @@ SDK converts this to
252
421
 
253
422
  ```python
254
423
  [
255
- types.Content(
256
- role='user',
257
- parts=[types.Part.from_text(text='Why is the sky blue?')]
258
- )
424
+ types.Content(
425
+ role='user',
426
+ parts=[types.Part.from_text(text='Why is the sky blue?')]
427
+ )
259
428
  ]
260
429
  ```
261
430
 
@@ -269,11 +438,11 @@ The SDK will assume this is a text part, and it converts this into the following
269
438
 
270
439
  ```python
271
440
  [
272
- types.UserContent(
273
- parts=[
274
- types.Part.from_text(text='Why is the sky blue?')
275
- ]
276
- )
441
+ types.UserContent(
442
+ parts=[
443
+ types.Part.from_text(text='Why is the sky blue?')
444
+ ]
445
+ )
277
446
  ]
278
447
  ```
279
448
 
@@ -291,12 +460,12 @@ like the following:
291
460
 
292
461
  ```python
293
462
  [
294
- types.UserContent(
295
- parts=[
296
- types.Part.from_text(text='Why is the sky blue?'),
297
- types.Part.from_text(text='Why is the cloud white?'),
298
- ]
299
- )
463
+ types.UserContent(
464
+ parts=[
465
+ types.Part.from_text(text='Why is the sky blue?'),
466
+ types.Part.from_text(text='Why is the cloud white?'),
467
+ ]
468
+ )
300
469
  ]
301
470
  ```
302
471
 
@@ -309,8 +478,8 @@ Where a `types.UserContent` is a subclass of `types.Content`, the
309
478
  from google.genai import types
310
479
 
311
480
  contents = types.Part.from_function_call(
312
- name='get_weather_by_location',
313
- args={'location': 'Boston'}
481
+ name='get_weather_by_location',
482
+ args={'location': 'Boston'}
314
483
  )
315
484
  ```
316
485
 
@@ -318,14 +487,14 @@ The SDK converts a function call part to a content with a `model` role:
318
487
 
319
488
  ```python
320
489
  [
321
- types.ModelContent(
322
- parts=[
323
- types.Part.from_function_call(
324
- name='get_weather_by_location',
325
- args={'location': 'Boston'}
326
- )
327
- ]
328
- )
490
+ types.ModelContent(
491
+ parts=[
492
+ types.Part.from_function_call(
493
+ name='get_weather_by_location',
494
+ args={'location': 'Boston'}
495
+ )
496
+ ]
497
+ )
329
498
  ]
330
499
  ```
331
500
 
@@ -338,14 +507,14 @@ Where a `types.ModelContent` is a subclass of `types.Content`, the
338
507
  from google.genai import types
339
508
 
340
509
  contents = [
341
- types.Part.from_function_call(
342
- name='get_weather_by_location',
343
- args={'location': 'Boston'}
344
- ),
345
- types.Part.from_function_call(
346
- name='get_weather_by_location',
347
- args={'location': 'New York'}
348
- ),
510
+ types.Part.from_function_call(
511
+ name='get_weather_by_location',
512
+ args={'location': 'Boston'}
513
+ ),
514
+ types.Part.from_function_call(
515
+ name='get_weather_by_location',
516
+ args={'location': 'New York'}
517
+ ),
349
518
  ]
350
519
  ```
351
520
 
@@ -353,18 +522,18 @@ The SDK converts a list of function call parts to the a content with a `model` r
353
522
 
354
523
  ```python
355
524
  [
356
- types.ModelContent(
357
- parts=[
358
- types.Part.from_function_call(
359
- name='get_weather_by_location',
360
- args={'location': 'Boston'}
361
- ),
362
- types.Part.from_function_call(
363
- name='get_weather_by_location',
364
- args={'location': 'New York'}
365
- )
366
- ]
367
- )
525
+ types.ModelContent(
526
+ parts=[
527
+ types.Part.from_function_call(
528
+ name='get_weather_by_location',
529
+ args={'location': 'Boston'}
530
+ ),
531
+ types.Part.from_function_call(
532
+ name='get_weather_by_location',
533
+ args={'location': 'New York'}
534
+ )
535
+ ]
536
+ )
368
537
  ]
369
538
  ```
370
539
 
@@ -377,8 +546,8 @@ Where a `types.ModelContent` is a subclass of `types.Content`, the
377
546
  from google.genai import types
378
547
 
379
548
  contents = types.Part.from_uri(
380
- file_uri: 'gs://generativeai-downloads/images/scones.jpg',
381
- mime_type: 'image/jpeg',
549
+ file_uri: 'gs://generativeai-downloads/images/scones.jpg',
550
+ mime_type: 'image/jpeg',
382
551
  )
383
552
  ```
384
553
 
@@ -386,12 +555,12 @@ The SDK converts all non function call parts into a content with a `user` role.
386
555
 
387
556
  ```python
388
557
  [
389
- types.UserContent(parts=[
390
- types.Part.from_uri(
391
- file_uri: 'gs://generativeai-downloads/images/scones.jpg',
392
- mime_type: 'image/jpeg',
393
- )
394
- ])
558
+ types.UserContent(parts=[
559
+ types.Part.from_uri(
560
+ file_uri: 'gs://generativeai-downloads/images/scones.jpg',
561
+ mime_type: 'image/jpeg',
562
+ )
563
+ ])
395
564
  ]
396
565
  ```
397
566
 
@@ -401,11 +570,11 @@ The SDK converts all non function call parts into a content with a `user` role.
401
570
  from google.genai import types
402
571
 
403
572
  contents = [
404
- types.Part.from_text('What is this image about?'),
405
- types.Part.from_uri(
406
- file_uri: 'gs://generativeai-downloads/images/scones.jpg',
407
- mime_type: 'image/jpeg',
408
- )
573
+ types.Part.from_text('What is this image about?'),
574
+ types.Part.from_uri(
575
+ file_uri: 'gs://generativeai-downloads/images/scones.jpg',
576
+ mime_type: 'image/jpeg',
577
+ )
409
578
  ]
410
579
  ```
411
580
 
@@ -413,15 +582,15 @@ The SDK will convert the list of parts into a content with a `user` role
413
582
 
414
583
  ```python
415
584
  [
416
- types.UserContent(
417
- parts=[
418
- types.Part.from_text('What is this image about?'),
419
- types.Part.from_uri(
420
- file_uri: 'gs://generativeai-downloads/images/scones.jpg',
421
- mime_type: 'image/jpeg',
422
- )
423
- ]
424
- )
585
+ types.UserContent(
586
+ parts=[
587
+ types.Part.from_text('What is this image about?'),
588
+ types.Part.from_uri(
589
+ file_uri: 'gs://generativeai-downloads/images/scones.jpg',
590
+ mime_type: 'image/jpeg',
591
+ )
592
+ ]
593
+ )
425
594
  ]
426
595
  ```
427
596
 
@@ -462,33 +631,6 @@ response = client.models.generate_content(
462
631
  print(response.text)
463
632
  ```
464
633
 
465
- ### Typed Config
466
-
467
- All API methods support Pydantic types for parameters as well as
468
- dictionaries. You can get the type from `google.genai.types`.
469
-
470
- ```python
471
- from google.genai import types
472
-
473
- response = client.models.generate_content(
474
- model='gemini-2.0-flash-001',
475
- contents=types.Part.from_text(text='Why is the sky blue?'),
476
- config=types.GenerateContentConfig(
477
- temperature=0,
478
- top_p=0.95,
479
- top_k=20,
480
- candidate_count=1,
481
- seed=5,
482
- max_output_tokens=100,
483
- stop_sequences=['STOP!'],
484
- presence_penalty=0.0,
485
- frequency_penalty=0.0,
486
- ),
487
- )
488
-
489
- print(response.text)
490
- ```
491
-
492
634
  ### List Base Models
493
635
 
494
636
  To retrieve tuned models, see [list tuned models](#list-tuned-models).
@@ -527,7 +669,7 @@ print(async_pager[0])
527
669
  from google.genai import types
528
670
 
529
671
  response = client.models.generate_content(
530
- model='gemini-2.0-flash-001',
672
+ model='gemini-2.5-flash',
531
673
  contents='Say something bad.',
532
674
  config=types.GenerateContentConfig(
533
675
  safety_settings=[
@@ -555,13 +697,13 @@ def get_current_weather(location: str) -> str:
555
697
  """Returns the current weather.
556
698
 
557
699
  Args:
558
- location: The city and state, e.g. San Francisco, CA
700
+ location: The city and state, e.g. San Francisco, CA
559
701
  """
560
702
  return 'sunny'
561
703
 
562
704
 
563
705
  response = client.models.generate_content(
564
- model='gemini-2.0-flash-001',
706
+ model='gemini-2.5-flash',
565
707
  contents='What is the weather like in Boston?',
566
708
  config=types.GenerateContentConfig(tools=[get_current_weather]),
567
709
  )
@@ -577,14 +719,14 @@ as follows:
577
719
  from google.genai import types
578
720
 
579
721
  response = client.models.generate_content(
580
- model='gemini-2.0-flash-001',
581
- contents='What is the weather like in Boston?',
582
- config=types.GenerateContentConfig(
583
- tools=[get_current_weather],
584
- automatic_function_calling=types.AutomaticFunctionCallingConfig(
585
- disable=True
722
+ model='gemini-2.5-flash',
723
+ contents='What is the weather like in Boston?',
724
+ config=types.GenerateContentConfig(
725
+ tools=[get_current_weather],
726
+ automatic_function_calling=types.AutomaticFunctionCallingConfig(
727
+ disable=True
728
+ ),
586
729
  ),
587
- ),
588
730
  )
589
731
  ```
590
732
 
@@ -624,7 +766,7 @@ function = types.FunctionDeclaration(
624
766
  tool = types.Tool(function_declarations=[function])
625
767
 
626
768
  response = client.models.generate_content(
627
- model='gemini-2.0-flash-001',
769
+ model='gemini-2.5-flash',
628
770
  contents='What is the weather like in Boston?',
629
771
  config=types.GenerateContentConfig(tools=[tool]),
630
772
  )
@@ -668,7 +810,7 @@ function_response_content = types.Content(
668
810
  )
669
811
 
670
812
  response = client.models.generate_content(
671
- model='gemini-2.0-flash-001',
813
+ model='gemini-2.5-flash',
672
814
  contents=[
673
815
  user_prompt_content,
674
816
  function_call_content,
@@ -698,12 +840,12 @@ def get_current_weather(location: str) -> str:
698
840
  """Returns the current weather.
699
841
 
700
842
  Args:
701
- location: The city and state, e.g. San Francisco, CA
843
+ location: The city and state, e.g. San Francisco, CA
702
844
  """
703
845
  return "sunny"
704
846
 
705
847
  response = client.models.generate_content(
706
- model="gemini-2.0-flash-001",
848
+ model="gemini-2.5-flash",
707
849
  contents="What is the weather like in Boston?",
708
850
  config=types.GenerateContentConfig(
709
851
  tools=[get_current_weather],
@@ -728,12 +870,12 @@ def get_current_weather(location: str) -> str:
728
870
  """Returns the current weather.
729
871
 
730
872
  Args:
731
- location: The city and state, e.g. San Francisco, CA
873
+ location: The city and state, e.g. San Francisco, CA
732
874
  """
733
875
  return "sunny"
734
876
 
735
877
  response = client.models.generate_content(
736
- model="gemini-2.0-flash-001",
878
+ model="gemini-2.5-flash",
737
879
  contents="What is the weather like in Boston?",
738
880
  config=types.GenerateContentConfig(
739
881
  tools=[get_current_weather],
@@ -823,7 +965,7 @@ user_profile = {
823
965
  }
824
966
 
825
967
  response = client.models.generate_content(
826
- model='gemini-2.0-flash',
968
+ model='gemini-2.5-flash',
827
969
  contents='Give me a random user profile.',
828
970
  config={
829
971
  'response_mime_type': 'application/json',
@@ -853,7 +995,7 @@ class CountryInfo(BaseModel):
853
995
 
854
996
 
855
997
  response = client.models.generate_content(
856
- model='gemini-2.0-flash-001',
998
+ model='gemini-2.5-flash',
857
999
  contents='Give me information for the United States.',
858
1000
  config=types.GenerateContentConfig(
859
1001
  response_mime_type='application/json',
@@ -867,7 +1009,7 @@ print(response.text)
867
1009
  from google.genai import types
868
1010
 
869
1011
  response = client.models.generate_content(
870
- model='gemini-2.0-flash-001',
1012
+ model='gemini-2.5-flash',
871
1013
  contents='Give me information for the United States.',
872
1014
  config=types.GenerateContentConfig(
873
1015
  response_mime_type='application/json',
@@ -905,21 +1047,23 @@ You can set response_mime_type to 'text/x.enum' to return one of those enum
905
1047
  values as the response.
906
1048
 
907
1049
  ```python
1050
+ from enum import Enum
1051
+
908
1052
  class InstrumentEnum(Enum):
909
- PERCUSSION = 'Percussion'
910
- STRING = 'String'
911
- WOODWIND = 'Woodwind'
912
- BRASS = 'Brass'
913
- KEYBOARD = 'Keyboard'
1053
+ PERCUSSION = 'Percussion'
1054
+ STRING = 'String'
1055
+ WOODWIND = 'Woodwind'
1056
+ BRASS = 'Brass'
1057
+ KEYBOARD = 'Keyboard'
914
1058
 
915
1059
  response = client.models.generate_content(
916
- model='gemini-2.0-flash-001',
917
- contents='What instrument plays multiple notes at once?',
918
- config={
919
- 'response_mime_type': 'text/x.enum',
920
- 'response_schema': InstrumentEnum,
921
- },
922
- )
1060
+ model='gemini-2.5-flash',
1061
+ contents='What instrument plays multiple notes at once?',
1062
+ config={
1063
+ 'response_mime_type': 'text/x.enum',
1064
+ 'response_schema': InstrumentEnum,
1065
+ },
1066
+ )
923
1067
  print(response.text)
924
1068
  ```
925
1069
 
@@ -932,20 +1076,20 @@ identical but in quotes.
932
1076
  from enum import Enum
933
1077
 
934
1078
  class InstrumentEnum(Enum):
935
- PERCUSSION = 'Percussion'
936
- STRING = 'String'
937
- WOODWIND = 'Woodwind'
938
- BRASS = 'Brass'
939
- KEYBOARD = 'Keyboard'
1079
+ PERCUSSION = 'Percussion'
1080
+ STRING = 'String'
1081
+ WOODWIND = 'Woodwind'
1082
+ BRASS = 'Brass'
1083
+ KEYBOARD = 'Keyboard'
940
1084
 
941
1085
  response = client.models.generate_content(
942
- model='gemini-2.0-flash-001',
943
- contents='What instrument plays multiple notes at once?',
944
- config={
945
- 'response_mime_type': 'application/json',
946
- 'response_schema': InstrumentEnum,
947
- },
948
- )
1086
+ model='gemini-2.5-flash',
1087
+ contents='What instrument plays multiple notes at once?',
1088
+ config={
1089
+ 'response_mime_type': 'application/json',
1090
+ 'response_schema': InstrumentEnum,
1091
+ },
1092
+ )
949
1093
  print(response.text)
950
1094
  ```
951
1095
 
@@ -958,7 +1102,7 @@ to you, rather than being returned as one chunk.
958
1102
 
959
1103
  ```python
960
1104
  for chunk in client.models.generate_content_stream(
961
- model='gemini-2.0-flash-001', contents='Tell me a story in 300 words.'
1105
+ model='gemini-2.5-flash', contents='Tell me a story in 300 words.'
962
1106
  ):
963
1107
  print(chunk.text, end='')
964
1108
  ```
@@ -972,7 +1116,7 @@ you can use the `from_uri` class method to create a `Part` object.
972
1116
  from google.genai import types
973
1117
 
974
1118
  for chunk in client.models.generate_content_stream(
975
- model='gemini-2.0-flash-001',
1119
+ model='gemini-2.5-flash',
976
1120
  contents=[
977
1121
  'What is this image about?',
978
1122
  types.Part.from_uri(
@@ -996,7 +1140,7 @@ with open(YOUR_IMAGE_PATH, 'rb') as f:
996
1140
  image_bytes = f.read()
997
1141
 
998
1142
  for chunk in client.models.generate_content_stream(
999
- model='gemini-2.0-flash-001',
1143
+ model='gemini-2.5-flash',
1000
1144
  contents=[
1001
1145
  'What is this image about?',
1002
1146
  types.Part.from_bytes(data=image_bytes, mime_type=YOUR_IMAGE_MIME_TYPE),
@@ -1015,7 +1159,7 @@ of `client.models.generate_content`
1015
1159
 
1016
1160
  ```python
1017
1161
  response = await client.aio.models.generate_content(
1018
- model='gemini-2.0-flash-001', contents='Tell me a story in 300 words.'
1162
+ model='gemini-2.5-flash', contents='Tell me a story in 300 words.'
1019
1163
  )
1020
1164
 
1021
1165
  print(response.text)
@@ -1023,10 +1167,9 @@ print(response.text)
1023
1167
 
1024
1168
  ### Generate Content (Asynchronous Streaming)
1025
1169
 
1026
-
1027
1170
  ```python
1028
1171
  async for chunk in await client.aio.models.generate_content_stream(
1029
- model='gemini-2.0-flash-001', contents='Tell me a story in 300 words.'
1172
+ model='gemini-2.5-flash', contents='Tell me a story in 300 words.'
1030
1173
  ):
1031
1174
  print(chunk.text, end='')
1032
1175
  ```
@@ -1035,7 +1178,7 @@ async for chunk in await client.aio.models.generate_content_stream(
1035
1178
 
1036
1179
  ```python
1037
1180
  response = client.models.count_tokens(
1038
- model='gemini-2.0-flash-001',
1181
+ model='gemini-2.5-flash',
1039
1182
  contents='why is the sky blue?',
1040
1183
  )
1041
1184
  print(response)
@@ -1047,7 +1190,7 @@ Compute tokens is only supported in Vertex AI.
1047
1190
 
1048
1191
  ```python
1049
1192
  response = client.models.compute_tokens(
1050
- model='gemini-2.0-flash-001',
1193
+ model='gemini-2.5-flash',
1051
1194
  contents='why is the sky blue?',
1052
1195
  )
1053
1196
  print(response)
@@ -1057,17 +1200,31 @@ print(response)
1057
1200
 
1058
1201
  ```python
1059
1202
  response = await client.aio.models.count_tokens(
1060
- model='gemini-2.0-flash-001',
1203
+ model='gemini-2.5-flash',
1061
1204
  contents='why is the sky blue?',
1062
1205
  )
1063
1206
  print(response)
1064
1207
  ```
1065
1208
 
1209
+ #### Local Count Tokens
1210
+
1211
+ ```python
1212
+ tokenizer = genai.LocalTokenizer(model_name='gemini-2.5-flash')
1213
+ result = tokenizer.count_tokens("What is your name?")
1214
+ ```
1215
+
1216
+ #### Local Compute Tokens
1217
+
1218
+ ```python
1219
+ tokenizer = genai.LocalTokenizer(model_name='gemini-2.5-flash')
1220
+ result = tokenizer.compute_tokens("What is your name?")
1221
+ ```
1222
+
1066
1223
  ### Embed Content
1067
1224
 
1068
1225
  ```python
1069
1226
  response = client.models.embed_content(
1070
- model='text-embedding-004',
1227
+ model='gemini-embedding-001',
1071
1228
  contents='why is the sky blue?',
1072
1229
  )
1073
1230
  print(response)
@@ -1078,7 +1235,7 @@ from google.genai import types
1078
1235
 
1079
1236
  # multiple contents with config
1080
1237
  response = client.models.embed_content(
1081
- model='text-embedding-004',
1238
+ model='gemini-embedding-001',
1082
1239
  contents=['why is the sky blue?', 'What is your age?'],
1083
1240
  config=types.EmbedContentConfig(output_dimensionality=10),
1084
1241
  )
@@ -1097,7 +1254,7 @@ from google.genai import types
1097
1254
 
1098
1255
  # Generate Image
1099
1256
  response1 = client.models.generate_images(
1100
- model='imagen-3.0-generate-002',
1257
+ model='imagen-4.0-generate-001',
1101
1258
  prompt='An umbrella in the foreground, and a rainy night sky in the background',
1102
1259
  config=types.GenerateImagesConfig(
1103
1260
  number_of_images=1,
@@ -1117,7 +1274,7 @@ from google.genai import types
1117
1274
 
1118
1275
  # Upscale the generated image from above
1119
1276
  response2 = client.models.upscale_image(
1120
- model='imagen-3.0-generate-001',
1277
+ model='imagen-4.0-upscale-preview',
1121
1278
  image=response1.generated_images[0].image,
1122
1279
  upscale_factor='x2',
1123
1280
  config=types.UpscaleImageConfig(
@@ -1178,7 +1335,7 @@ from google.genai import types
1178
1335
 
1179
1336
  # Create operation
1180
1337
  operation = client.models.generate_videos(
1181
- model='veo-2.0-generate-001',
1338
+ model='veo-3.1-generate-preview',
1182
1339
  prompt='A neon hologram of a cat driving at top speed',
1183
1340
  config=types.GenerateVideosConfig(
1184
1341
  number_of_videos=1,
@@ -1206,7 +1363,7 @@ image = types.Image.from_file("local/path/file.png")
1206
1363
 
1207
1364
  # Create operation
1208
1365
  operation = client.models.generate_videos(
1209
- model='veo-2.0-generate-001',
1366
+ model='veo-3.1-generate-preview',
1210
1367
  # Prompt is optional if image is provided
1211
1368
  prompt='Night sky',
1212
1369
  image=image,
@@ -1229,7 +1386,8 @@ video.show()
1229
1386
 
1230
1387
  #### Generate Videos (Video to Video)
1231
1388
 
1232
- Currently, only Vertex supports Video to Video generation (Video extension).
1389
+ Currently, only Gemini Developer API supports video extension on Veo 3.1 for
1390
+ previously generated videos. Vertex supports video extension on Veo 2.0.
1233
1391
 
1234
1392
  ```python
1235
1393
  from google.genai import types
@@ -1239,10 +1397,10 @@ video = types.Video.from_file("local/path/video.mp4")
1239
1397
 
1240
1398
  # Create operation
1241
1399
  operation = client.models.generate_videos(
1242
- model='veo-2.0-generate-001',
1400
+ model='veo-3.1-generate-preview',
1243
1401
  # Prompt is optional if Video is provided
1244
1402
  prompt='Night sky',
1245
- # Input video must be in GCS
1403
+ # Input video must be in GCS for Vertex or a URI for Gemini
1246
1404
  video=types.Video(
1247
1405
  uri="gs://bucket-name/inputs/videos/cat_driving.mp4",
1248
1406
  ),
@@ -1272,7 +1430,7 @@ that it can reflect on its previous responses (i.e., engage in an ongoing
1272
1430
  ### Send Message (Synchronous Non-Streaming)
1273
1431
 
1274
1432
  ```python
1275
- chat = client.chats.create(model='gemini-2.0-flash-001')
1433
+ chat = client.chats.create(model='gemini-2.5-flash')
1276
1434
  response = chat.send_message('tell me a story')
1277
1435
  print(response.text)
1278
1436
  response = chat.send_message('summarize the story you told me in 1 sentence')
@@ -1282,7 +1440,7 @@ print(response.text)
1282
1440
  ### Send Message (Synchronous Streaming)
1283
1441
 
1284
1442
  ```python
1285
- chat = client.chats.create(model='gemini-2.0-flash-001')
1443
+ chat = client.chats.create(model='gemini-2.5-flash')
1286
1444
  for chunk in chat.send_message_stream('tell me a story'):
1287
1445
  print(chunk.text)
1288
1446
  ```
@@ -1290,7 +1448,7 @@ for chunk in chat.send_message_stream('tell me a story'):
1290
1448
  ### Send Message (Asynchronous Non-Streaming)
1291
1449
 
1292
1450
  ```python
1293
- chat = client.aio.chats.create(model='gemini-2.0-flash-001')
1451
+ chat = client.aio.chats.create(model='gemini-2.5-flash')
1294
1452
  response = await chat.send_message('tell me a story')
1295
1453
  print(response.text)
1296
1454
  ```
@@ -1298,7 +1456,7 @@ print(response.text)
1298
1456
  ### Send Message (Asynchronous Streaming)
1299
1457
 
1300
1458
  ```python
1301
- chat = client.aio.chats.create(model='gemini-2.0-flash-001')
1459
+ chat = client.aio.chats.create(model='gemini-2.5-flash')
1302
1460
  async for chunk in await chat.send_message_stream('tell me a story'):
1303
1461
  print(chunk.text)
1304
1462
  ```
@@ -1308,7 +1466,7 @@ async for chunk in await chat.send_message_stream('tell me a story'):
1308
1466
  Files are only supported in Gemini Developer API. See the 'Create a client'
1309
1467
  section above to initialize a client.
1310
1468
 
1311
- ```cmd
1469
+ ```sh
1312
1470
  !gsutil cp gs://cloud-samples-data/generative-ai/pdf/2312.11805v3.pdf .
1313
1471
  !gsutil cp gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf .
1314
1472
  ```
@@ -1357,7 +1515,7 @@ else:
1357
1515
  file_uris = [file1.uri, file2.uri]
1358
1516
 
1359
1517
  cached_content = client.caches.create(
1360
- model='gemini-2.0-flash-001',
1518
+ model='gemini-2.5-flash',
1361
1519
  config=types.CreateCachedContentConfig(
1362
1520
  contents=[
1363
1521
  types.Content(
@@ -1392,7 +1550,7 @@ cached_content = client.caches.get(name=cached_content.name)
1392
1550
  from google.genai import types
1393
1551
 
1394
1552
  response = client.models.generate_content(
1395
- model='gemini-2.0-flash-001',
1553
+ model='gemini-2.5-flash',
1396
1554
  contents='Summarize the pdfs',
1397
1555
  config=types.GenerateContentConfig(
1398
1556
  cached_content=cached_content.name,
@@ -1409,15 +1567,15 @@ section above to initialize a client.
1409
1567
 
1410
1568
  ### Tune
1411
1569
 
1412
- - Vertex AI supports tuning from GCS source or from a Vertex Multimodal Dataset
1570
+ - 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)
1413
1571
 
1414
1572
  ```python
1415
1573
  from google.genai import types
1416
1574
 
1417
- model = 'gemini-2.0-flash-001'
1575
+ model = 'gemini-2.5-flash'
1418
1576
  training_dataset = types.TuningDataset(
1419
- # or gcs_uri=my_vertex_multimodal_dataset
1420
- gcs_uri='gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl',
1577
+ # or gcs_uri=my_vertex_multimodal_dataset
1578
+ gcs_uri='gs://your-gcs-bucket/your-tuning-data.jsonl',
1421
1579
  )
1422
1580
  ```
1423
1581
 
@@ -1568,11 +1726,11 @@ Vertex AI:
1568
1726
  ```python
1569
1727
  # Specify model and source file only, destination and job display name will be auto-populated
1570
1728
  job = client.batches.create(
1571
- model='gemini-2.0-flash-001',
1729
+ model='gemini-2.5-flash',
1572
1730
  src='bq://my-project.my-dataset.my-table', # or "gs://path/to/input/data"
1573
1731
  )
1574
1732
 
1575
- job
1733
+ print(job)
1576
1734
  ```
1577
1735
 
1578
1736
  Gemini Developer API:
@@ -1580,22 +1738,22 @@ Gemini Developer API:
1580
1738
  ```python
1581
1739
  # Create a batch job with inlined requests
1582
1740
  batch_job = client.batches.create(
1583
- model="gemini-2.0-flash",
1741
+ model="gemini-2.5-flash",
1584
1742
  src=[{
1585
- "contents": [{
1586
- "parts": [{
1587
- "text": "Hello!",
1743
+ "contents": [{
1744
+ "parts": [{
1745
+ "text": "Hello!",
1746
+ }],
1747
+ "role": "user",
1588
1748
  }],
1589
- "role": "user",
1590
- }],
1591
- "config": {"response_modalities": ["text"]},
1749
+ "config": {"response_modalities": ["text"]},
1592
1750
  }],
1593
1751
  )
1594
1752
 
1595
1753
  job
1596
1754
  ```
1597
1755
 
1598
- In order to create a batch job with file name. Need to upload a jsonl file.
1756
+ In order to create a batch job with file name. Need to upload a json file.
1599
1757
  For example myrequests.json:
1600
1758
 
1601
1759
  ```
@@ -1608,14 +1766,14 @@ Then upload the file.
1608
1766
  ```python
1609
1767
  # Upload the file
1610
1768
  file = client.files.upload(
1611
- file='myrequest.json',
1612
- config=types.UploadFileConfig(display_name='test_json')
1769
+ file='myrequests.json',
1770
+ config=types.UploadFileConfig(display_name='test-json')
1613
1771
  )
1614
1772
 
1615
1773
  # Create a batch job with file name
1616
1774
  batch_job = client.batches.create(
1617
1775
  model="gemini-2.0-flash",
1618
- src="files/file_name",
1776
+ src="files/test-json",
1619
1777
  )
1620
1778
  ```
1621
1779
 
@@ -1696,13 +1854,13 @@ To handle errors raised by the model service, the SDK provides this [APIError](h
1696
1854
  from google.genai import errors
1697
1855
 
1698
1856
  try:
1699
- client.models.generate_content(
1700
- model="invalid-model-name",
1701
- contents="What is your name?",
1702
- )
1857
+ client.models.generate_content(
1858
+ model="invalid-model-name",
1859
+ contents="What is your name?",
1860
+ )
1703
1861
  except errors.APIError as e:
1704
- print(e.code) # 404
1705
- print(e.message)
1862
+ print(e.code) # 404
1863
+ print(e.message)
1706
1864
  ```
1707
1865
 
1708
1866
  ## Extra Request Body