google-genai 1.19.0__py3-none-any.whl → 1.21.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.
google/genai/chats.py CHANGED
@@ -63,13 +63,8 @@ def _extract_curated_history(
63
63
  """Extracts the curated (valid) history from a comprehensive history.
64
64
 
65
65
  The comprehensive history contains all turns (user input and model responses),
66
- including any invalid or rejected model outputs. This function filters
67
- that history to return only the valid turns.
68
-
69
- A "turn" starts with one user input (a single content) and then follows by
70
- corresponding model response (which may consist of multiple contents).
71
- Turns are assumed to alternate: user input, model output, user input, model
72
- output, etc.
66
+ including any invalid or rejected model outputs. This function filters that
67
+ history to return only the valid turns.
73
68
 
74
69
  Args:
75
70
  comprehensive_history: A list representing the complete chat history.
@@ -84,8 +79,6 @@ def _extract_curated_history(
84
79
  length = len(comprehensive_history)
85
80
  i = 0
86
81
  current_input = comprehensive_history[i]
87
- if current_input.role != "user":
88
- raise ValueError("History must start with a user turn.")
89
82
  while i < length:
90
83
  if comprehensive_history[i].role not in ["user", "model"]:
91
84
  raise ValueError(
@@ -94,6 +87,7 @@ def _extract_curated_history(
94
87
 
95
88
  if comprehensive_history[i].role == "user":
96
89
  current_input = comprehensive_history[i]
90
+ curated_history.append(current_input)
97
91
  i += 1
98
92
  else:
99
93
  current_output = []
@@ -104,8 +98,9 @@ def _extract_curated_history(
104
98
  is_valid = False
105
99
  i += 1
106
100
  if is_valid:
107
- curated_history.append(current_input)
108
101
  curated_history.extend(current_output)
102
+ elif curated_history:
103
+ curated_history.pop()
109
104
  return curated_history
110
105
 
111
106
 
@@ -255,7 +250,7 @@ class Chat(_BaseChat):
255
250
  f"Message must be a valid part type: {types.PartUnion} or"
256
251
  f" {types.PartUnionDict}, got {type(message)}"
257
252
  )
258
- input_content = t.t_content(self._modules._api_client, message)
253
+ input_content = t.t_content(message)
259
254
  response = self._modules.generate_content(
260
255
  model=self._model,
261
256
  contents=self._curated_history + [input_content], # type: ignore[arg-type]
@@ -308,7 +303,7 @@ class Chat(_BaseChat):
308
303
  f"Message must be a valid part type: {types.PartUnion} or"
309
304
  f" {types.PartUnionDict}, got {type(message)}"
310
305
  )
311
- input_content = t.t_content(self._modules._api_client, message)
306
+ input_content = t.t_content(message)
312
307
  output_contents = []
313
308
  finish_reason = None
314
309
  is_valid = True
@@ -417,7 +412,7 @@ class AsyncChat(_BaseChat):
417
412
  f"Message must be a valid part type: {types.PartUnion} or"
418
413
  f" {types.PartUnionDict}, got {type(message)}"
419
414
  )
420
- input_content = t.t_content(self._modules._api_client, message)
415
+ input_content = t.t_content(message)
421
416
  response = await self._modules.generate_content(
422
417
  model=self._model,
423
418
  contents=self._curated_history + [input_content], # type: ignore[arg-type]
@@ -469,7 +464,7 @@ class AsyncChat(_BaseChat):
469
464
  f"Message must be a valid part type: {types.PartUnion} or"
470
465
  f" {types.PartUnionDict}, got {type(message)}"
471
466
  )
472
- input_content = t.t_content(self._modules._api_client, message)
467
+ input_content = t.t_content(message)
473
468
 
474
469
  async def async_generator(): # type: ignore[no-untyped-def]
475
470
  output_contents = []
google/genai/client.py CHANGED
@@ -31,7 +31,7 @@ from .models import AsyncModels, Models
31
31
  from .operations import AsyncOperations, Operations
32
32
  from .tokens import AsyncTokens, Tokens
33
33
  from .tunings import AsyncTunings, Tunings
34
- from .types import HttpOptions, HttpOptionsDict
34
+ from .types import HttpOptions, HttpOptionsDict, HttpRetryOptions
35
35
 
36
36
 
37
37
  class AsyncClient:
google/genai/errors.py CHANGED
@@ -22,6 +22,7 @@ import json
22
22
 
23
23
  if TYPE_CHECKING:
24
24
  from .replay_api_client import ReplayResponse
25
+ import aiohttp
25
26
 
26
27
 
27
28
  class APIError(Exception):
@@ -36,7 +37,9 @@ class APIError(Exception):
36
37
  self,
37
38
  code: int,
38
39
  response_json: Any,
39
- response: Optional[Union['ReplayResponse', httpx.Response]] = None,
40
+ response: Optional[
41
+ Union['ReplayResponse', httpx.Response, 'aiohttp.ClientResponse']
42
+ ] = None,
40
43
  ):
41
44
  self.response = response
42
45
  self.details = response_json
@@ -106,12 +109,17 @@ class APIError(Exception):
106
109
 
107
110
  @classmethod
108
111
  async def raise_for_async_response(
109
- cls, response: Union['ReplayResponse', httpx.Response]
112
+ cls,
113
+ response: Union[
114
+ 'ReplayResponse', httpx.Response, 'aiohttp.ClientResponse'
115
+ ],
110
116
  ) -> None:
111
117
  """Raises an error with detailed error message if the response has an error status."""
112
- if response.status_code == 200:
113
- return
118
+ status_code = 0
119
+ response_json = None
114
120
  if isinstance(response, httpx.Response):
121
+ if response.status_code == 200:
122
+ return
115
123
  try:
116
124
  await response.aread()
117
125
  response_json = response.json()
@@ -121,10 +129,28 @@ class APIError(Exception):
121
129
  'message': message,
122
130
  'status': response.reason_phrase,
123
131
  }
132
+ status_code = response.status_code
124
133
  else:
125
- response_json = response.body_segments[0].get('error', {})
134
+ try:
135
+ import aiohttp # pylint: disable=g-import-not-at-top
136
+
137
+ if isinstance(response, aiohttp.ClientResponse):
138
+ if response.status == 200:
139
+ return
140
+ try:
141
+ response_json = await response.json()
142
+ except aiohttp.client_exceptions.ContentTypeError:
143
+ message = await response.text()
144
+ response_json = {
145
+ 'message': message,
146
+ 'status': response.reason,
147
+ }
148
+ status_code = response.status
149
+ else:
150
+ response_json = response.body_segments[0].get('error', {})
151
+ except ImportError:
152
+ response_json = response.body_segments[0].get('error', {})
126
153
 
127
- status_code = response.status_code
128
154
  if 400 <= status_code < 500:
129
155
  raise ClientError(status_code, response_json, response)
130
156
  elif 500 <= status_code < 600:
google/genai/files.py CHANGED
@@ -16,17 +16,17 @@
16
16
  # Code generated by the Google Gen AI SDK generator DO NOT EDIT.
17
17
 
18
18
  import io
19
+ import json
19
20
  import logging
20
21
  import mimetypes
21
22
  import os
22
- import pathlib
23
23
  from typing import Any, Optional, Union
24
24
  from urllib.parse import urlencode
25
+
25
26
  from . import _api_module
26
27
  from . import _common
27
28
  from . import _transformers as t
28
29
  from . import types
29
- from ._api_client import BaseApiClient
30
30
  from ._common import get_value_by_path as getv
31
31
  from ._common import set_value_by_path as setv
32
32
  from .pagers import AsyncPager, Pager
@@ -35,7 +35,6 @@ logger = logging.getLogger('google_genai.files')
35
35
 
36
36
 
37
37
  def _ListFilesConfig_to_mldev(
38
- api_client: BaseApiClient,
39
38
  from_object: Union[dict[str, Any], object],
40
39
  parent_object: Optional[dict[str, Any]] = None,
41
40
  ) -> dict[str, Any]:
@@ -57,7 +56,6 @@ def _ListFilesConfig_to_mldev(
57
56
 
58
57
 
59
58
  def _ListFilesParameters_to_mldev(
60
- api_client: BaseApiClient,
61
59
  from_object: Union[dict[str, Any], object],
62
60
  parent_object: Optional[dict[str, Any]] = None,
63
61
  ) -> dict[str, Any]:
@@ -66,16 +64,13 @@ def _ListFilesParameters_to_mldev(
66
64
  setv(
67
65
  to_object,
68
66
  ['config'],
69
- _ListFilesConfig_to_mldev(
70
- api_client, getv(from_object, ['config']), to_object
71
- ),
67
+ _ListFilesConfig_to_mldev(getv(from_object, ['config']), to_object),
72
68
  )
73
69
 
74
70
  return to_object
75
71
 
76
72
 
77
73
  def _FileStatus_to_mldev(
78
- api_client: BaseApiClient,
79
74
  from_object: Union[dict[str, Any], object],
80
75
  parent_object: Optional[dict[str, Any]] = None,
81
76
  ) -> dict[str, Any]:
@@ -93,7 +88,6 @@ def _FileStatus_to_mldev(
93
88
 
94
89
 
95
90
  def _File_to_mldev(
96
- api_client: BaseApiClient,
97
91
  from_object: Union[dict[str, Any], object],
98
92
  parent_object: Optional[dict[str, Any]] = None,
99
93
  ) -> dict[str, Any]:
@@ -141,16 +135,13 @@ def _File_to_mldev(
141
135
  setv(
142
136
  to_object,
143
137
  ['error'],
144
- _FileStatus_to_mldev(
145
- api_client, getv(from_object, ['error']), to_object
146
- ),
138
+ _FileStatus_to_mldev(getv(from_object, ['error']), to_object),
147
139
  )
148
140
 
149
141
  return to_object
150
142
 
151
143
 
152
144
  def _CreateFileParameters_to_mldev(
153
- api_client: BaseApiClient,
154
145
  from_object: Union[dict[str, Any], object],
155
146
  parent_object: Optional[dict[str, Any]] = None,
156
147
  ) -> dict[str, Any]:
@@ -159,7 +150,7 @@ def _CreateFileParameters_to_mldev(
159
150
  setv(
160
151
  to_object,
161
152
  ['file'],
162
- _File_to_mldev(api_client, getv(from_object, ['file']), to_object),
153
+ _File_to_mldev(getv(from_object, ['file']), to_object),
163
154
  )
164
155
 
165
156
  if getv(from_object, ['config']) is not None:
@@ -169,16 +160,13 @@ def _CreateFileParameters_to_mldev(
169
160
 
170
161
 
171
162
  def _GetFileParameters_to_mldev(
172
- api_client: BaseApiClient,
173
163
  from_object: Union[dict[str, Any], object],
174
164
  parent_object: Optional[dict[str, Any]] = None,
175
165
  ) -> dict[str, Any]:
176
166
  to_object: dict[str, Any] = {}
177
167
  if getv(from_object, ['name']) is not None:
178
168
  setv(
179
- to_object,
180
- ['_url', 'file'],
181
- t.t_file_name(api_client, getv(from_object, ['name'])),
169
+ to_object, ['_url', 'file'], t.t_file_name(getv(from_object, ['name']))
182
170
  )
183
171
 
184
172
  if getv(from_object, ['config']) is not None:
@@ -188,16 +176,13 @@ def _GetFileParameters_to_mldev(
188
176
 
189
177
 
190
178
  def _DeleteFileParameters_to_mldev(
191
- api_client: BaseApiClient,
192
179
  from_object: Union[dict[str, Any], object],
193
180
  parent_object: Optional[dict[str, Any]] = None,
194
181
  ) -> dict[str, Any]:
195
182
  to_object: dict[str, Any] = {}
196
183
  if getv(from_object, ['name']) is not None:
197
184
  setv(
198
- to_object,
199
- ['_url', 'file'],
200
- t.t_file_name(api_client, getv(from_object, ['name'])),
185
+ to_object, ['_url', 'file'], t.t_file_name(getv(from_object, ['name']))
201
186
  )
202
187
 
203
188
  if getv(from_object, ['config']) is not None:
@@ -207,7 +192,6 @@ def _DeleteFileParameters_to_mldev(
207
192
 
208
193
 
209
194
  def _FileStatus_from_mldev(
210
- api_client: BaseApiClient,
211
195
  from_object: Union[dict[str, Any], object],
212
196
  parent_object: Optional[dict[str, Any]] = None,
213
197
  ) -> dict[str, Any]:
@@ -225,7 +209,6 @@ def _FileStatus_from_mldev(
225
209
 
226
210
 
227
211
  def _File_from_mldev(
228
- api_client: BaseApiClient,
229
212
  from_object: Union[dict[str, Any], object],
230
213
  parent_object: Optional[dict[str, Any]] = None,
231
214
  ) -> dict[str, Any]:
@@ -273,16 +256,13 @@ def _File_from_mldev(
273
256
  setv(
274
257
  to_object,
275
258
  ['error'],
276
- _FileStatus_from_mldev(
277
- api_client, getv(from_object, ['error']), to_object
278
- ),
259
+ _FileStatus_from_mldev(getv(from_object, ['error']), to_object),
279
260
  )
280
261
 
281
262
  return to_object
282
263
 
283
264
 
284
265
  def _ListFilesResponse_from_mldev(
285
- api_client: BaseApiClient,
286
266
  from_object: Union[dict[str, Any], object],
287
267
  parent_object: Optional[dict[str, Any]] = None,
288
268
  ) -> dict[str, Any]:
@@ -295,7 +275,7 @@ def _ListFilesResponse_from_mldev(
295
275
  to_object,
296
276
  ['files'],
297
277
  [
298
- _File_from_mldev(api_client, item, to_object)
278
+ _File_from_mldev(item, to_object)
299
279
  for item in getv(from_object, ['files'])
300
280
  ],
301
281
  )
@@ -304,19 +284,15 @@ def _ListFilesResponse_from_mldev(
304
284
 
305
285
 
306
286
  def _CreateFileResponse_from_mldev(
307
- api_client: BaseApiClient,
308
287
  from_object: Union[dict[str, Any], object],
309
288
  parent_object: Optional[dict[str, Any]] = None,
310
289
  ) -> dict[str, Any]:
311
290
  to_object: dict[str, Any] = {}
312
- if getv(from_object, ['httpHeaders']) is not None:
313
- setv(to_object, ['http_headers'], getv(from_object, ['httpHeaders']))
314
291
 
315
292
  return to_object
316
293
 
317
294
 
318
295
  def _DeleteFileResponse_from_mldev(
319
- api_client: BaseApiClient,
320
296
  from_object: Union[dict[str, Any], object],
321
297
  parent_object: Optional[dict[str, Any]] = None,
322
298
  ) -> dict[str, Any]:
@@ -357,9 +333,7 @@ class Files(_api_module.BaseModule):
357
333
  'This method is only supported in the Gemini Developer client.'
358
334
  )
359
335
  else:
360
- request_dict = _ListFilesParameters_to_mldev(
361
- self._api_client, parameter_model
362
- )
336
+ request_dict = _ListFilesParameters_to_mldev(parameter_model)
363
337
  request_url_dict = request_dict.get('_url')
364
338
  if request_url_dict:
365
339
  path = 'files'.format_map(request_url_dict)
@@ -382,14 +356,12 @@ class Files(_api_module.BaseModule):
382
356
  request_dict = _common.convert_to_dict(request_dict)
383
357
  request_dict = _common.encode_unserializable_types(request_dict)
384
358
 
385
- response_dict = self._api_client.request(
386
- 'get', path, request_dict, http_options
387
- )
359
+ response = self._api_client.request('get', path, request_dict, http_options)
360
+
361
+ response_dict = '' if not response.body else json.loads(response.body)
388
362
 
389
363
  if not self._api_client.vertexai:
390
- response_dict = _ListFilesResponse_from_mldev(
391
- self._api_client, response_dict
392
- )
364
+ response_dict = _ListFilesResponse_from_mldev(response_dict)
393
365
 
394
366
  return_value = types.ListFilesResponse._from_response(
395
367
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -414,9 +386,7 @@ class Files(_api_module.BaseModule):
414
386
  'This method is only supported in the Gemini Developer client.'
415
387
  )
416
388
  else:
417
- request_dict = _CreateFileParameters_to_mldev(
418
- self._api_client, parameter_model
419
- )
389
+ request_dict = _CreateFileParameters_to_mldev(parameter_model)
420
390
  request_url_dict = request_dict.get('_url')
421
391
  if request_url_dict:
422
392
  path = 'upload/v1beta/files'.format_map(request_url_dict)
@@ -439,14 +409,20 @@ class Files(_api_module.BaseModule):
439
409
  request_dict = _common.convert_to_dict(request_dict)
440
410
  request_dict = _common.encode_unserializable_types(request_dict)
441
411
 
442
- response_dict = self._api_client.request(
412
+ response = self._api_client.request(
443
413
  'post', path, request_dict, http_options
444
414
  )
445
415
 
416
+ if config is not None and getattr(
417
+ config, 'should_return_http_response', None
418
+ ):
419
+ return_value = types.CreateFileResponse(sdk_http_response=response)
420
+ return return_value
421
+
422
+ response_dict = '' if not response.body else json.loads(response.body)
423
+
446
424
  if not self._api_client.vertexai:
447
- response_dict = _CreateFileResponse_from_mldev(
448
- self._api_client, response_dict
449
- )
425
+ response_dict = _CreateFileResponse_from_mldev(response_dict)
450
426
 
451
427
  return_value = types.CreateFileResponse._from_response(
452
428
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -485,9 +461,7 @@ class Files(_api_module.BaseModule):
485
461
  'This method is only supported in the Gemini Developer client.'
486
462
  )
487
463
  else:
488
- request_dict = _GetFileParameters_to_mldev(
489
- self._api_client, parameter_model
490
- )
464
+ request_dict = _GetFileParameters_to_mldev(parameter_model)
491
465
  request_url_dict = request_dict.get('_url')
492
466
  if request_url_dict:
493
467
  path = 'files/{file}'.format_map(request_url_dict)
@@ -510,12 +484,12 @@ class Files(_api_module.BaseModule):
510
484
  request_dict = _common.convert_to_dict(request_dict)
511
485
  request_dict = _common.encode_unserializable_types(request_dict)
512
486
 
513
- response_dict = self._api_client.request(
514
- 'get', path, request_dict, http_options
515
- )
487
+ response = self._api_client.request('get', path, request_dict, http_options)
488
+
489
+ response_dict = '' if not response.body else json.loads(response.body)
516
490
 
517
491
  if not self._api_client.vertexai:
518
- response_dict = _File_from_mldev(self._api_client, response_dict)
492
+ response_dict = _File_from_mldev(response_dict)
519
493
 
520
494
  return_value = types.File._from_response(
521
495
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -553,9 +527,7 @@ class Files(_api_module.BaseModule):
553
527
  'This method is only supported in the Gemini Developer client.'
554
528
  )
555
529
  else:
556
- request_dict = _DeleteFileParameters_to_mldev(
557
- self._api_client, parameter_model
558
- )
530
+ request_dict = _DeleteFileParameters_to_mldev(parameter_model)
559
531
  request_url_dict = request_dict.get('_url')
560
532
  if request_url_dict:
561
533
  path = 'files/{file}'.format_map(request_url_dict)
@@ -578,14 +550,14 @@ class Files(_api_module.BaseModule):
578
550
  request_dict = _common.convert_to_dict(request_dict)
579
551
  request_dict = _common.encode_unserializable_types(request_dict)
580
552
 
581
- response_dict = self._api_client.request(
553
+ response = self._api_client.request(
582
554
  'delete', path, request_dict, http_options
583
555
  )
584
556
 
557
+ response_dict = '' if not response.body else json.loads(response.body)
558
+
585
559
  if not self._api_client.vertexai:
586
- response_dict = _DeleteFileResponse_from_mldev(
587
- self._api_client, response_dict
588
- )
560
+ response_dict = _DeleteFileResponse_from_mldev(response_dict)
589
561
 
590
562
  return_value = types.DeleteFileResponse._from_response(
591
563
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -596,7 +568,7 @@ class Files(_api_module.BaseModule):
596
568
  def upload(
597
569
  self,
598
570
  *,
599
- file: Union[str, pathlib.Path, os.PathLike[str], io.IOBase],
571
+ file: Union[str, os.PathLike[str], io.IOBase],
600
572
  config: Optional[types.UploadFileConfigOrDict] = None,
601
573
  ) -> types.File:
602
574
  """Calls the API to upload a file using a supported file service.
@@ -678,18 +650,22 @@ class Files(_api_module.BaseModule):
678
650
  },
