google-genai 1.31.0__py3-none-any.whl → 1.33.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/_api_client.py +32 -9
- google/genai/_base_transformers.py +26 -0
- google/genai/_local_tokenizer_loader.py +223 -0
- google/genai/_operations_converters.py +307 -0
- google/genai/_replay_api_client.py +15 -0
- google/genai/_transformers.py +0 -10
- google/genai/caches.py +14 -2
- google/genai/files.py +12 -2
- google/genai/local_tokenizer.py +362 -0
- google/genai/models.py +171 -196
- google/genai/tunings.py +134 -0
- google/genai/types.py +402 -304
- google/genai/version.py +1 -1
- {google_genai-1.31.0.dist-info → google_genai-1.33.0.dist-info}/METADATA +6 -6
- {google_genai-1.31.0.dist-info → google_genai-1.33.0.dist-info}/RECORD +18 -14
- {google_genai-1.31.0.dist-info → google_genai-1.33.0.dist-info}/WHEEL +0 -0
- {google_genai-1.31.0.dist-info → google_genai-1.33.0.dist-info}/licenses/LICENSE +0 -0
- {google_genai-1.31.0.dist-info → google_genai-1.33.0.dist-info}/top_level.txt +0 -0
google/genai/_api_client.py
CHANGED
@@ -584,13 +584,9 @@ class BaseApiClient:
|
|
584
584
|
# Initialize the lock. This lock will be used to protect access to the
|
585
585
|
# credentials. This is crucial for thread safety when multiple coroutines
|
586
586
|
# might be accessing the credentials at the same time.
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
except RuntimeError:
|
591
|
-
asyncio.set_event_loop(asyncio.new_event_loop())
|
592
|
-
self._sync_auth_lock = threading.Lock()
|
593
|
-
self._async_auth_lock = asyncio.Lock()
|
587
|
+
self._sync_auth_lock = threading.Lock()
|
588
|
+
self._async_auth_lock: Optional[asyncio.Lock] = None
|
589
|
+
self._async_auth_lock_creation_lock: Optional[asyncio.Lock] = None
|
594
590
|
|
595
591
|
# Handle when to use Vertex AI in express mode (api key).
|
596
592
|
# Explicit initializer arguments are already validated above.
|
@@ -903,10 +899,36 @@ class BaseApiClient:
|
|
903
899
|
else:
|
904
900
|
raise RuntimeError('Could not resolve API token from the environment')
|
905
901
|
|
902
|
+
async def _get_async_auth_lock(self) -> asyncio.Lock:
|
903
|
+
"""Lazily initializes and returns an asyncio.Lock for async authentication.
|
904
|
+
|
905
|
+
This method ensures that a single `asyncio.Lock` instance is created and
|
906
|
+
shared among all asynchronous operations that require authentication,
|
907
|
+
preventing race conditions when accessing or refreshing credentials.
|
908
|
+
|
909
|
+
The lock is created on the first call to this method. An internal async lock
|
910
|
+
is used to protect the creation of the main authentication lock to ensure
|
911
|
+
it's a singleton within the client instance.
|
912
|
+
|
913
|
+
Returns:
|
914
|
+
The asyncio.Lock instance for asynchronous authentication operations.
|
915
|
+
"""
|
916
|
+
if self._async_auth_lock is None:
|
917
|
+
# Create async creation lock if needed
|
918
|
+
if self._async_auth_lock_creation_lock is None:
|
919
|
+
self._async_auth_lock_creation_lock = asyncio.Lock()
|
920
|
+
|
921
|
+
async with self._async_auth_lock_creation_lock:
|
922
|
+
if self._async_auth_lock is None:
|
923
|
+
self._async_auth_lock = asyncio.Lock()
|
924
|
+
|
925
|
+
return self._async_auth_lock
|
926
|
+
|
906
927
|
async def _async_access_token(self) -> Union[str, Any]:
|
907
928
|
"""Retrieves the access token for the credentials asynchronously."""
|
908
929
|
if not self._credentials:
|
909
|
-
|
930
|
+
async_auth_lock = await self._get_async_auth_lock()
|
931
|
+
async with async_auth_lock:
|
910
932
|
# This ensures that only one coroutine can execute the auth logic at a
|
911
933
|
# time for thread safety.
|
912
934
|
if not self._credentials:
|
@@ -920,7 +942,8 @@ class BaseApiClient:
|
|
920
942
|
if self._credentials:
|
921
943
|
if self._credentials.expired or not self._credentials.token:
|
922
944
|
# Only refresh when it needs to. Default expiration is 3600 seconds.
|
923
|
-
|
945
|
+
async_auth_lock = await self._get_async_auth_lock()
|
946
|
+
async with async_auth_lock:
|
924
947
|
if self._credentials.expired or not self._credentials.token:
|
925
948
|
# Double check that the credentials expired before refreshing.
|
926
949
|
await asyncio.to_thread(refresh_auth, self._credentials)
|
@@ -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')
|
@@ -0,0 +1,223 @@
|
|
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
|
+
import dataclasses
|
17
|
+
import functools
|
18
|
+
import hashlib
|
19
|
+
import os
|
20
|
+
import tempfile
|
21
|
+
from typing import Optional, cast
|
22
|
+
import uuid
|
23
|
+
|
24
|
+
import requests # type: ignore
|
25
|
+
import sentencepiece as spm
|
26
|
+
from sentencepiece import sentencepiece_model_pb2
|
27
|
+
|
28
|
+
|
29
|
+
# Source of truth: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models
|
30
|
+
_GEMINI_MODELS_TO_TOKENIZER_NAMES = {
|
31
|
+
"gemini-1.0-pro": "gemma2",
|
32
|
+
"gemini-1.5-pro": "gemma2",
|
33
|
+
"gemini-1.5-flash": "gemma2",
|
34
|
+
"gemini-2.5-pro": "gemma3",
|
35
|
+
"gemini-2.5-flash": "gemma3",
|
36
|
+
"gemini-2.5-flash-lite": "gemma3",
|
37
|
+
"gemini-2.0-flash": "gemma3",
|
38
|
+
"gemini-2.0-flash-lite": "gemma3",
|
39
|
+
}
|
40
|
+
_GEMINI_STABLE_MODELS_TO_TOKENIZER_NAMES = {
|
41
|
+
"gemini-1.0-pro-001": "gemma2",
|
42
|
+
"gemini-1.0-pro-002": "gemma2",
|
43
|
+
"gemini-1.5-pro-001": "gemma2",
|
44
|
+
"gemini-1.5-flash-001": "gemma2",
|
45
|
+
"gemini-1.5-flash-002": "gemma2",
|
46
|
+
"gemini-1.5-pro-002": "gemma2",
|
47
|
+
"gemini-2.5-pro-preview-06-05": "gemma3",
|
48
|
+
"gemini-2.5-pro-preview-05-06": "gemma3",
|
49
|
+
"gemini-2.5-pro-exp-03-25": "gemma3",
|
50
|
+
"gemini-live-2.5-flash": "gemma3",
|
51
|
+
"gemini-2.5-flash-preview-05-20": "gemma3",
|
52
|
+
"gemini-2.5-flash-preview-04-17": "gemma3",
|
53
|
+
"gemini-2.5-flash-lite-preview-06-17": "gemma3",
|
54
|
+
"gemini-2.0-flash-001": "gemma3",
|
55
|
+
"gemini-2.0-flash-lite-001": "gemma3",
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
@dataclasses.dataclass(frozen=True)
|
60
|
+
class _TokenizerConfig:
|
61
|
+
model_url: str
|
62
|
+
model_hash: str
|
63
|
+
|
64
|
+
|
65
|
+
# TODO: update gemma3 tokenizer
|
66
|
+
_TOKENIZERS = {
|
67
|
+
"gemma2": _TokenizerConfig(
|
68
|
+
model_url="https://raw.githubusercontent.com/google/gemma_pytorch/33b652c465537c6158f9a472ea5700e5e770ad3f/tokenizer/tokenizer.model",
|
69
|
+
model_hash=(
|
70
|
+
"61a7b147390c64585d6c3543dd6fc636906c9af3865a5548f27f31aee1d4c8e2"
|
71
|
+
),
|
72
|
+
),
|
73
|
+
"gemma3": _TokenizerConfig(
|
74
|
+
model_url="https://raw.githubusercontent.com/google/gemma_pytorch/014acb7ac4563a5f77c76d7ff98f31b568c16508/tokenizer/gemma3_cleaned_262144_v2.spiece.model",
|
75
|
+
model_hash=(
|
76
|
+
"1299c11d7cf632ef3b4e11937501358ada021bbdf7c47638d13c0ee982f2e79c"
|
77
|
+
),
|
78
|
+
),
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
def _load_file(file_url_path: str) -> bytes:
|
83
|
+
"""Loads file bytes from the given file url path."""
|
84
|
+
resp = requests.get(file_url_path)
|
85
|
+
resp.raise_for_status()
|
86
|
+
return cast(bytes, resp.content)
|
87
|
+
|
88
|
+
|
89
|
+
def _is_valid_model(*, model_data: bytes, expected_hash: str) -> bool:
|
90
|
+
"""Returns true if the content is valid by checking the hash."""
|
91
|
+
if not expected_hash:
|
92
|
+
raise ValueError("expected_hash is required")
|
93
|
+
return hashlib.sha256(model_data).hexdigest() == expected_hash
|
94
|
+
|
95
|
+
|
96
|
+
def _maybe_remove_file(file_path: str) -> None:
|
97
|
+
"""Removes the file if exists."""
|
98
|
+
if not os.path.exists(file_path):
|
99
|
+
return
|
100
|
+
try:
|
101
|
+
os.remove(file_path)
|
102
|
+
except OSError:
|
103
|
+
# Don't raise if we cannot remove file.
|
104
|
+
pass
|
105
|
+
|
106
|
+
|
107
|
+
def _maybe_load_from_cache(
|
108
|
+
*, file_path: str, expected_hash: str
|
109
|
+
) -> Optional[bytes]:
|
110
|
+
"""Loads the content from the cache path."""
|
111
|
+
if not os.path.exists(file_path):
|
112
|
+
return None
|
113
|
+
with open(file_path, "rb") as f:
|
114
|
+
content = f.read()
|
115
|
+
if _is_valid_model(model_data=content, expected_hash=expected_hash):
|
116
|
+
return content
|
117
|
+
|
118
|
+
# Cached file corrupted.
|
119
|
+
_maybe_remove_file(file_path)
|
120
|
+
return None
|
121
|
+
|
122
|
+
|
123
|
+
def _maybe_save_to_cache(
|
124
|
+
*, cache_dir: str, cache_path: str, content: bytes
|
125
|
+
) -> None:
|
126
|
+
"""Saves the content to the cache path."""
|
127
|
+
try:
|
128
|
+
os.makedirs(cache_dir, exist_ok=True)
|
129
|
+
tmp_path = cache_dir + "." + str(uuid.uuid4()) + ".tmp"
|
130
|
+
with open(tmp_path, "wb") as f:
|
131
|
+
f.write(content)
|
132
|
+
os.rename(tmp_path, cache_path)
|
133
|
+
except OSError:
|
134
|
+
# Don't raise if we cannot write file.
|
135
|
+
pass
|
136
|
+
|
137
|
+
|
138
|
+
def _load_from_url(*, file_url: str, expected_hash: str) -> bytes:
|
139
|
+
"""Loads model bytes from the given file url."""
|
140
|
+
content = _load_file(file_url)
|
141
|
+
if not _is_valid_model(model_data=content, expected_hash=expected_hash):
|
142
|
+
actual_hash = hashlib.sha256(content).hexdigest()
|
143
|
+
raise ValueError(
|
144
|
+
"Downloaded model file is corrupted."
|
145
|
+
f" Expected hash {expected_hash}. Got file hash {actual_hash}."
|
146
|
+
)
|
147
|
+
return content
|
148
|
+
|
149
|
+
|
150
|
+
def _load(*, file_url: str, expected_hash: str) -> bytes:
|
151
|
+
"""Loads model bytes from the given file url.
|
152
|
+
|
153
|
+
1. If the find local cached file for the given url and the cached file hash
|
154
|
+
matches the expected hash, the cached file is returned.
|
155
|
+
2. If local cached file is not found or the hash does not match, the file is
|
156
|
+
downloaded from the given url. And write to local cache and return the
|
157
|
+
file bytes.
|
158
|
+
3. If the file downloaded from the given url does not match the expected
|
159
|
+
hash, raise ValueError.
|
160
|
+
|
161
|
+
Args:
|
162
|
+
file_url: The url of the file to load.
|
163
|
+
expected_hash: The expected hash of the file.
|
164
|
+
|
165
|
+
Returns:
|
166
|
+
The file bytes.
|
167
|
+
"""
|
168
|
+
model_dir = os.path.join(tempfile.gettempdir(), "vertexai_tokenizer_model")
|
169
|
+
filename = hashlib.sha1(file_url.encode()).hexdigest()
|
170
|
+
model_path = os.path.join(model_dir, filename)
|
171
|
+
|
172
|
+
model_data = _maybe_load_from_cache(
|
173
|
+
file_path=model_path, expected_hash=expected_hash
|
174
|
+
)
|
175
|
+
if not model_data:
|
176
|
+
model_data = _load_from_url(file_url=file_url, expected_hash=expected_hash)
|
177
|
+
|
178
|
+
_maybe_save_to_cache(
|
179
|
+
cache_dir=model_dir, cache_path=model_path, content=model_data
|
180
|
+
)
|
181
|
+
return model_data
|
182
|
+
|
183
|
+
|
184
|
+
def _load_model_proto_bytes(tokenizer_name: str) -> bytes:
|
185
|
+
"""Loads model proto bytes from the given tokenizer name."""
|
186
|
+
if tokenizer_name not in _TOKENIZERS:
|
187
|
+
raise ValueError(
|
188
|
+
f"Tokenizer {tokenizer_name} is not supported."
|
189
|
+
f"Supported tokenizers: {list(_TOKENIZERS.keys())}"
|
190
|
+
)
|
191
|
+
return _load(
|
192
|
+
file_url=_TOKENIZERS[tokenizer_name].model_url,
|
193
|
+
expected_hash=_TOKENIZERS[tokenizer_name].model_hash,
|
194
|
+
)
|
195
|
+
|
196
|
+
|
197
|
+
@functools.lru_cache()
|
198
|
+
def load_model_proto(
|
199
|
+
tokenizer_name: str,
|
200
|
+
) -> sentencepiece_model_pb2.ModelProto:
|
201
|
+
"""Loads model proto from the given tokenizer name."""
|
202
|
+
model_proto = sentencepiece_model_pb2.ModelProto()
|
203
|
+
model_proto.ParseFromString(_load_model_proto_bytes(tokenizer_name))
|
204
|
+
return model_proto
|
205
|
+
|
206
|
+
|
207
|
+
def get_tokenizer_name(model_name: str) -> str:
|
208
|
+
"""Gets the tokenizer name for the given model name."""
|
209
|
+
if model_name in _GEMINI_MODELS_TO_TOKENIZER_NAMES.keys():
|
210
|
+
return _GEMINI_MODELS_TO_TOKENIZER_NAMES[model_name]
|
211
|
+
if model_name in _GEMINI_STABLE_MODELS_TO_TOKENIZER_NAMES.keys():
|
212
|
+
return _GEMINI_STABLE_MODELS_TO_TOKENIZER_NAMES[model_name]
|
213
|
+
raise ValueError(
|
214
|
+
f"Model {model_name} is not supported. Supported models: {', '.join(_GEMINI_MODELS_TO_TOKENIZER_NAMES.keys())}, {', '.join(_GEMINI_STABLE_MODELS_TO_TOKENIZER_NAMES.keys())}.\n" # pylint: disable=line-too-long
|
215
|
+
)
|
216
|
+
|
217
|
+
|
218
|
+
@functools.lru_cache()
|
219
|
+
def get_sentencepiece(tokenizer_name: str) -> spm.SentencePieceProcessor:
|
220
|
+
"""Loads sentencepiece tokenizer from the given tokenizer name."""
|
221
|
+
processor = spm.SentencePieceProcessor()
|
222
|
+
processor.LoadFromSerializedProto(_load_model_proto_bytes(tokenizer_name))
|
223
|
+
return processor
|
@@ -0,0 +1,307 @@
|
|
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
|
+
# Code generated by the Google Gen AI SDK generator DO NOT EDIT.
|
17
|
+
|
18
|
+
from typing import Any, Optional, Union
|
19
|
+
from . import _base_transformers as base_t
|
20
|
+
from ._common import get_value_by_path as getv
|
21
|
+
from ._common import set_value_by_path as setv
|
22
|
+
|
23
|
+
|
24
|
+
def _FetchPredictOperationParameters_to_mldev(
|
25
|
+
from_object: Union[dict[str, Any], object],
|
26
|
+
parent_object: Optional[dict[str, Any]] = None,
|
27
|
+
) -> dict[str, Any]:
|
28
|
+
to_object: dict[str, Any] = {}
|
29
|
+
if getv(from_object, ['operation_name']) is not None:
|
30
|
+
raise ValueError('operation_name parameter is not supported in Gemini API.')
|
31
|
+
|
32
|
+
if getv(from_object, ['resource_name']) is not None:
|
33
|
+
raise ValueError('resource_name parameter is not supported in Gemini API.')
|
34
|
+
|
35
|
+
if getv(from_object, ['config']) is not None:
|
36
|
+
raise ValueError('config parameter is not supported in Gemini API.')
|
37
|
+
|
38
|
+
return to_object
|
39
|
+
|
40
|
+
|
41
|
+
def _GetOperationParameters_to_mldev(
|
42
|
+
from_object: Union[dict[str, Any], object],
|
43
|
+
parent_object: Optional[dict[str, Any]] = None,
|
44
|
+
) -> dict[str, Any]:
|
45
|
+
to_object: dict[str, Any] = {}
|
46
|
+
if getv(from_object, ['operation_name']) is not None:
|
47
|
+
setv(
|
48
|
+
to_object,
|
49
|
+
['_url', 'operationName'],
|
50
|
+
getv(from_object, ['operation_name']),
|
51
|
+
)
|
52
|
+
|
53
|
+
if getv(from_object, ['config']) is not None:
|
54
|
+
setv(to_object, ['config'], getv(from_object, ['config']))
|
55
|
+
|
56
|
+
return to_object
|
57
|
+
|
58
|
+
|
59
|
+
def _FetchPredictOperationParameters_to_vertex(
|
60
|
+
from_object: Union[dict[str, Any], object],
|
61
|
+
parent_object: Optional[dict[str, Any]] = None,
|
62
|
+
) -> dict[str, Any]:
|
63
|
+
to_object: dict[str, Any] = {}
|
64
|
+
if getv(from_object, ['operation_name']) is not None:
|
65
|
+
setv(to_object, ['operationName'], getv(from_object, ['operation_name']))
|
66
|
+
|
67
|
+
if getv(from_object, ['resource_name']) is not None:
|
68
|
+
setv(
|
69
|
+
to_object,
|
70
|
+
['_url', 'resourceName'],
|
71
|
+
getv(from_object, ['resource_name']),
|
72
|
+
)
|
73
|
+
|
74
|
+
if getv(from_object, ['config']) is not None:
|
75
|
+
setv(to_object, ['config'], getv(from_object, ['config']))
|
76
|
+
|
77
|
+
return to_object
|
78
|
+
|
79
|
+
|
80
|
+
def _GetOperationParameters_to_vertex(
|
81
|
+
from_object: Union[dict[str, Any], object],
|
82
|
+
parent_object: Optional[dict[str, Any]] = None,
|
83
|
+
) -> dict[str, Any]:
|
84
|
+
to_object: dict[str, Any] = {}
|
85
|
+
if getv(from_object, ['operation_name']) is not None:
|
86
|
+
setv(
|
87
|
+
to_object,
|
88
|
+
['_url', 'operationName'],
|
89
|
+
getv(from_object, ['operation_name']),
|
90
|
+
)
|
91
|
+
|
92
|
+
if getv(from_object, ['config']) is not None:
|
93
|
+
setv(to_object, ['config'], getv(from_object, ['config']))
|
94
|
+
|
95
|
+
return to_object
|
96
|
+
|
97
|
+
|
98
|
+
def _Video_from_mldev(
|
99
|
+
from_object: Union[dict[str, Any], object],
|
100
|
+
parent_object: Optional[dict[str, Any]] = None,
|
101
|
+
) -> dict[str, Any]:
|
102
|
+
to_object: dict[str, Any] = {}
|
103
|
+
if getv(from_object, ['video', 'uri']) is not None:
|
104
|
+
setv(to_object, ['uri'], getv(from_object, ['video', 'uri']))
|
105
|
+
|
106
|
+
if getv(from_object, ['video', 'encodedVideo']) is not None:
|
107
|
+
setv(
|
108
|
+
to_object,
|
109
|
+
['video_bytes'],
|
110
|
+
base_t.t_bytes(getv(from_object, ['video', 'encodedVideo'])),
|
111
|
+
)
|
112
|
+
|
113
|
+
if getv(from_object, ['encoding']) is not None:
|
114
|
+
setv(to_object, ['mime_type'], getv(from_object, ['encoding']))
|
115
|
+
|
116
|
+
return to_object
|
117
|
+
|
118
|
+
|
119
|
+
def _GeneratedVideo_from_mldev(
|
120
|
+
from_object: Union[dict[str, Any], object],
|
121
|
+
parent_object: Optional[dict[str, Any]] = None,
|
122
|
+
) -> dict[str, Any]:
|
123
|
+
to_object: dict[str, Any] = {}
|
124
|
+
if getv(from_object, ['_self']) is not None:
|
125
|
+
setv(
|
126
|
+
to_object,
|
127
|
+
['video'],
|
128
|
+
_Video_from_mldev(getv(from_object, ['_self']), to_object),
|
129
|
+
)
|
130
|
+
|
131
|
+
return to_object
|
132
|
+
|
133
|
+
|
134
|
+
def _GenerateVideosResponse_from_mldev(
|
135
|
+
from_object: Union[dict[str, Any], object],
|
136
|
+
parent_object: Optional[dict[str, Any]] = None,
|
137
|
+
) -> dict[str, Any]:
|
138
|
+
to_object: dict[str, Any] = {}
|
139
|
+
if getv(from_object, ['generatedSamples']) is not None:
|
140
|
+
setv(
|
141
|
+
to_object,
|
142
|
+
['generated_videos'],
|
143
|
+
[
|
144
|
+
_GeneratedVideo_from_mldev(item, to_object)
|
145
|
+
for item in getv(from_object, ['generatedSamples'])
|
146
|
+
],
|
147
|
+
)
|
148
|
+
|
149
|
+
if getv(from_object, ['raiMediaFilteredCount']) is not None:
|
150
|
+
setv(
|
151
|
+
to_object,
|
152
|
+
['rai_media_filtered_count'],
|
153
|
+
getv(from_object, ['raiMediaFilteredCount']),
|
154
|
+
)
|
155
|
+
|
156
|
+
if getv(from_object, ['raiMediaFilteredReasons']) is not None:
|
157
|
+
setv(
|
158
|
+
to_object,
|
159
|
+
['rai_media_filtered_reasons'],
|
160
|
+
getv(from_object, ['raiMediaFilteredReasons']),
|
161
|
+
)
|
162
|
+
|
163
|
+
return to_object
|
164
|
+
|
165
|
+
|
166
|
+
def _GenerateVideosOperation_from_mldev(
|
167
|
+
from_object: Union[dict[str, Any], object],
|
168
|
+
parent_object: Optional[dict[str, Any]] = None,
|
169
|
+
) -> dict[str, Any]:
|
170
|
+
to_object: dict[str, Any] = {}
|
171
|
+
if getv(from_object, ['name']) is not None:
|
172
|
+
setv(to_object, ['name'], getv(from_object, ['name']))
|
173
|
+
|
174
|
+
if getv(from_object, ['metadata']) is not None:
|
175
|
+
setv(to_object, ['metadata'], getv(from_object, ['metadata']))
|
176
|
+
|
177
|
+
if getv(from_object, ['done']) is not None:
|
178
|
+
setv(to_object, ['done'], getv(from_object, ['done']))
|
179
|
+
|
180
|
+
if getv(from_object, ['error']) is not None:
|
181
|
+
setv(to_object, ['error'], getv(from_object, ['error']))
|
182
|
+
|
183
|
+
if getv(from_object, ['response', 'generateVideoResponse']) is not None:
|
184
|
+
setv(
|
185
|
+
to_object,
|
186
|
+
['response'],
|
187
|
+
_GenerateVideosResponse_from_mldev(
|
188
|
+
getv(from_object, ['response', 'generateVideoResponse']), to_object
|
189
|
+
),
|
190
|
+
)
|
191
|
+
|
192
|
+
if getv(from_object, ['response', 'generateVideoResponse']) is not None:
|
193
|
+
setv(
|
194
|
+
to_object,
|
195
|
+
['result'],
|
196
|
+
_GenerateVideosResponse_from_mldev(
|
197
|
+
getv(from_object, ['response', 'generateVideoResponse']), to_object
|
198
|
+
),
|
199
|
+
)
|
200
|
+
|
201
|
+
return to_object
|
202
|
+
|
203
|
+
|
204
|
+
def _Video_from_vertex(
|
205
|
+
from_object: Union[dict[str, Any], object],
|
206
|
+
parent_object: Optional[dict[str, Any]] = None,
|
207
|
+
) -> dict[str, Any]:
|
208
|
+
to_object: dict[str, Any] = {}
|
209
|
+
if getv(from_object, ['gcsUri']) is not None:
|
210
|
+
setv(to_object, ['uri'], getv(from_object, ['gcsUri']))
|
211
|
+
|
212
|
+
if getv(from_object, ['bytesBase64Encoded']) is not None:
|
213
|
+
setv(
|
214
|
+
to_object,
|
215
|
+
['video_bytes'],
|
216
|
+
base_t.t_bytes(getv(from_object, ['bytesBase64Encoded'])),
|
217
|
+
)
|
218
|
+
|
219
|
+
if getv(from_object, ['mimeType']) is not None:
|
220
|
+
setv(to_object, ['mime_type'], getv(from_object, ['mimeType']))
|
221
|
+
|
222
|
+
return to_object
|
223
|
+
|
224
|
+
|
225
|
+
def _GeneratedVideo_from_vertex(
|
226
|
+
from_object: Union[dict[str, Any], object],
|
227
|
+
parent_object: Optional[dict[str, Any]] = None,
|
228
|
+
) -> dict[str, Any]:
|
229
|
+
to_object: dict[str, Any] = {}
|
230
|
+
if getv(from_object, ['_self']) is not None:
|
231
|
+
setv(
|
232
|
+
to_object,
|
233
|
+
['video'],
|
234
|
+
_Video_from_vertex(getv(from_object, ['_self']), to_object),
|
235
|
+
)
|
236
|
+
|
237
|
+
return to_object
|
238
|
+
|
239
|
+
|
240
|
+
def _GenerateVideosResponse_from_vertex(
|
241
|
+
from_object: Union[dict[str, Any], object],
|
242
|
+
parent_object: Optional[dict[str, Any]] = None,
|
243
|
+
) -> dict[str, Any]:
|
244
|
+
to_object: dict[str, Any] = {}
|
245
|
+
if getv(from_object, ['videos']) is not None:
|
246
|
+
setv(
|
247
|
+
to_object,
|
248
|
+
['generated_videos'],
|
249
|
+
[
|
250
|
+
_GeneratedVideo_from_vertex(item, to_object)
|
251
|
+
for item in getv(from_object, ['videos'])
|
252
|
+
],
|
253
|
+
)
|
254
|
+
|
255
|
+
if getv(from_object, ['raiMediaFilteredCount']) is not None:
|
256
|
+
setv(
|
257
|
+
to_object,
|
258
|
+
['rai_media_filtered_count'],
|
259
|
+
getv(from_object, ['raiMediaFilteredCount']),
|
260
|
+
)
|
261
|
+
|
262
|
+
if getv(from_object, ['raiMediaFilteredReasons']) is not None:
|
263
|
+
setv(
|
264
|
+
to_object,
|
265
|
+
['rai_media_filtered_reasons'],
|
266
|
+
getv(from_object, ['raiMediaFilteredReasons']),
|
267
|
+
)
|
268
|
+
|
269
|
+
return to_object
|
270
|
+
|
271
|
+
|
272
|
+
def _GenerateVideosOperation_from_vertex(
|
273
|
+
from_object: Union[dict[str, Any], object],
|
274
|
+
parent_object: Optional[dict[str, Any]] = None,
|
275
|
+
) -> dict[str, Any]:
|
276
|
+
to_object: dict[str, Any] = {}
|
277
|
+
if getv(from_object, ['name']) is not None:
|
278
|
+
setv(to_object, ['name'], getv(from_object, ['name']))
|
279
|
+
|
280
|
+
if getv(from_object, ['metadata']) is not None:
|
281
|
+
setv(to_object, ['metadata'], getv(from_object, ['metadata']))
|
282
|
+
|
283
|
+
if getv(from_object, ['done']) is not None:
|
284
|
+
setv(to_object, ['done'], getv(from_object, ['done']))
|
285
|
+
|
286
|
+
if getv(from_object, ['error']) is not None:
|
287
|
+
setv(to_object, ['error'], getv(from_object, ['error']))
|
288
|
+
|
289
|
+
if getv(from_object, ['response']) is not None:
|
290
|
+
setv(
|
291
|
+
to_object,
|
292
|
+
['response'],
|
293
|
+
_GenerateVideosResponse_from_vertex(
|
294
|
+
getv(from_object, ['response']), to_object
|
295
|
+
),
|
296
|
+
)
|
297
|
+
|
298
|
+
if getv(from_object, ['response']) is not None:
|
299
|
+
setv(
|
300
|
+
to_object,
|
301
|
+
['result'],
|
302
|
+
_GenerateVideosResponse_from_vertex(
|
303
|
+
getv(from_object, ['response']), to_object
|
304
|
+
),
|
305
|
+
)
|
306
|
+
|
307
|
+
return to_object
|
@@ -471,6 +471,21 @@ class ReplayApiClient(BaseApiClient):
|
|
471
471
|
expected = interaction.response.sdk_response_segments[
|
472
472
|
self._sdk_response_index
|
473
473
|
]
|
474
|
+
# The sdk_http_response.body has format in the string, need to get rid of
|
475
|
+
# the format information before comparing.
|
476
|
+
if isinstance(expected, dict):
|
477
|
+
if 'sdk_http_response' in expected and isinstance(
|
478
|
+
expected['sdk_http_response'], dict
|
479
|
+
):
|
480
|
+
if 'body' in expected['sdk_http_response']:
|
481
|
+
raw_body = expected['sdk_http_response']['body']
|
482
|
+
print('raw_body length: ', len(raw_body))
|
483
|
+
print('raw_body: ', raw_body)
|
484
|
+
if isinstance(raw_body, str) and raw_body != '':
|
485
|
+
raw_body = json.loads(raw_body)
|
486
|
+
raw_body = json.dumps(raw_body)
|
487
|
+
expected['sdk_http_response']['body'] = raw_body
|
488
|
+
|
474
489
|
assert (
|
475
490
|
actual == expected
|
476
491
|
), f'SDK response mismatch:\nActual: {actual}\nExpected: {expected}'
|
google/genai/_transformers.py
CHANGED
@@ -1156,16 +1156,6 @@ def t_tuning_job_status(status: str) -> Union[types.JobState, str]:
|
|
1156
1156
|
return status
|
1157
1157
|
|
1158
1158
|
|
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
|
-
|
1169
1159
|
def t_content_strict(content: types.ContentOrDict) -> types.Content:
|
1170
1160
|
if isinstance(content, dict):
|
1171
1161
|
return types.Content.model_validate(content)
|