google-genai 0.7.0__py3-none-any.whl → 1.0.0rc0__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.
google/genai/files.py CHANGED
@@ -19,7 +19,7 @@ import io
19
19
  import mimetypes
20
20
  import os
21
21
  import pathlib
22
- from typing import Optional, Union
22
+ from typing import Any, Optional, Union
23
23
  from urllib.parse import urlencode
24
24
  from . import _api_module
25
25
  from . import _common
@@ -351,12 +351,12 @@ def _DeleteFileParameters_to_vertex(
351
351
  return to_object
352
352
 
353
353
 
354
- def _FileState_to_vertex_enum_validate(enum_value: any):
354
+ def _FileState_to_vertex_enum_validate(enum_value: Any):
355
355
  if enum_value in set(['STATE_UNSPECIFIED', 'PROCESSING', 'ACTIVE', 'FAILED']):
356
356
  raise ValueError(f'{enum_value} enum value is not supported in Vertex AI.')
357
357
 
358
358
 
359
- def _FileSource_to_vertex_enum_validate(enum_value: any):
359
+ def _FileSource_to_vertex_enum_validate(enum_value: Any):
360
360
  if enum_value in set(['SOURCE_UNSPECIFIED', 'UPLOADED', 'GENERATED']):
361
361
  raise ValueError(f'{enum_value} enum value is not supported in Vertex AI.')
362
362
 
@@ -781,16 +781,17 @@ class Files(_api_module.BaseModule):
781
781
  def upload(
782
782
  self,
783
783
  *,
784
- path: Union[str, pathlib.Path, os.PathLike, io.IOBase],
784
+ file: Union[str, pathlib.Path, os.PathLike, io.IOBase],
785
785
  config: Optional[types.UploadFileConfigOrDict] = None,
786
786
  ) -> types.File:
787
787
  """Calls the API to upload a file using a supported file service.
788
788
 
789
789
  Args:
790
- path: The path to the file or an `IOBase` object to be uploaded. If it's
791
- an IOBase object, it must be opened in blocking mode and binary mode. In
792
- other words, do not use non-blocking mode or text mode. The given stream
793
- must be seekable, that is, it must be able to call seek() on 'path'.
790
+ file: A path to the file or an `IOBase` object to be uploaded. If it's an
791
+ IOBase object, it must be opened in blocking (the default) mode and
792
+ binary mode. In other words, do not use non-blocking mode or text mode.
793
+ The given stream must be seekable, that is, it must be able to call
794
+ `seek()` on 'path'.
794
795
  config: Optional parameters to set `diplay_name`, `mime_type`, and `name`.
795
796
  """
796
797
  if self._api_client.vertexai:
@@ -804,37 +805,37 @@ class Files(_api_module.BaseModule):
804
805
  config_model = types.UploadFileConfig(**config)
805
806
  else:
806
807
  config_model = config
807
- file = types.File(
808
+ file_obj = types.File(
808
809
  mime_type=config_model.mime_type,
809
810
  name=config_model.name,
810
811
  display_name=config_model.display_name,
811
812
  )
812
813
  else: # if not config
813
- file = types.File()
814
- if file.name is not None and not file.name.startswith('files/'):
815
- file.name = f'files/{file.name}'
814
+ file_obj = types.File()
815
+ if file_obj.name is not None and not file_obj.name.startswith('files/'):
816
+ file_obj.name = f'files/{file_obj.name}'
816
817
 
817
- if isinstance(path, io.IOBase):
818
- if file.mime_type is None:
818
+ if isinstance(file, io.IOBase):
819
+ if file_obj.mime_type is None:
819
820
  raise ValueError(
820
821
  'Unknown mime type: Could not determine the mimetype for your'
821
822
  ' file\n please set the `mime_type` argument'
822
823
  )
823
- if hasattr(path, 'mode'):
824
- if 'b' not in path.mode:
824
+ if hasattr(file, 'mode'):
825
+ if 'b' not in file.mode:
825
826
  raise ValueError('The file must be opened in binary mode.')
826
- offset = path.tell()
827
- path.seek(0, os.SEEK_END)
828
- file.size_bytes = path.tell() - offset
829
- path.seek(offset, os.SEEK_SET)
827
+ offset = file.tell()
828
+ file.seek(0, os.SEEK_END)
829
+ file_obj.size_bytes = file.tell() - offset
830
+ file.seek(offset, os.SEEK_SET)
830
831
  else:
831
- fs_path = os.fspath(path)
832
+ fs_path = os.fspath(file)
832
833
  if not fs_path or not os.path.isfile(fs_path):
833
- raise FileNotFoundError(f'{path} is not a valid file path.')
834
- file.size_bytes = os.path.getsize(fs_path)
835
- if file.mime_type is None:
836
- file.mime_type, _ = mimetypes.guess_type(fs_path)
837
- if file.mime_type is None:
834
+ raise FileNotFoundError(f'{file} is not a valid file path.')
835
+ file_obj.size_bytes = os.path.getsize(fs_path)
836
+ if file_obj.mime_type is None:
837
+ file_obj.mime_type, _ = mimetypes.guess_type(fs_path)
838
+ if file_obj.mime_type is None:
838
839
  raise ValueError(
839
840
  'Unknown mime type: Could not determine the mimetype for your'
840
841
  ' file\n please set the `mime_type` argument'
@@ -849,12 +850,12 @@ class Files(_api_module.BaseModule):
849
850
  'Content-Type': 'application/json',
850
851
  'X-Goog-Upload-Protocol': 'resumable',
851
852
  'X-Goog-Upload-Command': 'start',
852
- 'X-Goog-Upload-Header-Content-Length': f'{file.size_bytes}',
853
- 'X-Goog-Upload-Header-Content-Type': f'{file.mime_type}',
853
+ 'X-Goog-Upload-Header-Content-Length': f'{file_obj.size_bytes}',
854
+ 'X-Goog-Upload-Header-Content-Type': f'{file_obj.mime_type}',
854
855
  },
855
856
  'deprecated_response_payload': response,
856
857
  }
857
- self._create(file=file, config={'http_options': http_options})
858
+ self._create(file=file_obj, config={'http_options': http_options})
858
859
 
859
860
  if (
860
861
  'headers' not in response
@@ -866,13 +867,13 @@ class Files(_api_module.BaseModule):
866
867
  )
867
868
  upload_url = response['headers']['X-Goog-Upload-URL']
868
869
 
869
- if isinstance(path, io.IOBase):
870
+ if isinstance(file, io.IOBase):
870
871
  return_file = self._api_client.upload_file(
871
- path, upload_url, file.size_bytes
872
+ file, upload_url, file_obj.size_bytes
872
873
  )
873
874
  else:
874
875
  return_file = self._api_client.upload_file(
875
- fs_path, upload_url, file.size_bytes
876
+ fs_path, upload_url, file_obj.size_bytes
876
877
  )
877
878
 
878
879
  return types.File._from_response(
@@ -1211,16 +1212,17 @@ class AsyncFiles(_api_module.BaseModule):
1211
1212
  async def upload(
1212
1213
  self,
1213
1214
  *,
1214
- path: Union[str, pathlib.Path, os.PathLike, io.IOBase],
1215
+ file: Union[str, pathlib.Path, os.PathLike, io.IOBase],
1215
1216
  config: Optional[types.UploadFileConfigOrDict] = None,
1216
1217
  ) -> types.File:
1217
1218
  """Calls the API to upload a file asynchronously using a supported file service.
1218
1219
 
1219
1220
  Args:
1220
- path: The path to the file or an `IOBase` object to be uploaded. If it's
1221
- an IOBase object, it must be opened in blocking mode and binary mode. In
1222
- other words, do not use non-blocking mode or text mode. The given stream
1223
- must be seekable, that is, it must be able to call seek() on 'path'.
1221
+ file: A path to the file or an `IOBase` object to be uploaded. If it's an
1222
+ IOBase object, it must be opened in blocking (the default) mode and
1223
+ binary mode. In other words, do not use non-blocking mode or text mode.
1224
+ The given stream must be seekable, that is, it must be able to call
1225
+ `seek()` on 'path'.
1224
1226
  config: Optional parameters to set `diplay_name`, `mime_type`, and `name`.
1225
1227
  """
1226
1228
  if self._api_client.vertexai:
@@ -1234,37 +1236,37 @@ class AsyncFiles(_api_module.BaseModule):
1234
1236
  config_model = types.UploadFileConfig(**config)
1235
1237
  else:
1236
1238
  config_model = config
1237
- file = types.File(
1239
+ file_obj = types.File(
1238
1240
  mime_type=config_model.mime_type,
1239
1241
  name=config_model.name,
1240
1242
  display_name=config_model.display_name,
1241
1243
  )
1242
1244
  else: # if not config
1243
- file = types.File()
1244
- if file.name is not None and not file.name.startswith('files/'):
1245
- file.name = f'files/{file.name}'
1245
+ file_obj = types.File()
1246
+ if file_obj.name is not None and not file_obj.name.startswith('files/'):
1247
+ file_obj.name = f'files/{file_obj.name}'
1246
1248
 
1247
- if isinstance(path, io.IOBase):
1248
- if file.mime_type is None:
1249
+ if isinstance(file, io.IOBase):
1250
+ if file_obj.mime_type is None:
1249
1251
  raise ValueError(
1250
1252
  'Unknown mime type: Could not determine the mimetype for your'
1251
1253
  ' file\n please set the `mime_type` argument'
1252
1254
  )
1253
- if hasattr(path, 'mode'):
1254
- if 'b' not in path.mode:
1255
+ if hasattr(file, 'mode'):
1256
+ if 'b' not in file.mode:
1255
1257
  raise ValueError('The file must be opened in binary mode.')
1256
- offset = path.tell()
1257
- path.seek(0, os.SEEK_END)
1258
- file.size_bytes = path.tell() - offset
1259
- path.seek(offset, os.SEEK_SET)
1258
+ offset = file.tell()
1259
+ file.seek(0, os.SEEK_END)
1260
+ file_obj.size_bytes = file.tell() - offset
1261
+ file.seek(offset, os.SEEK_SET)
1260
1262
  else:
1261
- fs_path = os.fspath(path)
1263
+ fs_path = os.fspath(file)
1262
1264
  if not fs_path or not os.path.isfile(fs_path):
1263
- raise FileNotFoundError(f'{path} is not a valid file path.')
1264
- file.size_bytes = os.path.getsize(fs_path)
1265
- if file.mime_type is None:
1266
- file.mime_type, _ = mimetypes.guess_type(fs_path)
1267
- if file.mime_type is None:
1265
+ raise FileNotFoundError(f'{file} is not a valid file path.')
1266
+ file_obj.size_bytes = os.path.getsize(fs_path)
1267
+ if file_obj.mime_type is None:
1268
+ file_obj.mime_type, _ = mimetypes.guess_type(fs_path)
1269
+ if file_obj.mime_type is None:
1268
1270
  raise ValueError(
1269
1271
  'Unknown mime type: Could not determine the mimetype for your'
1270
1272
  ' file\n please set the `mime_type` argument'
@@ -1280,12 +1282,12 @@ class AsyncFiles(_api_module.BaseModule):
1280
1282
  'Content-Type': 'application/json',
1281
1283
  'X-Goog-Upload-Protocol': 'resumable',
1282
1284
  'X-Goog-Upload-Command': 'start',
1283
- 'X-Goog-Upload-Header-Content-Length': f'{file.size_bytes}',
1284
- 'X-Goog-Upload-Header-Content-Type': f'{file.mime_type}',
1285
+ 'X-Goog-Upload-Header-Content-Length': f'{file_obj.size_bytes}',
1286
+ 'X-Goog-Upload-Header-Content-Type': f'{file_obj.mime_type}',
1285
1287
  },
1286
1288
  'deprecated_response_payload': response,
1287
1289
  }
1288
- await self._create(file=file, config={'http_options': http_options})
1290
+ await self._create(file=file_obj, config={'http_options': http_options})
1289
1291
  if (
1290
1292
  'headers' not in response
1291
1293
  or 'X-Goog-Upload-URL' not in response['headers']
@@ -1296,13 +1298,13 @@ class AsyncFiles(_api_module.BaseModule):
1296
1298
  )
1297
1299
  upload_url = response['headers']['X-Goog-Upload-URL']
1298
1300
 
1299
- if isinstance(path, io.IOBase):
1301
+ if isinstance(file, io.IOBase):
1300
1302
  return_file = await self._api_client.async_upload_file(
1301
- path, upload_url, file.size_bytes
1303
+ file, upload_url, file_obj.size_bytes
1302
1304
  )
1303
1305
  else:
1304
1306
  return_file = await self._api_client.async_upload_file(
1305
- fs_path, upload_url, file.size_bytes
1307
+ fs_path, upload_url, file_obj.size_bytes
1306
1308
  )
1307
1309
 
1308
1310
  return types.File._from_response(
google/genai/models.py CHANGED
@@ -16,7 +16,7 @@
16
16
  # Code generated by the Google Gen AI SDK generator DO NOT EDIT.
17
17
 
18
18
  import logging
19
- from typing import AsyncIterator, Awaitable, Iterator, Optional, Union
19
+ from typing import Any, AsyncIterator, Awaitable, Iterator, Optional, Union
20
20
  from urllib.parse import urlencode
21
21
  from . import _api_module
22
22
  from . import _common
@@ -904,6 +904,9 @@ def _GenerateContentConfig_to_mldev(
904
904
  ),
905
905
  )
906
906
 
907
+ if getv(from_object, ['labels']) is not None:
908
+ raise ValueError('labels parameter is not supported in Gemini API.')
909
+
907
910
  if getv(from_object, ['cached_content']) is not None:
908
911
  setv(
909
912
  parent_object,
@@ -1066,6 +1069,9 @@ def _GenerateContentConfig_to_vertex(
1066
1069
  ),
1067
1070
  )
1068
1071
 
1072
+ if getv(from_object, ['labels']) is not None:
1073
+ setv(parent_object, ['labels'], getv(from_object, ['labels']))
1074
+
1069
1075
  if getv(from_object, ['cached_content']) is not None:
1070
1076
  setv(
1071
1077
  parent_object,
@@ -2785,7 +2791,7 @@ def _ComputeTokensParameters_to_vertex(
2785
2791
  return to_object
2786
2792
 
2787
2793
 
2788
- def _MediaResolution_to_mldev_enum_validate(enum_value: any):
2794
+ def _MediaResolution_to_mldev_enum_validate(enum_value: Any):
2789
2795
  if enum_value in set([
2790
2796
  'MEDIA_RESOLUTION_UNSPECIFIED',
2791
2797
  'MEDIA_RESOLUTION_LOW',
@@ -2795,17 +2801,17 @@ def _MediaResolution_to_mldev_enum_validate(enum_value: any):
2795
2801
  raise ValueError(f'{enum_value} enum value is not supported in Gemini API.')
2796
2802
 
2797
2803
 
2798
- def _SafetyFilterLevel_to_mldev_enum_validate(enum_value: any):
2804
+ def _SafetyFilterLevel_to_mldev_enum_validate(enum_value: Any):
2799
2805
  if enum_value in set(['BLOCK_NONE']):
2800
2806
  raise ValueError(f'{enum_value} enum value is not supported in Gemini API.')
2801
2807
 
2802
2808
 
2803
- def _PersonGeneration_to_mldev_enum_validate(enum_value: any):
2809
+ def _PersonGeneration_to_mldev_enum_validate(enum_value: Any):
2804
2810
  if enum_value in set(['ALLOW_ALL']):
2805
2811
  raise ValueError(f'{enum_value} enum value is not supported in Gemini API.')
2806
2812
 
2807
2813
 
2808
- def _MaskReferenceMode_to_mldev_enum_validate(enum_value: any):
2814
+ def _MaskReferenceMode_to_mldev_enum_validate(enum_value: Any):
2809
2815
  if enum_value in set([
2810
2816
  'MASK_MODE_DEFAULT',
2811
2817
  'MASK_MODE_USER_PROVIDED',
@@ -2816,7 +2822,7 @@ def _MaskReferenceMode_to_mldev_enum_validate(enum_value: any):
2816
2822
  raise ValueError(f'{enum_value} enum value is not supported in Gemini API.')
2817
2823
 
2818
2824
 
2819
- def _ControlReferenceType_to_mldev_enum_validate(enum_value: any):
2825
+ def _ControlReferenceType_to_mldev_enum_validate(enum_value: Any):
2820
2826
  if enum_value in set([
2821
2827
  'CONTROL_TYPE_DEFAULT',
2822
2828
  'CONTROL_TYPE_CANNY',
@@ -2826,7 +2832,7 @@ def _ControlReferenceType_to_mldev_enum_validate(enum_value: any):
2826
2832
  raise ValueError(f'{enum_value} enum value is not supported in Gemini API.')
2827
2833
 
2828
2834
 
2829
- def _SubjectReferenceType_to_mldev_enum_validate(enum_value: any):
2835
+ def _SubjectReferenceType_to_mldev_enum_validate(enum_value: Any):
2830
2836
  if enum_value in set([
2831
2837
  'SUBJECT_TYPE_DEFAULT',
2832
2838
  'SUBJECT_TYPE_PERSON',
@@ -2836,7 +2842,7 @@ def _SubjectReferenceType_to_mldev_enum_validate(enum_value: any):
2836
2842
  raise ValueError(f'{enum_value} enum value is not supported in Gemini API.')
2837
2843
 
2838
2844
 
2839
- def _EditMode_to_mldev_enum_validate(enum_value: any):
2845
+ def _EditMode_to_mldev_enum_validate(enum_value: Any):
2840
2846
  if enum_value in set([
2841
2847
  'EDIT_MODE_DEFAULT',
2842
2848
  'EDIT_MODE_INPAINT_REMOVAL',
@@ -3366,6 +3372,9 @@ def _GeneratedImage_from_mldev(
3366
3372
  getv(from_object, ['raiFilteredReason']),
3367
3373
  )
3368
3374
 
3375
+ if getv(from_object, ['prompt']) is not None:
3376
+ setv(to_object, ['enhanced_prompt'], getv(from_object, ['prompt']))
3377
+
3369
3378
  return to_object
3370
3379
 
3371
3380
 
@@ -3389,6 +3398,9 @@ def _GeneratedImage_from_vertex(
3389
3398
  getv(from_object, ['raiFilteredReason']),
3390
3399
  )
3391
3400
 
3401
+ if getv(from_object, ['prompt']) is not None:
3402
+ setv(to_object, ['enhanced_prompt'], getv(from_object, ['prompt']))
3403
+
3392
3404
  return to_object
3393
3405
 
3394
3406
 
@@ -4690,15 +4702,18 @@ class Models(_api_module.BaseModule):
4690
4702
  )
4691
4703
  if not func_response_parts:
4692
4704
  break
4693
- contents = t.t_contents(self._api_client, contents)
4694
- contents.append(response.candidates[0].content)
4695
- contents.append(
4696
- types.Content(
4697
- role='user',
4698
- parts=func_response_parts,
4699
- )
4705
+ func_call_content = response.candidates[0].content
4706
+ func_response_content = types.Content(
4707
+ role='user',
4708
+ parts=func_response_parts,
4700
4709
  )
4701
- automatic_function_calling_history.extend(contents)
4710
+ contents = t.t_contents(self._api_client, contents)
4711
+ if not automatic_function_calling_history:
4712
+ automatic_function_calling_history.extend(contents)
4713
+ contents.append(func_call_content)
4714
+ contents.append(func_response_content)
4715
+ automatic_function_calling_history.append(func_call_content)
4716
+ automatic_function_calling_history.append(func_response_content)
4702
4717
  if _extra_utils.should_append_afc_history(config):
4703
4718
  response.automatic_function_calling_history = (
4704
4719
  automatic_function_calling_history
@@ -4768,8 +4783,8 @@ class Models(_api_module.BaseModule):
4768
4783
  ) -> Pager[types.Model]:
4769
4784
  """Makes an API request to list the available models.
4770
4785
 
4771
- If `query_base` is set to True in the config, the API will return all
4772
- available base models. If set to False or not set (default), it will return
4786
+ If `query_base` is set to True in the config or not set (default), the
4787
+ API will return all available base models. If set to False, it will return
4773
4788
  all tuned models.
4774
4789
 
4775
4790
  Args:
@@ -4792,6 +4807,8 @@ class Models(_api_module.BaseModule):
4792
4807
  types._ListModelsParameters(config=config).config
4793
4808
  or types.ListModelsConfig()
4794
4809
  )
4810
+ if config.query_base is None:
4811
+ config.query_base = True
4795
4812
  if self._api_client.vertexai:
4796
4813
  config = config.copy()
4797
4814
  if not config.query_base:
@@ -4802,8 +4819,6 @@ class Models(_api_module.BaseModule):
4802
4819
  if filter_value
4803
4820
  else 'labels.tune-type:*'
4804
4821
  )
4805
- if not config.query_base:
4806
- config.query_base = False
4807
4822
  return Pager(
4808
4823
  'models',
4809
4824
  self._list,
@@ -5688,15 +5703,18 @@ class AsyncModels(_api_module.BaseModule):
5688
5703
  )
5689
5704
  if not func_response_parts:
5690
5705
  break
5691
- contents = t.t_contents(self._api_client, contents)
5692
- contents.append(response.candidates[0].content)
5693
- contents.append(
5694
- types.Content(
5695
- role='user',
5696
- parts=func_response_parts,
5697
- )
5706
+ func_call_content = response.candidates[0].content
5707
+ func_response_content = types.Content(
5708
+ role='user',
5709
+ parts=func_response_parts,
5698
5710
  )
5699
- automatic_function_calling_history.extend(contents)
5711
+ contents = t.t_contents(self._api_client, contents)
5712
+ if not automatic_function_calling_history:
5713
+ automatic_function_calling_history.extend(contents)
5714
+ contents.append(func_call_content)
5715
+ contents.append(func_response_content)
5716
+ automatic_function_calling_history.append(func_call_content)
5717
+ automatic_function_calling_history.append(func_response_content)
5700
5718
 
5701
5719
  if _extra_utils.should_append_afc_history(config):
5702
5720
  response.automatic_function_calling_history = (