google-genai 1.4.0__py3-none-any.whl → 1.6.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.
@@ -113,14 +113,14 @@ def _Video_from_mldev(
113
113
  parent_object: Optional[dict] = None,
114
114
  ) -> dict:
115
115
  to_object: dict[str, Any] = {}
116
- if getv(from_object, ['uri']) is not None:
117
- setv(to_object, ['uri'], getv(from_object, ['uri']))
116
+ if getv(from_object, ['video', 'uri']) is not None:
117
+ setv(to_object, ['uri'], getv(from_object, ['video', 'uri']))
118
118
 
119
- if getv(from_object, ['encodedVideo']) is not None:
119
+ if getv(from_object, ['video', 'encodedVideo']) is not None:
120
120
  setv(
121
121
  to_object,
122
122
  ['video_bytes'],
123
- t.t_bytes(api_client, getv(from_object, ['encodedVideo'])),
123
+ t.t_bytes(api_client, getv(from_object, ['video', 'encodedVideo'])),
124
124
  )
125
125
 
126
126
  if getv(from_object, ['encoding']) is not None:
@@ -189,13 +189,13 @@ def _GenerateVideosResponse_from_mldev(
189
189
  parent_object: Optional[dict] = None,
190
190
  ) -> dict:
191
191
  to_object: dict[str, Any] = {}
192
- if getv(from_object, ['videos']) is not None:
192
+ if getv(from_object, ['generatedSamples']) is not None:
193
193
  setv(
194
194
  to_object,
195
195
  ['generated_videos'],
196
196
  [
197
197
  _GeneratedVideo_from_mldev(api_client, item, to_object)
198
- for item in getv(from_object, ['videos'])
198
+ for item in getv(from_object, ['generatedSamples'])
199
199
  ],
200
200
  )
201
201
 
@@ -359,7 +359,7 @@ class Operations(_api_module.BaseModule):
359
359
  http_options: Optional[types.HttpOptionsOrDict] = None
360
360
  if isinstance(config, dict):
361
361
  http_options = config.get('http_options', None)
362
- elif hasattr(config, 'http_options'):
362
+ elif hasattr(config, 'http_options') and config is not None:
363
363
  http_options = config.http_options
364
364
 
365
365
  request_dict = _common.convert_to_dict(request_dict)
@@ -421,7 +421,7 @@ class Operations(_api_module.BaseModule):
421
421
  http_options: Optional[types.HttpOptionsOrDict] = None
422
422
  if isinstance(config, dict):
423
423
  http_options = config.get('http_options', None)
424
- elif hasattr(config, 'http_options'):
424
+ elif hasattr(config, 'http_options') and config is not None:
425
425
  http_options = config.http_options
426
426
 
427
427
  request_dict = _common.convert_to_dict(request_dict)
@@ -465,10 +465,24 @@ class Operations(_api_module.BaseModule):
465
465
  # TODO(b/398233524): Cast operation types
466
466
  if self._api_client.vertexai:
467
467
  resource_name = operation_name.rpartition('/operations/')[0]
468
+ http_options = types.HttpOptions()
469
+ if isinstance(config, dict):
470
+ dict_options = config.get('http_options', None)
471
+ if dict_options is not None:
472
+ http_options = types.HttpOptions(**dict(dict_options))
473
+ elif isinstance(config, types.GetOperationConfig) and config is not None:
474
+ http_options = (
475
+ config.http_options
476
+ if config.http_options is not None
477
+ else types.HttpOptions()
478
+ )
479
+ fetch_operation_config = types.FetchPredictOperationConfig(
480
+ http_options=http_options
481
+ )
468
482
  return self._fetch_predict_operation(
469
483
  operation_name=operation_name,
470
484
  resource_name=resource_name,
471
- config=config,
485
+ config=fetch_operation_config,
472
486
  )
473
487
  else:
