Skip to content

Commit 197fa46

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor!: Remove deprecated field: deprecated_response_payload
PiperOrigin-RevId: 723238336
1 parent 4a38fdf commit 197fa46

4 files changed

Lines changed: 50 additions & 42 deletions

File tree

google/genai/_api_client.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ class HttpRequest:
9999
timeout: Optional[float] = None
100100

101101

102+
# TODO(b/394358912): Update this class to use a SDKResponse class that can be
103+
# generated and used for all languages.
104+
@dataclass
105+
class BaseResponse:
106+
http_headers: dict[str, str]
107+
108+
@property
109+
def dict(self) -> dict[str, Any]:
110+
if isinstance(self, dict):
111+
return self
112+
return {'httpHeaders': self.http_headers}
113+
114+
102115
class HttpResponse:
103116

104117
def __init__(
@@ -434,18 +447,12 @@ def request(
434447
http_method, path, request_dict, http_options
435448
)
436449
response = self._request(http_request, stream=False)
437-
if http_options:
438-
if (
439-
isinstance(http_options, HttpOptions)
440-
and http_options.deprecated_response_payload is not None
441-
):
442-
response._copy_to_dict(http_options.deprecated_response_payload)
443-
elif (
444-
isinstance(http_options, dict)
445-
and 'deprecated_response_payload' in http_options
446-
):
447-
response._copy_to_dict(http_options['deprecated_response_payload'])
448-
return response.json
450+
json_response = response.json
451+
if not json_response:
452+
base_response = BaseResponse(response.headers).dict
453+
return base_response
454+
455+
return json_response
449456

450457
def request_streamed(
451458
self,
@@ -459,10 +466,6 @@ def request_streamed(
459466
)
460467

461468
session_response = self._request(http_request, stream=True)
462-
if http_options and 'deprecated_response_payload' in http_options:
463-
session_response._copy_to_dict(
464-
http_options['deprecated_response_payload']
465-
)
466469
for chunk in session_response.segments():
467470
yield chunk
468471

@@ -478,9 +481,11 @@ async def async_request(
478481
)
479482

480483
result = await self._async_request(http_request=http_request, stream=False)
481-
if http_options and 'deprecated_response_payload' in http_options:
482-
result._copy_to_dict(http_options['deprecated_response_payload'])
483-
return result.json
484+
json_response = result.json
485+
if not json_response:
486+
base_response = BaseResponse(result.headers).dict
487+
return base_response
488+
return json_response
484489

485490
async def async_request_streamed(
486491
self,
@@ -495,8 +500,6 @@ async def async_request_streamed(
495500

496501
response = await self._async_request(http_request=http_request, stream=True)
497502

498-
if http_options and 'deprecated_response_payload' in http_options:
499-
response._copy_to_dict(http_options['deprecated_response_payload'])
500503
async def async_generator():
501504
async for chunk in response:
502505
yield chunk

google/genai/_replay_api_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ def _verify_response(self, response_model: BaseModel):
362362
if self._should_update_replay():
363363
if isinstance(response_model, list):
364364
response_model = response_model[0]
365+
if response_model:
366+
response_model.http_headers.pop('Date', None)
365367
interaction.response.sdk_response_segments.append(
366368
response_model.model_dump(exclude_none=True)
367369
)

google/genai/files.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ def _CreateFileResponse_from_mldev(
494494
parent_object: dict = None,
495495
) -> dict:
496496
to_object = {}
497+
if getv(from_object, ['httpHeaders']) is not None:
498+
setv(to_object, ['http_headers'], getv(from_object, ['httpHeaders']))
497499

498500
return to_object
499501

@@ -504,6 +506,8 @@ def _CreateFileResponse_from_vertex(
504506
parent_object: dict = None,
505507
) -> dict:
506508
to_object = {}
509+
if getv(from_object, ['httpHeaders']) is not None:
510+
setv(to_object, ['http_headers'], getv(from_object, ['httpHeaders']))
507511

508512
return to_object
509513

@@ -840,7 +844,7 @@ def upload(
840844
'Unknown mime type: Could not determine the mimetype for your'
841845
' file\n please set the `mime_type` argument'
842846
)
843-
response = {}
847+
844848
if config_model and config_model.http_options:
845849
http_options = config_model.http_options
846850
else:
@@ -853,19 +857,20 @@ def upload(
853857
'X-Goog-Upload-Header-Content-Length': f'{file_obj.size_bytes}',
854858
'X-Goog-Upload-Header-Content-Type': f'{file_obj.mime_type}',
855859
},
856-
'deprecated_response_payload': response,
857860
}
858-
self._create(file=file_obj, config={'http_options': http_options})
861+
response = self._create(
862+
file=file_obj, config={'http_options': http_options}
863+
)
859864

860865
if (
861-
'headers' not in response
862-
or 'X-Goog-Upload-URL' not in response['headers']
866+
response.http_headers is None
867+
or 'X-Goog-Upload-URL' not in response.http_headers
863868
):
864869
raise KeyError(
865870
'Failed to create file. Upload URL did not returned from the create'
866871
' file request.'
867872
)
868-
upload_url = response['headers']['X-Goog-Upload-URL']
873+
upload_url = response.http_headers['X-Goog-Upload-URL']
869874

870875
if isinstance(file, io.IOBase):
871876
return_file = self._api_client.upload_file(
@@ -1272,7 +1277,6 @@ async def upload(
12721277
' file\n please set the `mime_type` argument'
12731278
)
12741279

1275-
response = {}
12761280
if config_model and config_model.http_options:
12771281
http_options = config_model.http_options
12781282
else:
@@ -1285,18 +1289,20 @@ async def upload(
12851289
'X-Goog-Upload-Header-Content-Length': f'{file_obj.size_bytes}',
12861290
'X-Goog-Upload-Header-Content-Type': f'{file_obj.mime_type}',
12871291
},
1288-
'deprecated_response_payload': response,
12891292
}
1290-
await self._create(file=file_obj, config={'http_options': http_options})
1293+
response = await self._create(
1294+
file=file_obj, config={'http_options': http_options}
1295+
)
1296+
12911297
if (
1292-
'headers' not in response
1293-
or 'X-Goog-Upload-URL' not in response['headers']
1298+
response.http_headers is None
1299+
or 'X-Goog-Upload-URL' not in response.http_headers
12941300
):
12951301
raise KeyError(
12961302
'Failed to create file. Upload URL did not returned from the create'
12971303
' file request.'
12981304
)
1299-
upload_url = response['headers']['X-Goog-Upload-URL']
1305+
upload_url = response.http_headers['X-Goog-Upload-URL']
13001306

13011307
if isinstance(file, io.IOBase):
13021308
return_file = await self._api_client.async_upload_file(

google/genai/types.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,6 @@ class HttpOptions(_common.BaseModel):
723723
timeout: Optional[int] = Field(
724724
default=None, description="""Timeout for the request in milliseconds."""
725725
)
726-
deprecated_response_payload: Optional[dict[str, Any]] = Field(
727-
default=None,
728-
description="""This field is deprecated. If set, the response payload will be returned int the supplied dict.""",
729-
)
730726

731727

732728
class HttpOptionsDict(TypedDict, total=False):
@@ -744,9 +740,6 @@ class HttpOptionsDict(TypedDict, total=False):
744740
timeout: Optional[int]
745741
"""Timeout for the request in milliseconds."""
746742

747-
deprecated_response_payload: Optional[dict[str, Any]]
748-
"""This field is deprecated. If set, the response payload will be returned int the supplied dict."""
749-
750743

751744
HttpOptionsOrDict = Union[HttpOptions, HttpOptionsDict]
752745

@@ -6583,13 +6576,17 @@ class _CreateFileParametersDict(TypedDict, total=False):
65836576
class CreateFileResponse(_common.BaseModel):
65846577
"""Response for the create file method."""
65856578

6586-
pass
6579+
http_headers: Optional[dict[str, str]] = Field(
6580+
default=None,
6581+
description="""Used to retain the HTTP headers in the request""",
6582+
)
65876583

65886584

65896585
class CreateFileResponseDict(TypedDict, total=False):
65906586
"""Response for the create file method."""
65916587

6592-
pass
6588+
http_headers: Optional[dict[str, str]]
6589+
"""Used to retain the HTTP headers in the request"""
65936590

65946591

65956592
CreateFileResponseOrDict = Union[CreateFileResponse, CreateFileResponseDict]

0 commit comments

Comments
 (0)