google-genai 1.4.0__py3-none-any.whl → 1.5.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.
@@ -179,8 +179,8 @@ def t_models_url(api_client: _api_client.BaseApiClient, base_models: bool) -> st
179
179
 
180
180
 
181
181
  def t_extract_models(
182
- api_client: _api_client.BaseApiClient, response: dict
183
- ) -> list[types.Model]:
182
+ api_client: _api_client.BaseApiClient, response: dict[str, list[types.ModelDict]]
183
+ ) -> Optional[list[types.ModelDict]]:
184
184
  if not response:
185
185
  return []
186
186
  elif response.get('models') is not None:
@@ -241,7 +241,7 @@ def pil_to_blob(img) -> types.Blob:
241
241
 
242
242
 
243
243
  def t_part(
244
- client: _api_client.BaseApiClient, part: Optional[types.PartUnionDict]
244
+ part: Optional[types.PartUnionDict]
245
245
  ) -> types.Part:
246
246
  try:
247
247
  import PIL.Image
@@ -268,22 +268,21 @@ def t_part(
268
268
 
269
269
 
270
270
  def t_parts(
271
- client: _api_client.BaseApiClient,
272
271
  parts: Optional[Union[list[types.PartUnionDict], types.PartUnionDict]],
273
272
  ) -> list[types.Part]:
274
273
  #
275
274
  if parts is None or (isinstance(parts, list) and not parts):
276
275
  raise ValueError('content parts are required.')
277
276
  if isinstance(parts, list):
278
- return [t_part(client, part) for part in parts]
277
+ return [t_part(part) for part in parts]
279
278
  else:
280
- return [t_part(client, parts)]
279
+ return [t_part(parts)]
281
280
 
282
281
 
283
282
  def t_image_predictions(
284
283
  client: _api_client.BaseApiClient,
285
284
  predictions: Optional[Iterable[Mapping[str, Any]]],
286
- ) -> list[types.GeneratedImage]:
285
+ ) -> Optional[list[types.GeneratedImage]]:
287
286
  if not predictions:
288
287
  return None
289
288
  images = []
@@ -408,7 +407,7 @@ def t_contents(
408
407
  accumulated_parts: list[types.Part],
409
408
  current_part: types.PartUnionDict,
410
409
  ):
411
- current_part = t_part(client, current_part)
410
+ current_part = t_part(current_part)
412
411
  if _is_user_part(current_part) == _are_user_parts(accumulated_parts):
413
412
  accumulated_parts.append(current_part)
414
413
  else:
@@ -507,7 +506,7 @@ def handle_null_fields(schema: dict[str, Any]):
507
506
 
