Skip to content

Commit 4e56c64

Browse files
IvanLHcopybara-github
authored andcommitted
chore: Refactored the _fromResponse method on GenerateVideosOperation to use the autogenerated code.
PiperOrigin-RevId: 795187934
1 parent 5fe9f16 commit 4e56c64

File tree

6 files changed

+47
-72
lines changed

6 files changed

+47
-72
lines changed

google/genai/_base_transformers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
"""Base transformers for Google GenAI SDK."""
17+
import base64
18+
19+
# Some fields don't accept url safe base64 encoding.
20+
# We shouldn't use this transformer if the backend adhere to Cloud Type
21+
# format https://cloud.google.com/docs/discovery/type-format.
22+
# TODO(b/389133914,b/390320301): Remove the hack after backend fix the issue.
23+
def t_bytes(data: bytes) -> str:
24+
if not isinstance(data, bytes):
25+
return data
26+
return base64.b64encode(data).decode('ascii')

google/genai/_operations_converters.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
# Code generated by the Google Gen AI SDK generator DO NOT EDIT.
1717

1818
from typing import Any, Optional, Union
19-
20-
from . import _transformers as t
19+
from . import _base_transformers as base_t
2120
from ._common import get_value_by_path as getv
2221
from ._common import set_value_by_path as setv
2322