474
488
  return self._get_operation(
@@ -519,7 +533,7 @@ class AsyncOperations(_api_module.BaseModule):
519
533
  http_options: Optional[types.HttpOptionsOrDict] = None
520
534
  if isinstance(config, dict):
521
535
  http_options = config.get('http_options', None)
522
- elif hasattr(config, 'http_options'):
536
+ elif hasattr(config, 'http_options') and config is not None:
523
537
  http_options = config.http_options
524
538
 
525
539
  request_dict = _common.convert_to_dict(request_dict)
@@ -581,7 +595,7 @@ class AsyncOperations(_api_module.BaseModule):
581
595
  http_options: Optional[types.HttpOptionsOrDict] = None
582
596
  if isinstance(config, dict):
583
597
  http_options = config.get('http_options', None)
584
- elif hasattr(config, 'http_options'):
598
+ elif hasattr(config, 'http_options') and config is not None:
585
599
  http_options = config.http_options
586
600
 
587
601
  request_dict = _common.convert_to_dict(request_dict)
@@ -623,10 +637,24 @@ class AsyncOperations(_api_module.BaseModule):
623
637
 
624
638
  if self._api_client.vertexai:
625
639
  resource_name = operation_name.rpartition('/operations/')[0]
640
+ http_options = types.HttpOptions()
641
+ if isinstance(config, dict):
642
+ dict_options = config.get('http_options', None)
643
+ if dict_options is not None:
644
+ http_options = types.HttpOptions(**dict(dict_options))
645
+ elif isinstance(config, types.GetOperationConfig) and config is not None:
646
+ http_options = (
647
+ config.http_options
648
+ if config.http_options is not None
649
+ else types.HttpOptions()
650
+ )
651
+ fetch_operation_config = types.FetchPredictOperationConfig(
652
+ http_options=http_options
653
+ )
626
654
  return await self._fetch_predict_operation(
627
655
  operation_name=operation_name,
628
656
  resource_name=resource_name,
629
- config=config,
657
+ config=fetch_operation_config,
630
658
  )
631
659
  else:
632
660
  return await self._get_operation(
google/genai/pagers.py CHANGED
@@ -18,7 +18,7 @@
18
18
  # pylint: disable=protected-access
19
19
 
20
20
  import copy
21
- from typing import Any, Awaitable, Callable, Generic, Iterator, Literal, TypeVar
21
+ from typing import Any, AsyncIterator,Awaitable, Callable, Generic, Iterator, Literal, TypeVar
22
22
 
23
23
  T = TypeVar('T')
24
24
 
@@ -30,7 +30,7 @@ PagedItem = Literal[
30
30
  class _BasePager(Generic[T]):
31
31
  """Base pager class for iterating through paginated results."""
32
32
 
33
- def __init__(
33
+ def _init_page(
34
34
  self,
35
35
  name: PagedItem,
36
36
  request: Callable[..., Any],
@@ -54,11 +54,20 @@ class _BasePager(Generic[T]):
54
54
 
55
55
  self._page_size = request_config.get('page_size', len(self._page))
56
56
 
57
+ def __init__(
58
+ self,
59
+ name: PagedItem,
60
+ request: Callable[..., Any],
61
+ response: Any,
62
+ config: Any,
63
+ ):
64
+ self._init_page(name, request, response, config)
65
+
57
66
  @property
58
67
  def page(self) -> list[T]:
59
- """Returns the current page, which is a list of items.
68
+ """Returns a subset of the entire list of items.
60
69
 
61
- The returned list of items is a subset of the entire list.
70
+ For the number of items returned, see `pageSize()`.
62
71
 
63
72
  Usage:
64
73
 
@@ -72,7 +81,7 @@ class _BasePager(Generic[T]):
72
81
  return self._page
73
82
 
74
83
  @property
75
- def name(self) -> str:
84
+ def name(self) -> PagedItem:
76
85
  """Returns the type of paged item (for example, ``batch_jobs``).
77
86
 
78
87
  Usage:
@@ -88,9 +97,7 @@ class _BasePager(Generic[T]):
88
97
 
89
98
  @property
90
99
  def page_size(self) -> int:
91
- """Returns the length of the page fetched each time by this pager.
92
-
93
- The number of items in the page is less than or equal to the page length.
100
+ """Returns the maximum number of items fetched by the pager at one time.
94
101
 
95
102
  Usage:
96
103
 
@@ -139,7 +146,7 @@ class _BasePager(Generic[T]):
139
146
  Args:
140
147
  response: The response object from the API request.
141
148
  """
142
- self.__init__(self.name, self._request, response, self.config)
149
+ self._init_page(self.name, self._request, response, self.config)
143
150
 
144
151
 
145
152
  class Pager(_BasePager[T]):
@@ -197,7 +204,7 @@ class AsyncPager(_BasePager[T]):
197
204
  ):
198
205
  super().__init__(name, request, response, config)
199
206
 
200
- def __aiter__(self) -> T:
207
+ def __aiter__(self) -> AsyncIterator[T]:
201
208
  """Returns an async iterator over the items."""
202
209
  self._idx = 0
203
210
  return self
google/genai/tunings.py CHANGED
@@ -332,7 +332,7 @@ def _CreateTuningJobConfig_to_vertex(
332
332
 
333
333
  if getv(from_object, ['learning_rate_multiplier']) is not None:
334
334
  setv(
335
- to_object,
335
+ parent_object,
336
336
  ['supervisedTuningSpec', 'hyperParameters', 'learningRateMultiplier'],
337
337
  getv(from_object, ['learning_rate_multiplier']),
338
338
  )
@@ -757,7 +757,7 @@ class Tunings(_api_module.BaseModule):
757
757
  http_options: Optional[types.HttpOptionsOrDict] = None
758
758
  if isinstance(config, dict):
759
759
  http_options = config.get('http_options', None)
760
- elif hasattr(config, 'http_options'):
760
+ elif hasattr(config, 'http_options') and config is not None:
761
761
  http_options = config.http_options
762
762
 
763
763
  request_dict = _common.convert_to_dict(request_dict)
@@ -781,13 +781,13 @@ class Tunings(_api_module.BaseModule):
781
781
  def _list(
782
782
  self, *, config: Optional[types.ListTuningJobsConfigOrDict] = None
783
783
  ) -> types.ListTuningJobsResponse:
784
- """Lists tuning jobs.
784
+ """Lists `TuningJob` objects.
785
785
 
786
786
  Args:
787
787
  config: The configuration for the list request.
788
788
 
789
789
  Returns:
790
- A list of tuning jobs.
790
+ A list of `TuningJob` objects.
791
791
  """
792
792
 
793
793
  parameter_model = types._ListTuningJobsParameters(
@@ -823,7 +823,7 @@ class Tunings(_api_module.BaseModule):
823
823
  http_options: Optional[types.HttpOptionsOrDict] = None
824
824
  if isinstance(config, dict):
825
825
  http_options = config.get('http_options', None)
826
- elif hasattr(config, 'http_options'):
826
+ elif hasattr(config, 'http_options') and config is not None:
827
827
  http_options = config.http_options
828
828
 
829
829
  request_dict = _common.convert_to_dict(request_dict)
@@ -855,7 +855,7 @@ class Tunings(_api_module.BaseModule):
855
855
  training_dataset: types.TuningDatasetOrDict,
856
856
  config: Optional[types.CreateTuningJobConfigOrDict] = None,
857
857
  ) -> types.TuningJob:
858
- """Creates a supervised fine-tuning job.
858
+ """Creates a supervised fine-tuning job and returns the TuningJob object.
859
859
 
860
860
  Args:
861
861
  base_model: The name of the model to tune.
@@ -894,7 +894,7 @@ class Tunings(_api_module.BaseModule):
894
894
  http_options: Optional[types.HttpOptionsOrDict] = None
895
895
  if isinstance(config, dict):
896
896
  http_options = config.get('http_options', None)
897
- elif hasattr(config, 'http_options'):
897
+ elif hasattr(config, 'http_options') and config is not None:
898
898
  http_options = config.http_options
899
899
 
900
900
  request_dict = _common.convert_to_dict(request_dict)
@@ -922,7 +922,7 @@ class Tunings(_api_module.BaseModule):
922
922
  training_dataset: types.TuningDatasetOrDict,
923
923
  config: Optional[types.CreateTuningJobConfigOrDict] = None,
924
924
  ) -> types.Operation:
925
- """Creates a supervised fine-tuning job.
925
+ """Creates a supervised fine-tuning job and returns the TuningJob object.
926
926
 
927
927
  Args:
928
928
  base_model: The name of the model to tune.
@@ -961,7 +961,7 @@ class Tunings(_api_module.BaseModule):
961
961
  http_options: Optional[types.HttpOptionsOrDict] = None
962
962
  if isinstance(config, dict):
963
963
  http_options = config.get('http_options', None)
964
- elif hasattr(config, 'http_options'):
964
+ elif hasattr(config, 'http_options') and config is not None:
965
965
  http_options = config.http_options
966
966
 
967
967
  request_dict = _common.convert_to_dict(request_dict)
@@ -999,7 +999,11 @@ class Tunings(_api_module.BaseModule):
999
999
  config: Optional[types.GetTuningJobConfigOrDict] = None,
1000
1000
  ) -> types.TuningJob:
1001
1001
  job = self._get(name=name, config=config)
1002
- if job.experiment and self._api_client.vertexai:
1002
+ if (
1003
+ job.experiment
1004
+ and self._api_client.vertexai
1005
+ and self._api_client.project is not None
1006
+ ):
1003
1007
  _IpythonUtils.display_experiment_button(
1004
1008
  experiment=job.experiment,
1005
1009
  project=self._api_client.project,
@@ -1029,11 +1033,12 @@ class Tunings(_api_module.BaseModule):
1029
1033
  training_dataset=training_dataset,
1030
1034
  config=config,
1031
1035
  )
1032
- operation_dict = operation.to_json_dict()
1033
- try:
1034
- tuned_model_name = operation_dict['metadata']['tunedModel']
1035
- except KeyError:
1036
- tuned_model_name = operation_dict['name'].partition('/operations/')[0]
1036
+ if operation.metadata is not None and 'tunedModel' in operation.metadata:
1037
+ tuned_model_name = operation.metadata['tunedModel']
1038
+ else:
1039
+ if operation.name is None:
1040
+ raise ValueError('Operation name is required.')
1041
+ tuned_model_name = operation.name.partition('/operations/')[0]
1037
1042
  tuning_job = types.TuningJob(
1038
1043
  name=tuned_model_name,
1039
1044
  state=types.JobState.JOB_STATE_QUEUED,
@@ -1096,7 +1101,7 @@ class AsyncTunings(_api_module.BaseModule):
1096
1101
  http_options: Optional[types.HttpOptionsOrDict] = None
1097
1102
  if isinstance(config, dict):
1098
1103
  http_options = config.get('http_options', None)
1099
- elif hasattr(config, 'http_options'):
1104
+ elif hasattr(config, 'http_options') and config is not None:
1100
1105
  http_options = config.http_options
1101
1106
 
1102
1107
  request_dict = _common.convert_to_dict(request_dict)
@@ -1120,13 +1125,13 @@ class AsyncTunings(_api_module.BaseModule):
1120
1125
  async def _list(
1121
1126
  self, *, config: Optional[types.ListTuningJobsConfigOrDict] = None
1122
1127
  ) -> types.ListTuningJobsResponse:
1123
- """Lists tuning jobs.
1128
+ """Lists `TuningJob` objects.
1124
1129
 
1125
1130
  Args:
1126
1131
  config: The configuration for the list request.
1127
1132
 
1128
1133
  Returns:
1129
- A list of tuning jobs.
1134
+ A list of `TuningJob` objects.
1130
1135
  """
1131
1136
 
1132
1137
  parameter_model = types._ListTuningJobsParameters(
@@ -1162,7 +1167,7 @@ class AsyncTunings(_api_module.BaseModule):
1162
1167
  http_options: Optional[types.HttpOptionsOrDict] = None
1163
1168
  if isinstance(config, dict):
1164
1169
  http_options = config.get('http_options', None)
1165
- elif hasattr(config, 'http_options'):
1170
+ elif hasattr(config, 'http_options') and config is not None:
1166
1171
  http_options = config.http_options
1167
1172
 
1168
1173
  request_dict = _common.convert_to_dict(request_dict)
@@ -1194,7 +1199,7 @@ class AsyncTunings(_api_module.BaseModule):
1194
1199
  training_dataset: types.TuningDatasetOrDict,
1195
1200
  config: Optional[types.CreateTuningJobConfigOrDict] = None,
1196
1201
  ) -> types.TuningJob:
1197
- """Creates a supervised fine-tuning job.
1202
+ """Creates a supervised fine-tuning job and returns the TuningJob object.
1198
1203
 
1199
1204
  Args:
1200
1205
  base_model: The name of the model to tune.
@@ -1233,7 +1238,7 @@ class AsyncTunings(_api_module.BaseModule):
1233
1238
  http_options: Optional[types.HttpOptionsOrDict] = None
1234
1239
  if isinstance(config, dict):
1235
1240
  http_options = config.get('http_options', None)
1236
- elif hasattr(config, 'http_options'):
1241
+ elif hasattr(config, 'http_options') and config is not None:
1237
1242
  http_options = config.http_options
1238
1243
 
1239
1244
  request_dict = _common.convert_to_dict(request_dict)
@@ -1261,7 +1266,7 @@ class AsyncTunings(_api_module.BaseModule):
1261
1266
  training_dataset: types.TuningDatasetOrDict,
1262
1267
  config: Optional[types.CreateTuningJobConfigOrDict] = None,
1263
1268
  ) -> types.Operation:
1264
- """Creates a supervised fine-tuning job.
1269
+ """Creates a supervised fine-tuning job and returns the TuningJob object.
1265
1270
 
1266
1271
  Args:
1267
1272
  base_model: The name of the model to tune.
@@ -1300,7 +1305,7 @@ class AsyncTunings(_api_module.BaseModule):
1300
1305
  http_options: Optional[types.HttpOptionsOrDict] = None
1301
1306
  if isinstance(config, dict):
1302
1307
  http_options = config.get('http_options', None)
1303
- elif hasattr(config, 'http_options'):
1308
+ elif hasattr(config, 'http_options') and config is not None:
1304
1309
  http_options = config.http_options
1305
1310
 
1306
1311
  request_dict = _common.convert_to_dict(request_dict)
@@ -1338,7 +1343,11 @@ class AsyncTunings(_api_module.BaseModule):
1338
1343
  config: Optional[types.GetTuningJobConfigOrDict] = None,
1339
1344
  ) -> types.TuningJob:
1340
1345
  job = await self._get(name=name, config=config)
1341
- if job.experiment and self._api_client.vertexai:
1346
+ if (
1347
+ job.experiment
1348
+ and self._api_client.vertexai
1349
+ and self._api_client.project is not None
1350
+ ):
1342
1351
  _IpythonUtils.display_experiment_button(
1343
1352
  experiment=job.experiment,
1344
1353
  project=self._api_client.project,
@@ -1368,11 +1377,12 @@ class AsyncTunings(_api_module.BaseModule):
1368
1377
  training_dataset=training_dataset,
1369
1378
  config=config,
1370
1379
  )
1371
- operation_dict = operation.to_json_dict()
1372
- try:
1373
- tuned_model_name = operation_dict['metadata']['tunedModel']
1374
- except KeyError:
1375
- tuned_model_name = operation_dict['name'].partition('/operations/')[0]
1380
+ if operation.metadata is not None and 'tunedModel' in operation.metadata:
1381
+ tuned_model_name = operation.metadata['tunedModel']
1382
+ else:
1383
+ if operation.name is None:
1384
+ raise ValueError('Operation name is required.')
1385
+ tuned_model_name = operation.name.partition('/operations/')[0]
1376
1386
  tuning_job = types.TuningJob(
1377
1387
  name=tuned_model_name,
1378
1388
  state=types.JobState.JOB_STATE_QUEUED,
@@ -1404,7 +1414,7 @@ class _IpythonUtils:
1404
1414
  return bool(_IpythonUtils._get_ipython_shell_name())
1405
1415
 
1406
1416
  @staticmethod
1407
- def _get_styles() -> None:
1417
+ def _get_styles() -> str:
1408
1418
  """Returns the HTML style markup to support custom buttons."""
1409
1419
  return """
1410
1420
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">