google-genai 1.37.0__py3-none-any.whl → 1.39.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/client.py CHANGED
@@ -14,6 +14,7 @@
14
14
  #
15
15
 
16
16
  import os
17
+ from types import TracebackType
17
18
  from typing import Optional, Union
18
19
 
19
20
  import google.auth
@@ -85,6 +86,51 @@ class AsyncClient:
85
86
  def operations(self) -> AsyncOperations:
86
87
  return self._operations
87
88
 
89
+ async def aclose(self) -> None:
90
+ """Closes the async client explicitly.
91
+
92
+ However, it doesn't close the sync client, which can be closed using the
93
+ Client.close() method or using the context manager.
94
+
95
+ Usage:
96
+ .. code-block:: python
97
+
98
+ from google.genai import Client
99
+
100
+ async_client = Client(
101
+ vertexai=True, project='my-project-id', location='us-central1'
102
+ ).aio
103
+ response_1 = await async_client.models.generate_content(
104
+ model='gemini-2.0-flash',
105
+ contents='Hello World',
106
+ )
107
+ response_2 = await async_client.models.generate_content(
108
+ model='gemini-2.0-flash',
109
+ contents='Hello World',
110
+ )
111
+ # Close the client to release resources.
112
+ await async_client.aclose()
113
+ """
114
+ await self._api_client.aclose()
115
+
116
+ async def __aenter__(self) -> 'AsyncClient':
117
+ return self
118
+
119
+ async def __aexit__(
120
+ self,
121
+ exc_type: Optional[Exception],
122
+ exc_value: Optional[Exception],
123
+ traceback: Optional[TracebackType],
124
+ ) -> None:
125
+ await self.aclose()
126
+
127
+ def __del__(self) -> None:
128
+ import asyncio
129
+ try:
130
+ asyncio.get_running_loop().create_task(self.aclose())
131
+ except Exception:
132
+ pass
133
+
88
134
 
89
135
  class DebugConfig(pydantic.BaseModel):
90
136
  """Configuration options that change client network behavior when testing."""
@@ -311,3 +357,44 @@ class Client:
311
357
  def vertexai(self) -> bool:
312
358
  """Returns whether the client is using the Vertex AI API."""
313
359
  return self._api_client.vertexai or False
360
+
361
+ def close(self) -> None:
362
+ """Closes the synchronous client explicitly.
363
+
364
+ However, it doesn't close the async client, which can be closed using the
365
+ Client.aio.aclose() method or using the async context manager.
366
+
367
+ Usage:
368
+ .. code-block:: python
369
+
370
+ from google.genai import Client
371
+
372
+ client = Client(
373
+ vertexai=True, project='my-project-id', location='us-central1'
374
+ )
375
+ response_1 = client.models.generate_content(
376
+ model='gemini-2.0-flash',
377
+ contents='Hello World',
378
+ )
379
+ response_2 = client.models.generate_content(
380
+ model='gemini-2.0-flash',
381
+ contents='Hello World',
382
+ )
383
+ # Close the client to release resources.
384
+ client.close()
385
+ """
386
+ self._api_client.close()
387
+
388
+ def __enter__(self) -> 'Client':
389
+ return self
390
+
391
+ def __exit__(
392
+ self,
393
+ exc_type: Optional[Exception],
394
+ exc_value: Optional[Exception],
395
+ traceback: Optional[TracebackType],
396
+ ) -> None:
397
+ self.close()
398
+
399
+ def __del__(self) -> None:
400
+ self.close()
google/genai/files.py CHANGED
@@ -35,164 +35,78 @@ from .pagers import AsyncPager, Pager
35
35
  logger = logging.getLogger('google_genai.files')
36
36
 
37
37
 