@@ -108,7 +107,7 @@ def _Video_from_mldev(
108107
setv(
109108
to_object,
110109
['video_bytes'],
111-
t.t_bytes(getv(from_object, ['video', 'encodedVideo'])),
110+
base_t.t_bytes(getv(from_object, ['video', 'encodedVideo'])),
112111
)
113112

114113
if getv(from_object, ['encoding']) is not None:
@@ -214,7 +213,7 @@ def _Video_from_vertex(
214213
setv(
215214
to_object,
216215
['video_bytes'],
217-
t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
216+
base_t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
218217
)
219218

220219
if getv(from_object, ['mimeType']) is not None:

google/genai/_transformers.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,16 +1156,6 @@ def t_tuning_job_status(status: str) -> Union[types.JobState, str]:
11561156
return status
11571157

11581158

1159-
# Some fields don't accept url safe base64 encoding.
1160-
# We shouldn't use this transformer if the backend adhere to Cloud Type
1161-
# format https://cloud.google.com/docs/discovery/type-format.
1162-
# TODO(b/389133914,b/390320301): Remove the hack after backend fix the issue.
1163-
def t_bytes(data: bytes) -> str:
1164-
if not isinstance(data, bytes):
1165-
return data
1166-
return base64.b64encode(data).decode('ascii')
1167-
1168-
11691159
def t_content_strict(content: types.ContentOrDict) -> types.Content:
11701160
if isinstance(content, dict):
11711161
return types.Content.model_validate(content)

google/genai/models.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from urllib.parse import urlencode
2222

2323
from . import _api_module
24+
from . import _base_transformers as base_t
2425
from . import _common
2526
from . import _extra_utils
2627
from . import _mcp_utils
@@ -1260,7 +1261,7 @@ def _Image_to_mldev(
12601261
setv(
12611262
to_object,
12621263
['bytesBase64Encoded'],
1263-
t.t_bytes(getv(from_object, ['image_bytes'])),
1264+
base_t.t_bytes(getv(from_object, ['image_bytes'])),
12641265
)
12651266

12661267
if getv(from_object, ['mime_type']) is not None:
@@ -2502,7 +2503,7 @@ def _Image_to_vertex(
25022503
setv(
25032504
to_object,
25042505
['bytesBase64Encoded'],
2505-
t.t_bytes(getv(from_object, ['image_bytes'])),
2506+
base_t.t_bytes(getv(from_object, ['image_bytes'])),
25062507
)
25072508

25082509
if getv(from_object, ['mime_type']) is not None:
@@ -3394,7 +3395,7 @@ def _Video_to_vertex(
33943395
setv(
33953396
to_object,
33963397
['bytesBase64Encoded'],
3397-
t.t_bytes(getv(from_object, ['video_bytes'])),
3398+
base_t.t_bytes(getv(from_object, ['video_bytes'])),
33983399
)
33993400

34003401
if getv(from_object, ['mime_type']) is not None:
@@ -3921,7 +3922,7 @@ def _Image_from_mldev(
39213922
setv(
39223923
to_object,
39233924
['image_bytes'],
3924-
t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
3925+
base_t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
39253926
)
39263927

39273928
if getv(from_object, ['mimeType']) is not None:
@@ -4147,7 +4148,7 @@ def _Video_from_mldev(
41474148
setv(
41484149
to_object,
41494150
['video_bytes'],
4150-
t.t_bytes(getv(from_object, ['video', 'encodedVideo'])),
4151+
base_t.t_bytes(getv(from_object, ['video', 'encodedVideo'])),
41514152
)
41524153

41534154
if getv(from_object, ['encoding']) is not None:
@@ -4613,7 +4614,7 @@ def _Image_from_vertex(
46134614
setv(
46144615
to_object,
46154616
['image_bytes'],
4616-
t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
4617+
base_t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
46174618
)
46184619

46194620
if getv(from_object, ['mimeType']) is not None:
@@ -5023,7 +5024,7 @@ def _Video_from_vertex(
50235024
setv(
50245025
to_object,
50255026
['video_bytes'],
5026-
t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
5027+
base_t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
50275028
)
50285029

50295030
if getv(from_object, ['mimeType']) is not None:

google/genai/tests/transformers/test_bytes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import base64
44

5-
from ... import _transformers as t
5+
from ... import _base_transformers as t
66

77
_RAW_BYTES = (
88
b'\xfb\xf6\x9bq\xd7\x9f\x82\x18\xa3\x92Y\xa7\xa2\x9a\xab\xb2\xdb\xaf\xc3\x1c\xb3\x00\x10\x83\x10Q\x87'

google/genai/types.py

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
from pydantic import ConfigDict, Field, PrivateAttr, model_validator
3030
from typing_extensions import Self, TypedDict
3131
from . import _common
32+
from ._operations_converters import (
33+
_GenerateVideosOperation_from_mldev,
34+
_GenerateVideosOperation_from_vertex,
35+
)
36+
3237

3338
if sys.version_info >= (3, 10):
3439
# Supports both Union[t1, t2] and t1 | t2
@@ -8674,58 +8679,12 @@ def from_api_response(
86748679
cls, api_response: Any, is_vertex_ai: bool = False
86758680
) -> Self:
86768681
"""Instantiates a GenerateVideosOperation from an API response."""
8677-
new_operation = cls()
8678-
new_operation.name = api_response.get('name', None)
8679-
new_operation.metadata = api_response.get('metadata', None)
8680-
new_operation.done = api_response.get('done', None)
8681-
new_operation.error = api_response.get('error', None)
8682-
86838682
if is_vertex_ai:
8684-
if api_response.get('response', None) is not None:
8685-
new_operation.response = GenerateVideosResponse(
8686-
generated_videos=[
8687-
GeneratedVideo(
8688-
video=Video(
8689-
uri=video.get('gcsUri', None),
8690-
video_bytes=video.get('bytesBase64Encoded', None),
8691-
mime_type=video.get('mimeType', None),
8692-
)
8693-
)
8694-
for video in api_response.get('response', {}).get('videos', [])
8695-
],
8696-
rai_media_filtered_count=api_response.get('response', {}).get(
8697-
'raiMediaFilteredCount', None
8698-
),
8699-
rai_media_filtered_reasons=api_response.get('response', {}).get(
8700-
'raiMediaFilteredReasons', None
8701-
),
8702-
)
8683+
response_dict = _GenerateVideosOperation_from_vertex(api_response)
87038684
else:
8704-
if api_response.get('response', None) is not None:
8705-
new_operation.response = GenerateVideosResponse(
8706-
generated_videos=[
8707-
GeneratedVideo(
8708-
video=Video(
8709-
uri=video.get('video', {}).get('uri', None),
8710-
video_bytes=video.get('video', {}).get(
8711-
'encodedVideo', None
8712-
),
8713-
mime_type=video.get('encoding', None),
8714-
)
8715-
)
8716-
for video in api_response.get('response', {})
8717-
.get('generateVideoResponse', {})
8718-
.get('generatedSamples', [])
8719-
],
8720-
rai_media_filtered_count=api_response.get('response', {})
8721-
.get('generateVideoResponse', {})
8722-
.get('raiMediaFilteredCount', None),
8723-
rai_media_filtered_reasons=api_response.get('response', {})
8724-
.get('generateVideoResponse', {})
8725-
.get('raiMediaFilteredReasons', None),
8726-
)
8727-
new_operation.result = new_operation.response
8728-
return new_operation
8685+
response_dict = _GenerateVideosOperation_from_mldev(api_response)
8686+
8687+
return cls._from_response(response=response_dict, kwargs={})
87298688

87308689

87318690
class GetTuningJobConfig(_common.BaseModel):

0 commit comments

Comments
 (0)