679
651
  )
680
652
  response = self._create(
681
- file=file_obj, config=types.CreateFileConfig(http_options=http_options)
653
+ file=file_obj,
654
+ config=types.CreateFileConfig(
655
+ http_options=http_options, should_return_http_response=True
656
+ ),
682
657
  )
683
658
 
684
659
  if (
685
- response.http_headers is None
686
- or 'x-goog-upload-url' not in response.http_headers
660
+ response.sdk_http_response is None
661
+ or response.sdk_http_response.headers is None
662
+ or 'x-goog-upload-url' not in response.sdk_http_response.headers
687
663
  ):
688
664
  raise KeyError(
689
665
  'Failed to create file. Upload URL did not returned from the create'
690
666
  ' file request.'
691
667
  )
692
- upload_url = response.http_headers['x-goog-upload-url']
668
+ upload_url = response.sdk_http_response.headers['x-goog-upload-url']
693
669
 
694
670
  if isinstance(file, io.IOBase):
695
671
  return_file = self._api_client.upload_file(
@@ -701,7 +677,7 @@ class Files(_api_module.BaseModule):
701
677
  )
702
678
 
703
679
  return types.File._from_response(
704
- response=_File_from_mldev(self._api_client, return_file.json['file']),
680
+ response=_File_from_mldev(return_file.json['file']),
705
681
  kwargs=config_model.model_dump() if config else {},
706
682
  )