38
- def _ListFilesConfig_to_mldev(
38
+ def _CreateFileParameters_to_mldev(
39
39
  from_object: Union[dict[str, Any], object],
40
40
  parent_object: Optional[dict[str, Any]] = None,
41
41
  ) -> dict[str, Any]:
42
42
  to_object: dict[str, Any] = {}
43
-
44
- if getv(from_object, ['page_size']) is not None:
45
- setv(
46
- parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
47
- )
48
-
49
- if getv(from_object, ['page_token']) is not None:
43
+ if getv(from_object, ['file']) is not None:
50
44
  setv(
51
- parent_object,
52
- ['_query', 'pageToken'],
53
- getv(from_object, ['page_token']),
45
+ to_object,
46
+ ['file'],
47
+ _File_to_mldev(getv(from_object, ['file']), to_object),
54
48
  )
55
49
 
56
50
  return to_object
57
51
 
58
52
 
59
- def _ListFilesParameters_to_mldev(
53
+ def _CreateFileResponse_from_mldev(
60
54
  from_object: Union[dict[str, Any], object],
61
55
  parent_object: Optional[dict[str, Any]] = None,
62
56
  ) -> dict[str, Any]:
63
57
  to_object: dict[str, Any] = {}
64
- if getv(from_object, ['config']) is not None:
58
+ if getv(from_object, ['sdkHttpResponse']) is not None:
65
59
  setv(
66
- to_object,
67
- ['config'],
68
- _ListFilesConfig_to_mldev(getv(from_object, ['config']), to_object),
60
+ to_object, ['sdk_http_response'], getv(from_object, ['sdkHttpResponse'])
69
61
  )
70
62
 
71
63
  return to_object
72
64
 
73
65
 
74
- def _FileStatus_to_mldev(
75
- from_object: Union[dict[str, Any], object],
76
- parent_object: Optional[dict[str, Any]] = None,
77
- ) -> dict[str, Any]:
78
- to_object: dict[str, Any] = {}
79
- if getv(from_object, ['details']) is not None:
80
- setv(to_object, ['details'], getv(from_object, ['details']))
81
-
82
- if getv(from_object, ['message']) is not None:
83
- setv(to_object, ['message'], getv(from_object, ['message']))
84
-
85
- if getv(from_object, ['code']) is not None:
86
- setv(to_object, ['code'], getv(from_object, ['code']))
87
-
88
- return to_object
89
-
90
-
91
- def _File_to_mldev(
66
+ def _DeleteFileParameters_to_mldev(
92
67
  from_object: Union[dict[str, Any], object],
93
68
  parent_object: Optional[dict[str, Any]] = None,
94
69
  ) -> dict[str, Any]:
95
70
  to_object: dict[str, Any] = {}
96
71
  if getv(from_object, ['name']) is not None:
97
- setv(to_object, ['name'], getv(from_object, ['name']))
98
-
99
- if getv(from_object, ['display_name']) is not None:
100
- setv(to_object, ['displayName'], getv(from_object, ['display_name']))
101
-
102
- if getv(from_object, ['mime_type']) is not None:
103
- setv(to_object, ['mimeType'], getv(from_object, ['mime_type']))
104
-
105
- if getv(from_object, ['size_bytes']) is not None:
106
- setv(to_object, ['sizeBytes'], getv(from_object, ['size_bytes']))
107
-
108
- if getv(from_object, ['create_time']) is not None:
109
- setv(to_object, ['createTime'], getv(from_object, ['create_time']))
110
-
111
- if getv(from_object, ['expiration_time']) is not None:
112
- setv(to_object, ['expirationTime'], getv(from_object, ['expiration_time']))
113
-
114
- if getv(from_object, ['update_time']) is not None:
115
- setv(to_object, ['updateTime'], getv(from_object, ['update_time']))
116
-
117
- if getv(from_object, ['sha256_hash']) is not None:
118
- setv(to_object, ['sha256Hash'], getv(from_object, ['sha256_hash']))
119
-
120
- if getv(from_object, ['uri']) is not None:
121
- setv(to_object, ['uri'], getv(from_object, ['uri']))
122
-
123
- if getv(from_object, ['download_uri']) is not None:
124
- setv(to_object, ['downloadUri'], getv(from_object, ['download_uri']))
125
-
126
- if getv(from_object, ['state']) is not None:
127
- setv(to_object, ['state'], getv(from_object, ['state']))
128
-
129
- if getv(from_object, ['source']) is not None:
130
- setv(to_object, ['source'], getv(from_object, ['source']))
131
-
132
- if getv(from_object, ['video_metadata']) is not None:
133
- setv(to_object, ['videoMetadata'], getv(from_object, ['video_metadata']))
134
-
135
- if getv(from_object, ['error']) is not None:
136
72
  setv(
137
- to_object,
138
- ['error'],
139
- _FileStatus_to_mldev(getv(from_object, ['error']), to_object),
73
+ to_object, ['_url', 'file'], t.t_file_name(getv(from_object, ['name']))
140
74
  )
141
75
 
142
76
  return to_object
143
77
 
144
78
 
145
- def _CreateFileParameters_to_mldev(
79
+ def _DeleteFileResponse_from_mldev(
146
80
  from_object: Union[dict[str, Any], object],
147
81
  parent_object: Optional[dict[str, Any]] = None,
148
82
  ) -> dict[str, Any]:
149
83
  to_object: dict[str, Any] = {}
150
- if getv(from_object, ['file']) is not None:
84
+ if getv(from_object, ['sdkHttpResponse']) is not None:
151
85
  setv(
152
- to_object,
153
- ['file'],
154
- _File_to_mldev(getv(from_object, ['file']), to_object),
86
+ to_object, ['sdk_http_response'], getv(from_object, ['sdkHttpResponse'])
155
87
  )
156
88
 
157
- if getv(from_object, ['config']) is not None:
158
- setv(to_object, ['config'], getv(from_object, ['config']))
159
-
160
89
  return to_object
161
90
 
162
91
 
163
- def _GetFileParameters_to_mldev(
92
+ def _FileStatus_from_mldev(
164
93
  from_object: Union[dict[str, Any], object],
165
94
  parent_object: Optional[dict[str, Any]] = None,
166
95
  ) -> dict[str, Any]:
167
96
  to_object: dict[str, Any] = {}
168
- if getv(from_object, ['name']) is not None:
169
- setv(
170
- to_object, ['_url', 'file'], t.t_file_name(getv(from_object, ['name']))
171
- )
172
-
173
- if getv(from_object, ['config']) is not None:
174
- setv(to_object, ['config'], getv(from_object, ['config']))
175
-
176
- return to_object
177
-
97
+ if getv(from_object, ['details']) is not None:
98
+ setv(to_object, ['details'], getv(from_object, ['details']))
178
99
 
179
- def _DeleteFileParameters_to_mldev(
180
- from_object: Union[dict[str, Any], object],
181
- parent_object: Optional[dict[str, Any]] = None,
182
- ) -> dict[str, Any]:
183
- to_object: dict[str, Any] = {}
184
- if getv(from_object, ['name']) is not None:
185
- setv(
186
- to_object, ['_url', 'file'], t.t_file_name(getv(from_object, ['name']))
187
- )
100
+ if getv(from_object, ['message']) is not None:
101
+ setv(to_object, ['message'], getv(from_object, ['message']))
188
102
 
189
- if getv(from_object, ['config']) is not None:
190
- setv(to_object, ['config'], getv(from_object, ['config']))
103
+ if getv(from_object, ['code']) is not None:
104
+ setv(to_object, ['code'], getv(from_object, ['code']))
191
105
 
192
106
  return to_object
193
107
 
194
108
 
195
- def _FileStatus_from_mldev(
109
+ def _FileStatus_to_mldev(
196
110
  from_object: Union[dict[str, Any], object],
197
111
  parent_object: Optional[dict[str, Any]] = None,
198
112
  ) -> dict[str, Any]:
@@ -263,46 +177,106 @@ def _File_from_mldev(
263
177
  return to_object
264
178
 
265
179
 
266
- def _ListFilesResponse_from_mldev(
180
+ def _File_to_mldev(
267
181
  from_object: Union[dict[str, Any], object],
268
182
  parent_object: Optional[dict[str, Any]] = None,
269
183
  ) -> dict[str, Any]:
270
184
  to_object: dict[str, Any] = {}
271
- if getv(from_object, ['sdkHttpResponse']) is not None:
185
+ if getv(from_object, ['name']) is not None:
186
+ setv(to_object, ['name'], getv(from_object, ['name']))
187
+
188
+ if getv(from_object, ['display_name']) is not None:
189
+ setv(to_object, ['displayName'], getv(from_object, ['display_name']))
190
+
191
+ if getv(from_object, ['mime_type']) is not None:
192
+ setv(to_object, ['mimeType'], getv(from_object, ['mime_type']))
193
+
194
+ if getv(from_object, ['size_bytes']) is not None:
195
+ setv(to_object, ['sizeBytes'], getv(from_object, ['size_bytes']))
196
+
197
+ if getv(from_object, ['create_time']) is not None:
198
+ setv(to_object, ['createTime'], getv(from_object, ['create_time']))
199
+
200
+ if getv(from_object, ['expiration_time']) is not None:
201
+ setv(to_object, ['expirationTime'], getv(from_object, ['expiration_time']))
202
+
203
+ if getv(from_object, ['update_time']) is not None:
204
+ setv(to_object, ['updateTime'], getv(from_object, ['update_time']))
205
+
206
+ if getv(from_object, ['sha256_hash']) is not None:
207
+ setv(to_object, ['sha256Hash'], getv(from_object, ['sha256_hash']))
208
+
209
+ if getv(from_object, ['uri']) is not None:
210
+ setv(to_object, ['uri'], getv(from_object, ['uri']))
211
+
212
+ if getv(from_object, ['download_uri']) is not None:
213
+ setv(to_object, ['downloadUri'], getv(from_object, ['download_uri']))
214
+
215
+ if getv(from_object, ['state']) is not None:
216
+ setv(to_object, ['state'], getv(from_object, ['state']))
217
+
218
+ if getv(from_object, ['source']) is not None:
219
+ setv(to_object, ['source'], getv(from_object, ['source']))
220
+
221
+ if getv(from_object, ['video_metadata']) is not None:
222
+ setv(to_object, ['videoMetadata'], getv(from_object, ['video_metadata']))
223
+
224
+ if getv(from_object, ['error']) is not None:
272
225
  setv(
273
- to_object, ['sdk_http_response'], getv(from_object, ['sdkHttpResponse'])
226
+ to_object,
227
+ ['error'],
228
+ _FileStatus_to_mldev(getv(from_object, ['error']), to_object),
274
229
  )
275
230
 
276
- if getv(from_object, ['nextPageToken']) is not None:
277
- setv(to_object, ['next_page_token'], getv(from_object, ['nextPageToken']))
231
+ return to_object
278
232
 
279
- if getv(from_object, ['files']) is not None:
233
+
234
+ def _GetFileParameters_to_mldev(
235
+ from_object: Union[dict[str, Any], object],
236
+ parent_object: Optional[dict[str, Any]] = None,
237
+ ) -> dict[str, Any]:
238
+ to_object: dict[str, Any] = {}
239
+ if getv(from_object, ['name']) is not None:
280
240
  setv(
281
- to_object,
282
- ['files'],
283
- [
284
- _File_from_mldev(item, to_object)
285
- for item in getv(from_object, ['files'])
286
- ],
241
+ to_object, ['_url', 'file'], t.t_file_name(getv(from_object, ['name']))
287
242
  )
288
243
 
289
244
  return to_object
290
245
 
291
246
 
292
- def _CreateFileResponse_from_mldev(
247
+ def _ListFilesConfig_to_mldev(
293
248
  from_object: Union[dict[str, Any], object],
294
249
  parent_object: Optional[dict[str, Any]] = None,
295
250
  ) -> dict[str, Any]:
296
251
  to_object: dict[str, Any] = {}
297
- if getv(from_object, ['sdkHttpResponse']) is not None:
252
+
253
+ if getv(from_object, ['page_size']) is not None:
298
254
  setv(
299
- to_object, ['sdk_http_response'], getv(from_object, ['sdkHttpResponse'])
255
+ parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
256
+ )
257
+
258
+ if getv(from_object, ['page_token']) is not None:
259
+ setv(
260
+ parent_object,
261
+ ['_query', 'pageToken'],
262
+ getv(from_object, ['page_token']),
300
263
  )
301
264
 
302
265
  return to_object
303
266
 
304
267
 
305
- def _DeleteFileResponse_from_mldev(
268
+ def _ListFilesParameters_to_mldev(
269
+ from_object: Union[dict[str, Any], object],
270
+ parent_object: Optional[dict[str, Any]] = None,
271
+ ) -> dict[str, Any]:
272
+ to_object: dict[str, Any] = {}
273
+ if getv(from_object, ['config']) is not None:
274
+ _ListFilesConfig_to_mldev(getv(from_object, ['config']), to_object)
275
+
276
+ return to_object
277
+
278
+
279
+ def _ListFilesResponse_from_mldev(
306
280
  from_object: Union[dict[str, Any], object],
307
281
  parent_object: Optional[dict[str, Any]] = None,
308
282
  ) -> dict[str, Any]:
@@ -312,6 +286,19 @@ def _DeleteFileResponse_from_mldev(
312
286
  to_object, ['sdk_http_response'], getv(from_object, ['sdkHttpResponse'])
313
287
  )
314
288
 
289
+ if getv(from_object, ['nextPageToken']) is not None:
290
+ setv(to_object, ['next_page_token'], getv(from_object, ['nextPageToken']))
291
+
292
+ if getv(from_object, ['files']) is not None:
293
+ setv(
294
+ to_object,
295
+ ['files'],
296
+ [
297
+ _File_from_mldev(item, to_object)
298
+ for item in getv(from_object, ['files'])
299
+ ],
300
+ )
301
+
315
302
  return to_object
316
303
 
317
304
 
@@ -25,11 +25,16 @@ from . import _common
25
25
  from . import _local_tokenizer_loader as loader
26
26
  from . import _transformers as t
27
27
  from . import types
28
- from . import types
29
- from ._transformers import t_contents
30
28
 
31
29
  logger = logging.getLogger("google_genai.local_tokenizer")
32
30
 
31
+ __all__ = [
32
+ "_parse_hex_byte",
33
+ "_token_str_to_bytes",
34
+ "LocalTokenizer",
35
+ "_TextsAccumulator",
36
+ ]
37
+
33
38
 
34
39
  class _TextsAccumulator:
35
40
  """Accumulates countable texts from `Content` and `Tool` objects.