508
507
  def process_schema(
509
508
  schema: dict[str, Any],
510
- client: Optional[_api_client.BaseApiClient] = None,
509
+ client: _api_client.BaseApiClient,
511
510
  defs: Optional[dict[str, Any]] = None,
512
511
  *,
513
512
  order_properties: bool = True,
@@ -570,7 +569,7 @@ def process_schema(
570
569
  'type': 'array'
571
570
  }
572
571
  """
573
- if client and not client.vertexai:
572
+ if not client.vertexai:
574
573
  schema.pop('title', None)
575
574
 
576
575
  if schema.get('default') is not None:
@@ -596,15 +595,16 @@ def process_schema(
596
595
  # After removing null fields, Optional fields with only one possible type
597
596
  # will have a $ref key that needs to be flattened
598
597
  # For example: {'default': None, 'description': 'Name of the person', 'nullable': True, '$ref': '#/$defs/TestPerson'}
599
- if schema.get('$ref', None):
600
- ref = defs[schema.get('$ref').split('defs/')[-1]]
598
+ schema_ref = schema.get('$ref', None)
599
+ if schema_ref is not None:
600
+ ref = defs[schema_ref.split('defs/')[-1]]
601
601
  for schema_key in list(ref.keys()):
602
602
  schema[schema_key] = ref[schema_key]
603
603
  del schema['$ref']
604
604
 
605
605
  any_of = schema.get('anyOf', None)
606
606
  if any_of is not None:
607
- if not client.vertexai:
607
+ if client and not client.vertexai:
608
608
  raise ValueError(
609
609
  'AnyOf is not supported in the response schema for the Gemini API.'
610
610
  )
@@ -750,7 +750,10 @@ def t_speech_config(
750
750
  if (
751
751
  isinstance(origin, dict)
752
752
  and 'voice_config' in origin
753
+ and origin['voice_config'] is not None
753
754
  and 'prebuilt_voice_config' in origin['voice_config']
755
+ and origin['voice_config']['prebuilt_voice_config'] is not None
756
+ and 'voice_name' in origin['voice_config']['prebuilt_voice_config']
754
757
  ):
755
758
  return types.SpeechConfig(
756
759
  voice_config=types.VoiceConfig(
@@ -764,7 +767,7 @@ def t_speech_config(
764
767
  raise ValueError(f'Unsupported speechConfig type: {type(origin)}')
765
768
 
766
769
 
767
- def t_tool(client: _api_client.BaseApiClient, origin) -> types.Tool:
770
+ def t_tool(client: _api_client.BaseApiClient, origin) -> Optional[types.Tool]:
768
771
  if not origin:
769
772
  return None
770
773
  if inspect.isfunction(origin) or inspect.ismethod(origin):
@@ -790,12 +793,13 @@ def t_tools(
790
793
  for tool in origin:
791
794
  transformed_tool = t_tool(client, tool)
792
795
  # All functions should be merged into one tool.
793
- if transformed_tool.function_declarations:
794
- function_tool.function_declarations += (
795
- transformed_tool.function_declarations
796
- )
797
- else:
798
- tools.append(transformed_tool)
796
+ if transformed_tool is not None:
797
+ if transformed_tool.function_declarations:
798
+ function_tool.function_declarations += (
799
+ transformed_tool.function_declarations
800
+ )
801
+ else:
802
+ tools.append(transformed_tool)
799
803
  if function_tool.function_declarations:
800
804
  tools.append(function_tool)
801
805
  return tools
@@ -883,15 +887,25 @@ def t_resolve_operation(api_client: _api_client.BaseApiClient, struct: dict):
883
887
 
884
888
 
885
889
  def t_file_name(
886
- api_client: _api_client.BaseApiClient, name: Optional[Union[str, types.File]]
890
+ api_client: _api_client.BaseApiClient,
891
+ name: Optional[Union[str, types.File, types.Video, types.GeneratedVideo]],
887
892
  ):
888
893
  # Remove the files/ prefix since it's added to the url path.
889
894
  if isinstance(name, types.File):
890
895
  name = name.name
896
+ elif isinstance(name, types.Video):
897
+ name = name.uri
898
+ elif isinstance(name, types.GeneratedVideo):
899
+ name = name.video.uri
891
900
 
892
901
  if name is None:
893
902
  raise ValueError('File name is required.')
894
903
 
904
+ if not isinstance(name, str):
905
+ raise ValueError(
906
+ f'Could not convert object of type `{type(name)}` to a file name.'
907
+ )
908
+
895
909
  if name.startswith('https://'):
896
910
  suffix = name.split('files/')[1]
897
911
  match = re.match('[a-z0-9]+', suffix)
@@ -906,16 +920,19 @@ def t_file_name(
906
920
 
907
921
  def t_tuning_job_status(
908
922
  api_client: _api_client.BaseApiClient, status: str
909
- ) -> types.JobState:
923
+ ) -> Union[types.JobState, str]:
910
924
  if status == 'STATE_UNSPECIFIED':
911
- return 'JOB_STATE_UNSPECIFIED'
925
+ return types.JobState.JOB_STATE_UNSPECIFIED
912
926
  elif status == 'CREATING':
913
- return 'JOB_STATE_RUNNING'
927
+ return types.JobState.JOB_STATE_RUNNING
914
928
  elif status == 'ACTIVE':
915
- return 'JOB_STATE_SUCCEEDED'
929
+ return types.JobState.JOB_STATE_SUCCEEDED
916
930
  elif status == 'FAILED':
917
- return 'JOB_STATE_FAILED'
931
+ return types.JobState.JOB_STATE_FAILED
918
932
  else:
933
+ for state in types.JobState:
934
+ if str(state.value) == status:
935
+ return state
919
936
  return status
920
937
 
921
938
 
google/genai/batches.py CHANGED
@@ -668,7 +668,7 @@ class Batches(_api_module.BaseModule):
668
668
  http_options: Optional[types.HttpOptionsOrDict] = None
669
669
  if isinstance(config, dict):
670
670
  http_options = config.get('http_options', None)
671
- elif hasattr(config, 'http_options'):
671
+ elif hasattr(config, 'http_options') and config is not None:
672
672
  http_options = config.http_options
673
673
 
674
674
  request_dict = _common.convert_to_dict(request_dict)
@@ -737,7 +737,7 @@ class Batches(_api_module.BaseModule):
737
737
  http_options: Optional[types.HttpOptionsOrDict] = None
738
738
  if isinstance(config, dict):
739
739
  http_options = config.get('http_options', None)
740
- elif hasattr(config, 'http_options'):
740
+ elif hasattr(config, 'http_options') and config is not None:
741
741
  http_options = config.http_options
742
742
 
743
743
  request_dict = _common.convert_to_dict(request_dict)
@@ -807,7 +807,7 @@ class Batches(_api_module.BaseModule):
807
807
  http_options: Optional[types.HttpOptionsOrDict] = None
808
808
  if isinstance(config, dict):
809
809
  http_options = config.get('http_options', None)
810
- elif hasattr(config, 'http_options'):
810
+ elif hasattr(config, 'http_options') and config is not None:
811
811
  http_options = config.http_options
812
812
 
813
813
  request_dict = _common.convert_to_dict(request_dict)
@@ -846,7 +846,7 @@ class Batches(_api_module.BaseModule):
846
846
  http_options: Optional[types.HttpOptionsOrDict] = None
847
847
  if isinstance(config, dict):
848
848
  http_options = config.get('http_options', None)
849
- elif hasattr(config, 'http_options'):
849
+ elif hasattr(config, 'http_options') and config is not None:
850
850
  http_options = config.http_options
851
851
 
852
852
  request_dict = _common.convert_to_dict(request_dict)
@@ -921,7 +921,7 @@ class Batches(_api_module.BaseModule):
921
921
  http_options: Optional[types.HttpOptionsOrDict] = None
922
922
  if isinstance(config, dict):
923
923
  http_options = config.get('http_options', None)
924
- elif hasattr(config, 'http_options'):
924
+ elif hasattr(config, 'http_options') and config is not None:
925
925
  http_options = config.http_options
926
926
 
927
927
  request_dict = _common.convert_to_dict(request_dict)
@@ -1043,7 +1043,7 @@ class AsyncBatches(_api_module.BaseModule):
1043
1043
  http_options: Optional[types.HttpOptionsOrDict] = None
1044
1044
  if isinstance(config, dict):
1045
1045
  http_options = config.get('http_options', None)
1046
- elif hasattr(config, 'http_options'):
1046
+ elif hasattr(config, 'http_options') and config is not None:
1047
1047
  http_options = config.http_options
1048
1048
 
1049
1049
  request_dict = _common.convert_to_dict(request_dict)
@@ -1112,7 +1112,7 @@ class AsyncBatches(_api_module.BaseModule):
1112
1112
  http_options: Optional[types.HttpOptionsOrDict] = None
1113
1113
  if isinstance(config, dict):
1114
1114
  http_options = config.get('http_options', None)
1115
- elif hasattr(config, 'http_options'):
1115
+ elif hasattr(config, 'http_options') and config is not None:
1116
1116
  http_options = config.http_options
1117
1117
 
1118
1118
  request_dict = _common.convert_to_dict(request_dict)
@@ -1182,7 +1182,7 @@ class AsyncBatches(_api_module.BaseModule):
1182
1182
  http_options: Optional[types.HttpOptionsOrDict] = None
1183
1183
  if isinstance(config, dict):
1184
1184
  http_options = config.get('http_options', None)
1185
- elif hasattr(config, 'http_options'):
1185
+ elif hasattr(config, 'http_options') and config is not None:
1186
1186
  http_options = config.http_options
1187
1187
 
1188
1188
  request_dict = _common.convert_to_dict(request_dict)
@@ -1221,7 +1221,7 @@ class AsyncBatches(_api_module.BaseModule):
1221
1221
  http_options: Optional[types.HttpOptionsOrDict] = None
1222
1222
  if isinstance(config, dict):
1223
1223
  http_options = config.get('http_options', None)
1224
- elif hasattr(config, 'http_options'):
1224
+ elif hasattr(config, 'http_options') and config is not None:
1225
1225
  http_options = config.http_options
1226
1226
 
1227
1227
  request_dict = _common.convert_to_dict(request_dict)
@@ -1296,7 +1296,7 @@ class AsyncBatches(_api_module.BaseModule):
1296
1296
  http_options: Optional[types.HttpOptionsOrDict] = None
1297
1297
  if isinstance(config, dict):
1298
1298
  http_options = config.get('http_options', None)
1299
- elif hasattr(config, 'http_options'):
1299
+ elif hasattr(config, 'http_options') and config is not None:
1300
1300
  http_options = config.http_options
1301
1301
 
1302
1302
  request_dict = _common.convert_to_dict(request_dict)
google/genai/caches.py CHANGED
@@ -1231,7 +1231,7 @@ class Caches(_api_module.BaseModule):
1231
1231
  http_options: Optional[types.HttpOptionsOrDict] = None
1232
1232
  if isinstance(config, dict):
1233
1233
  http_options = config.get('http_options', None)
1234
- elif hasattr(config, 'http_options'):
1234
+ elif hasattr(config, 'http_options') and config is not None:
1235
1235
  http_options = config.http_options
1236
1236
 
1237
1237
  request_dict = _common.convert_to_dict(request_dict)
@@ -1301,7 +1301,7 @@ class Caches(_api_module.BaseModule):
1301
1301
  http_options: Optional[types.HttpOptionsOrDict] = None
1302
1302
  if isinstance(config, dict):
1303
1303
  http_options = config.get('http_options', None)
1304
- elif hasattr(config, 'http_options'):
1304
+ elif hasattr(config, 'http_options') and config is not None:
1305
1305
  http_options = config.http_options
1306
1306
 
1307
1307
  request_dict = _common.convert_to_dict(request_dict)
@@ -1373,7 +1373,7 @@ class Caches(_api_module.BaseModule):
1373
1373
  http_options: Optional[types.HttpOptionsOrDict] = None
1374
1374
  if isinstance(config, dict):
1375
1375
  http_options = config.get('http_options', None)
1376
- elif hasattr(config, 'http_options'):
1376
+ elif hasattr(config, 'http_options') and config is not None:
1377
1377
  http_options = config.http_options
1378
1378
 
1379
1379
  request_dict = _common.convert_to_dict(request_dict)
@@ -1450,7 +1450,7 @@ class Caches(_api_module.BaseModule):
1450
1450
  http_options: Optional[types.HttpOptionsOrDict] = None
1451
1451
  if isinstance(config, dict):
1452
1452
  http_options = config.get('http_options', None)
1453
- elif hasattr(config, 'http_options'):
1453
+ elif hasattr(config, 'http_options') and config is not None:
1454
1454
  http_options = config.http_options
1455
1455
 
1456
1456
  request_dict = _common.convert_to_dict(request_dict)
@@ -1518,7 +1518,7 @@ class Caches(_api_module.BaseModule):
1518
1518
  http_options: Optional[types.HttpOptionsOrDict] = None
1519
1519
  if isinstance(config, dict):
1520
1520
  http_options = config.get('http_options', None)
1521
- elif hasattr(config, 'http_options'):
1521
+ elif hasattr(config, 'http_options') and config is not None:
1522
1522
  http_options = config.http_options
1523
1523
 
1524
1524
  request_dict = _common.convert_to_dict(request_dict)
@@ -1617,7 +1617,7 @@ class AsyncCaches(_api_module.BaseModule):
1617
1617
  http_options: Optional[types.HttpOptionsOrDict] = None
1618
1618
  if isinstance(config, dict):
1619
1619
  http_options = config.get('http_options', None)
1620
- elif hasattr(config, 'http_options'):
1620
+ elif hasattr(config, 'http_options') and config is not None:
1621
1621
  http_options = config.http_options
1622
1622
 
1623
1623
  request_dict = _common.convert_to_dict(request_dict)
@@ -1688,7 +1688,7 @@ class AsyncCaches(_api_module.BaseModule):
1688
1688
  http_options: Optional[types.HttpOptionsOrDict] = None
1689
1689
  if isinstance(config, dict):
1690
1690
  http_options = config.get('http_options', None)
1691
- elif hasattr(config, 'http_options'):
1691
+ elif hasattr(config, 'http_options') and config is not None:
1692
1692
  http_options = config.http_options
1693
1693
 
1694
1694
  request_dict = _common.convert_to_dict(request_dict)
@@ -1761,7 +1761,7 @@ class AsyncCaches(_api_module.BaseModule):
1761
1761
  http_options: Optional[types.HttpOptionsOrDict] = None
1762
1762
  if isinstance(config, dict):
1763
1763
  http_options = config.get('http_options', None)
1764
- elif hasattr(config, 'http_options'):
1764
+ elif hasattr(config, 'http_options') and config is not None:
1765
1765
  http_options = config.http_options
1766
1766
 
1767
1767
  request_dict = _common.convert_to_dict(request_dict)
@@ -1838,7 +1838,7 @@ class AsyncCaches(_api_module.BaseModule):
1838
1838
  http_options: Optional[types.HttpOptionsOrDict] = None
1839
1839
  if isinstance(config, dict):
1840
1840
  http_options = config.get('http_options', None)
1841
- elif hasattr(config, 'http_options'):
1841
+ elif hasattr(config, 'http_options') and config is not None:
1842
1842
  http_options = config.http_options
1843
1843
 
1844
1844
  request_dict = _common.convert_to_dict(request_dict)
@@ -1906,7 +1906,7 @@ class AsyncCaches(_api_module.BaseModule):
1906
1906
  http_options: Optional[types.HttpOptionsOrDict] = None
1907
1907
  if isinstance(config, dict):
1908
1908
  http_options = config.get('http_options', None)
1909
- elif hasattr(config, 'http_options'):
1909
+ elif hasattr(config, 'http_options') and config is not None:
1910
1910
  http_options = config.http_options
1911
1911
 
1912
1912
  request_dict = _common.convert_to_dict(request_dict)
google/genai/files.py CHANGED
@@ -583,7 +583,7 @@ class Files(_api_module.BaseModule):
583
583
  http_options: Optional[types.HttpOptionsOrDict] = None
584
584
  if isinstance(config, dict):
585
585
  http_options = config.get('http_options', None)
586
- elif hasattr(config, 'http_options'):
586
+ elif hasattr(config, 'http_options') and config is not None:
587
587
  http_options = config.http_options
588
588
 
589
589
  request_dict = _common.convert_to_dict(request_dict)
@@ -641,7 +641,7 @@ class Files(_api_module.BaseModule):
641
641
  http_options: Optional[types.HttpOptionsOrDict] = None
642
642
  if isinstance(config, dict):
643
643
  http_options = config.get('http_options', None)
644
- elif hasattr(config, 'http_options'):
644
+ elif hasattr(config, 'http_options') and config is not None:
645
645
  http_options = config.http_options
646
646
 
647
647
  request_dict = _common.convert_to_dict(request_dict)
@@ -713,7 +713,7 @@ class Files(_api_module.BaseModule):
713
713
  http_options: Optional[types.HttpOptionsOrDict] = None
714
714
  if isinstance(config, dict):
715
715
  http_options = config.get('http_options', None)
716
- elif hasattr(config, 'http_options'):
716
+ elif hasattr(config, 'http_options') and config is not None:
717
717
  http_options = config.http_options
718
718
 
719
719
  request_dict = _common.convert_to_dict(request_dict)
@@ -780,7 +780,7 @@ class Files(_api_module.BaseModule):
780
780
  http_options: Optional[types.HttpOptionsOrDict] = None
781
781
  if isinstance(config, dict):
782
782
  http_options = config.get('http_options', None)
783
- elif hasattr(config, 'http_options'):
783
+ elif hasattr(config, 'http_options') and config is not None:
784
784
  http_options = config.http_options
785
785
 
786
786
  request_dict = _common.convert_to_dict(request_dict)
@@ -923,7 +923,7 @@ class Files(_api_module.BaseModule):
923
923
  def download(
924
924
  self,
925
925
  *,
926
- file: Union[str, types.File],
926
+ file: Union[str, types.File, types.Video, types.GeneratedVideo],
927
927
  config: Optional[types.DownloadFileConfigOrDict] = None,
928
928
  ) -> bytes:
929
929
  """Downloads a file's data from storage.
@@ -931,6 +931,10 @@ class Files(_api_module.BaseModule):
931
931
  Files created by `upload` can't be downloaded. You can tell which files are
932
932
  downloadable by checking the `source` or `download_uri` property.
933
933
 
934
+ Note: This method returns the data as bytes. For `Video` and
935
+ `GeneratedVideo` objects there is an additional side effect, that it also
936
+ sets the `video_bytes` property on the `Video` object.
937
+
934
938
  Args:
935
939
  file (str): A file name, uri, or file object. Identifying which file to
936
940
  download.
@@ -952,6 +956,10 @@ class Files(_api_module.BaseModule):
952
956
  data = client.files.download(file=file)
953
957
  # data = client.files.download(file=file.name)
954
958
  # data = client.files.download(file=file.download_uri)
959
+
960
+ video = types.Video(uri=file.uri)
961
+ video_bytes = client.files.download(file=video)
962
+ video.video_bytes
955
963
  """
956
964
  if self._api_client.vertexai:
957
965
  raise ValueError(
@@ -986,6 +994,11 @@ class Files(_api_module.BaseModule):
986
994
  http_options,
987
995
  )
988
996
 
997
+ if isinstance(file, types.Video):
998
+ file.video_bytes = data
999
+ elif isinstance(file, types.GeneratedVideo):
1000
+ file.video.video_bytes = data
1001
+
989
1002
  return data
990
1003
 
991
1004
 
@@ -1037,7 +1050,7 @@ class AsyncFiles(_api_module.BaseModule):
1037
1050
  http_options: Optional[types.HttpOptionsOrDict] = None
1038
1051
  if isinstance(config, dict):
1039
1052
  http_options = config.get('http_options', None)
1040
- elif hasattr(config, 'http_options'):
1053
+ elif hasattr(config, 'http_options') and config is not None:
1041
1054
  http_options = config.http_options
1042
1055
 
1043
1056
  request_dict = _common.convert_to_dict(request_dict)
@@ -1095,7 +1108,7 @@ class AsyncFiles(_api_module.BaseModule):
1095
1108
  http_options: Optional[types.HttpOptionsOrDict] = None
1096
1109
  if isinstance(config, dict):
1097
1110
  http_options = config.get('http_options', None)
1098
- elif hasattr(config, 'http_options'):
1111
+ elif hasattr(config, 'http_options') and config is not None:
1099
1112
  http_options = config.http_options
1100
1113
 
1101
1114
  request_dict = _common.convert_to_dict(request_dict)
@@ -1167,7 +1180,7 @@ class AsyncFiles(_api_module.BaseModule):
1167
1180
  http_options: Optional[types.HttpOptionsOrDict] = None
1168
1181
  if isinstance(config, dict):
1169
1182
  http_options = config.get('http_options', None)
1170
- elif hasattr(config, 'http_options'):
1183
+ elif hasattr(config, 'http_options') and config is not None:
1171
1184
  http_options = config.http_options
1172
1185
 
1173
1186
  request_dict = _common.convert_to_dict(request_dict)
@@ -1234,7 +1247,7 @@ class AsyncFiles(_api_module.BaseModule):
1234
1247
  http_options: Optional[types.HttpOptionsOrDict] = None
1235
1248
  if isinstance(config, dict):
1236
1249
  http_options = config.get('http_options', None)
1237
- elif hasattr(config, 'http_options'):
1250
+ elif hasattr(config, 'http_options') and config is not None:
1238
1251
  http_options = config.http_options
1239
1252
 
1240
1253
  request_dict = _common.convert_to_dict(request_dict)