707
683
 
@@ -774,7 +750,7 @@ class Files(_api_module.BaseModule):
774
750
  'downloaded. You can tell which files are downloadable by checking '
775
751
  'the `source` or `download_uri` property.'
776
752
  )
777
- name = t.t_file_name(self._api_client, file)
753
+ name = t.t_file_name(file)
778
754
 
779
755
  path = f'files/{name}:download'
780
756
 
@@ -829,9 +805,7 @@ class AsyncFiles(_api_module.BaseModule):
829
805
  'This method is only supported in the Gemini Developer client.'
830
806
  )
831
807
  else:
832
- request_dict = _ListFilesParameters_to_mldev(
833
- self._api_client, parameter_model
834
- )
808
+ request_dict = _ListFilesParameters_to_mldev(parameter_model)
835
809
  request_url_dict = request_dict.get('_url')
836
810
  if request_url_dict:
837
811
  path = 'files'.format_map(request_url_dict)
@@ -854,14 +828,14 @@ class AsyncFiles(_api_module.BaseModule):
854
828
  request_dict = _common.convert_to_dict(request_dict)
855
829
  request_dict = _common.encode_unserializable_types(request_dict)
856
830
 
857
- response_dict = await self._api_client.async_request(
831
+ response = await self._api_client.async_request(
858
832
  'get', path, request_dict, http_options
859
833
  )
860
834
 
835
+ response_dict = '' if not response.body else json.loads(response.body)
836
+
861
837
  if not self._api_client.vertexai:
862
- response_dict = _ListFilesResponse_from_mldev(
863
- self._api_client, response_dict
864
- )
838
+ response_dict = _ListFilesResponse_from_mldev(response_dict)
865
839
 
866
840
  return_value = types.ListFilesResponse._from_response(
867
841
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -886,9 +860,7 @@ class AsyncFiles(_api_module.BaseModule):
886
860
  'This method is only supported in the Gemini Developer client.'
887
861
  )
888
862
  else:
889
- request_dict = _CreateFileParameters_to_mldev(
890
- self._api_client, parameter_model
891
- )
863
+ request_dict = _CreateFileParameters_to_mldev(parameter_model)
892
864
  request_url_dict = request_dict.get('_url')
893
865
  if request_url_dict:
894
866
  path = 'upload/v1beta/files'.format_map(request_url_dict)
@@ -911,14 +883,20 @@ class AsyncFiles(_api_module.BaseModule):
911
883
  request_dict = _common.convert_to_dict(request_dict)
912
884
  request_dict = _common.encode_unserializable_types(request_dict)
913
885
 
914
- response_dict = await self._api_client.async_request(
886
+ response = await self._api_client.async_request(
915
887
  'post', path, request_dict, http_options
916
888
  )
917
889
 
890
+ if config is not None and getattr(
891
+ config, 'should_return_http_response', None
892
+ ):
893
+ return_value = types.CreateFileResponse(sdk_http_response=response)
894
+ return return_value
895
+
896
+ response_dict = '' if not response.body else json.loads(response.body)
897
+
918
898
  if not self._api_client.vertexai:
919
- response_dict = _CreateFileResponse_from_mldev(
920
- self._api_client, response_dict
921
- )
899
+ response_dict = _CreateFileResponse_from_mldev(response_dict)
922
900
 
923
901
  return_value = types.CreateFileResponse._from_response(
924
902
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -957,9 +935,7 @@ class AsyncFiles(_api_module.BaseModule):
957
935
  'This method is only supported in the Gemini Developer client.'
958
936
  )
959
937
  else:
960
- request_dict = _GetFileParameters_to_mldev(
961
- self._api_client, parameter_model
962
- )
938
+ request_dict = _GetFileParameters_to_mldev(parameter_model)
963
939
  request_url_dict = request_dict.get('_url')
964
940
  if request_url_dict:
965
941
  path = 'files/{file}'.format_map(request_url_dict)
@@ -982,12 +958,14 @@ class AsyncFiles(_api_module.BaseModule):
982
958
  request_dict = _common.convert_to_dict(request_dict)
983
959
  request_dict = _common.encode_unserializable_types(request_dict)
984
960
 
985
- response_dict = await self._api_client.async_request(
961
+ response = await self._api_client.async_request(
986
962
  'get', path, request_dict, http_options
987
963
  )
988
964
 
965
+ response_dict = '' if not response.body else json.loads(response.body)
966
+
989
967
  if not self._api_client.vertexai:
990
- response_dict = _File_from_mldev(self._api_client, response_dict)
968
+ response_dict = _File_from_mldev(response_dict)
991
969
 
992
970
  return_value = types.File._from_response(
993
971
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -1025,9 +1003,7 @@ class AsyncFiles(_api_module.BaseModule):
1025
1003
  'This method is only supported in the Gemini Developer client.'
1026
1004
  )
1027
1005
  else:
1028
- request_dict = _DeleteFileParameters_to_mldev(
1029
- self._api_client, parameter_model
1030
- )
1006
+ request_dict = _DeleteFileParameters_to_mldev(parameter_model)
1031
1007
  request_url_dict = request_dict.get('_url')
1032
1008
  if request_url_dict:
1033
1009
  path = 'files/{file}'.format_map(request_url_dict)
@@ -1050,14 +1026,14 @@ class AsyncFiles(_api_module.BaseModule):
1050
1026
  request_dict = _common.convert_to_dict(request_dict)
1051
1027
  request_dict = _common.encode_unserializable_types(request_dict)
1052
1028
 
1053
- response_dict = await self._api_client.async_request(
1029
+ response = await self._api_client.async_request(
1054
1030
  'delete', path, request_dict, http_options
1055
1031
  )
1056
1032
 
1033
+ response_dict = '' if not response.body else json.loads(response.body)
1034
+
1057
1035
  if not self._api_client.vertexai:
1058
- response_dict = _DeleteFileResponse_from_mldev(
1059
- self._api_client, response_dict
1060
- )
1036
+ response_dict = _DeleteFileResponse_from_mldev(response_dict)
1061
1037
 
1062
1038
  return_value = types.DeleteFileResponse._from_response(
1063
1039
  response=response_dict, kwargs=parameter_model.model_dump()
@@ -1068,7 +1044,7 @@ class AsyncFiles(_api_module.BaseModule):
1068
1044
  async def upload(
1069
1045
  self,
1070
1046
  *,
1071
- file: Union[str, pathlib.Path, os.PathLike[str], io.IOBase],
1047
+ file: Union[str, os.PathLike[str], io.IOBase],
1072
1048
  config: Optional[types.UploadFileConfigOrDict] = None,
1073
1049
  ) -> types.File:
1074
1050
  """Calls the API to upload a file asynchronously using a supported file service.
@@ -1150,17 +1126,27 @@ class AsyncFiles(_api_module.BaseModule):
1150
1126
  },
1151
1127
  )
1152
1128
  response = await self._create(
1153
- file=file_obj, config=types.CreateFileConfig(http_options=http_options)
1129
+ file=file_obj,
1130
+ config=types.CreateFileConfig(
1131
+ http_options=http_options, should_return_http_response=True
1132
+ ),
1154
1133
  )
1155
1134
  if (
1156
- response.http_headers is None
1157
- or 'x-goog-upload-url' not in response.http_headers
1135
+ response.sdk_http_response is None
1136
+ or response.sdk_http_response.headers is None
1137
+ or (
1138
+ 'x-goog-upload-url' not in response.sdk_http_response.headers
1139
+ and 'X-Goog-Upload-URL' not in response.sdk_http_response.headers
1140
+ )
1158
1141
  ):
1159
1142
  raise KeyError(
1160
1143
  'Failed to create file. Upload URL did not returned from the create'
1161
1144
  ' file request.'
1162
1145
  )
1163
- upload_url = response.http_headers['x-goog-upload-url']
1146
+ elif 'x-goog-upload-url' in response.sdk_http_response.headers:
1147
+ upload_url = response.sdk_http_response.headers['x-goog-upload-url']
1148
+ else:
1149
+ upload_url = response.sdk_http_response.headers['X-Goog-Upload-URL']
1164
1150
 
1165
1151
  if isinstance(file, io.IOBase):
1166
1152
  return_file = await self._api_client.async_upload_file(
@@ -1172,7 +1158,7 @@ class AsyncFiles(_api_module.BaseModule):
1172
1158
  )
1173
1159
 
1174
1160
  return types.File._from_response(
1175
- response=_File_from_mldev(self._api_client, return_file.json['file']),
1161
+ response=_File_from_mldev(return_file.json['file']),
1176
1162
  kwargs=config_model.model_dump() if config else {},
1177
1163
  )
1178
1164
 
@@ -1233,7 +1219,7 @@ class AsyncFiles(_api_module.BaseModule):
1233
1219
  else:
1234
1220
  config_model = config
1235
1221
 
1236
- name = t.t_file_name(self._api_client, file)
1222
+ name = t.t_file_name(file)
1237
1223
 
1238
1224
  path = f'files/{name}:download'
1239
1225