google-genai 1.47.0__py3-none-any.whl → 1.49.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.
@@ -0,0 +1,549 @@
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
+ import json
19
+ import logging
20
+ from typing import Any, Optional, Union
21
+ from urllib.parse import urlencode
22
+
23
+ from . import _api_module
24
+ from . import _common
25
+ from . import types
26
+ from ._common import get_value_by_path as getv
27
+ from ._common import set_value_by_path as setv
28
+ from .pagers import AsyncPager, Pager
29
+
30
+
31
+ logger = logging.getLogger('google_genai.documents')
32
+
33
+
34
+ def _DeleteDocumentConfig_to_mldev(
35
+ from_object: Union[dict[str, Any], object],
36
+ parent_object: Optional[dict[str, Any]] = None,
37
+ ) -> dict[str, Any]:
38
+ to_object: dict[str, Any] = {}
39
+
40
+ if getv(from_object, ['force']) is not None:
41
+ setv(parent_object, ['_query', 'force'], getv(from_object, ['force']))
42
+
43
+ return to_object
44
+
45
+
46
+ def _DeleteDocumentParameters_to_mldev(
47
+ from_object: Union[dict[str, Any], object],
48
+ parent_object: Optional[dict[str, Any]] = None,
49
+ ) -> dict[str, Any]:
50
+ to_object: dict[str, Any] = {}
51
+ if getv(from_object, ['name']) is not None:
52
+ setv(to_object, ['_url', 'name'], getv(from_object, ['name']))
53
+
54
+ if getv(from_object, ['config']) is not None:
55
+ _DeleteDocumentConfig_to_mldev(getv(from_object, ['config']), to_object)
56
+
57
+ return to_object
58
+
59
+
60
+ def _GetDocumentParameters_to_mldev(
61
+ from_object: Union[dict[str, Any], object],
62
+ parent_object: Optional[dict[str, Any]] = None,
63
+ ) -> dict[str, Any]:
64
+ to_object: dict[str, Any] = {}
65
+ if getv(from_object, ['name']) is not None:
66
+ setv(to_object, ['_url', 'name'], getv(from_object, ['name']))
67
+
68
+ return to_object
69
+
70
+
71
+ def _ListDocumentsConfig_to_mldev(
72
+ from_object: Union[dict[str, Any], object],
73
+ parent_object: Optional[dict[str, Any]] = None,
74
+ ) -> dict[str, Any]:
75
+ to_object: dict[str, Any] = {}
76
+
77
+ if getv(from_object, ['page_size']) is not None:
78
+ setv(
79
+ parent_object, ['_query', 'pageSize'], getv(from_object, ['page_size'])
80
+ )
81
+
82
+ if getv(from_object, ['page_token']) is not None:
83
+ setv(
84
+ parent_object,
85
+ ['_query', 'pageToken'],
86
+ getv(from_object, ['page_token']),
87
+ )
88
+
89
+ return to_object
90
+
91
+
92
+ def _ListDocumentsParameters_to_mldev(
93
+ from_object: Union[dict[str, Any], object],
94
+ parent_object: Optional[dict[str, Any]] = None,
95
+ ) -> dict[str, Any]:
96
+ to_object: dict[str, Any] = {}
97
+ if getv(from_object, ['parent']) is not None:
98
+ setv(to_object, ['_url', 'parent'], getv(from_object, ['parent']))
99
+
100
+ if getv(from_object, ['config']) is not None:
101
+ _ListDocumentsConfig_to_mldev(getv(from_object, ['config']), to_object)
102
+
103
+ return to_object
104
+
105
+
106
+ def _ListDocumentsResponse_from_mldev(
107
+ from_object: Union[dict[str, Any], object],
108
+ parent_object: Optional[dict[str, Any]] = None,
109
+ ) -> dict[str, Any]:
110
+ to_object: dict[str, Any] = {}
111
+ if getv(from_object, ['sdkHttpResponse']) is not None:
112
+ setv(
113
+ to_object, ['sdk_http_response'], getv(from_object, ['sdkHttpResponse'])
114
+ )
115
+
116
+ if getv(from_object, ['nextPageToken']) is not None:
117
+ setv(to_object, ['next_page_token'], getv(from_object, ['nextPageToken']))
118
+
119
+ if getv(from_object, ['documents']) is not None:
120
+ setv(
121
+ to_object,
122
+ ['documents'],
123
+ [item for item in getv(from_object, ['documents'])],
124
+ )
125
+
126
+ return to_object
127
+
128
+
129
+ class Documents(_api_module.BaseModule):
130
+
131
+ def get(
132
+ self, *, name: str, config: Optional[types.GetDocumentConfigOrDict] = None
133
+ ) -> types.Document:
134
+ """Gets metadata about a Document.
135
+
136
+ Args:
137
+ name (str): The resource name of the Document.
138
+ Example: ragStores/rag-store-foo/documents/documents-bar
139
+ config (GetDocumentConfig | None): Optional parameters for the request.
140
+
141
+ Returns:
142
+ The Document.
143
+ """
144
+
145
+ parameter_model = types._GetDocumentParameters(
146
+ name=name,
147
+ config=config,
148
+ )
149
+
150
+ request_url_dict: Optional[dict[str, str]]
151
+ if self._api_client.vertexai:
152
+ raise ValueError(
153
+ 'This method is only supported in the Gemini Developer client.'
154
+ )
155
+ else:
156
+ request_dict = _GetDocumentParameters_to_mldev(parameter_model)
157
+ request_url_dict = request_dict.get('_url')
158
+ if request_url_dict:
159
+ path = '{name}'.format_map(request_url_dict)
160
+ else:
161
+ path = '{name}'
162
+
163
+ query_params = request_dict.get('_query')
164
+ if query_params:
165
+ path = f'{path}?{urlencode(query_params)}'
166
+ # TODO: remove the hack that pops config.
167
+ request_dict.pop('config', None)
168
+
169
+ http_options: Optional[types.HttpOptions] = None
170
+ if (
171
+ parameter_model.config is not None
172
+ and parameter_model.config.http_options is not None
173
+ ):
174
+ http_options = parameter_model.config.http_options
175
+
176
+ request_dict = _common.convert_to_dict(request_dict)
177
+ request_dict = _common.encode_unserializable_types(request_dict)
178
+
179
+ response = self._api_client.request('get', path, request_dict, http_options)
180
+
181
+ response_dict = {} if not response.body else json.loads(response.body)
182
+
183
+ return_value = types.Document._from_response(
184
+ response=response_dict, kwargs=parameter_model.model_dump()
185
+ )
186
+
187
+ self._api_client._verify_response(return_value)
188
+ return return_value
189
+
190
+ def delete(
191
+ self,
192
+ *,
193
+ name: str,
194
+ config: Optional[types.DeleteDocumentConfigOrDict] = None,
195
+ ) -> None:
196
+ """Deletes a Document.
197
+
198
+ Args:
199
+ name (str): The resource name of the Document.
200
+ Example: ragStores/rag-store-foo/documents/documents-bar
201
+ config (DeleteDocumentConfig | None): Optional parameters for the request.
202
+
203
+ Returns:
204
+ None
205
+ """
206
+
207
+ parameter_model = types._DeleteDocumentParameters(
208
+ name=name,
209
+ config=config,
210
+ )
211
+
212
+ request_url_dict: Optional[dict[str, str]]
213
+ if self._api_client.vertexai:
214
+ raise ValueError(
215
+ 'This method is only supported in the Gemini Developer client.'
216
+ )
217
+ else:
218
+ request_dict = _DeleteDocumentParameters_to_mldev(parameter_model)
219
+ request_url_dict = request_dict.get('_url')
220
+ if request_url_dict:
221
+ path = '{name}'.format_map(request_url_dict)
222
+ else:
223
+ path = '{name}'
224
+
225
+ query_params = request_dict.get('_query')
226
+ if query_params:
227
+ path = f'{path}?{urlencode(query_params)}'
228
+ # TODO: remove the hack that pops config.
229
+ request_dict.pop('config', None)
230
+
231
+ http_options: Optional[types.HttpOptions] = None
232
+ if (
233
+ parameter_model.config is not None
234
+ and parameter_model.config.http_options is not None
235
+ ):
236
+ http_options = parameter_model.config.http_options
237
+
238
+ request_dict = _common.convert_to_dict(request_dict)
239
+ request_dict = _common.encode_unserializable_types(request_dict)
240
+
241
+ response = self._api_client.request(
242
+ 'delete', path, request_dict, http_options
243
+ )
244
+
245
+ def _list(
246
+ self,
247
+ *,
248
+ parent: str,
249
+ config: Optional[types.ListDocumentsConfigOrDict] = None,
250
+ ) -> types.ListDocumentsResponse:
251
+ """Lists all Documents in a FileSearchStore.
252
+
253
+ Args:
254
+ parent (str): The name of the FileSearchStore containing the Documents.
255
+ config (ListDocumentsConfig | None): Optional parameters for the request,
256
+ such as page_size.
257
+
258
+ Returns:
259
+ ListDocumentsResponse: A paginated list of Documents.
260
+ """
261
+
262
+ parameter_model = types._ListDocumentsParameters(
263
+ parent=parent,
264
+ config=config,
265
+ )
266
+
267
+ request_url_dict: Optional[dict[str, str]]
268
+ if self._api_client.vertexai:
269
+ raise ValueError(
270
+ 'This method is only supported in the Gemini Developer client.'
271
+ )
272
+ else:
273
+ request_dict = _ListDocumentsParameters_to_mldev(parameter_model)
274
+ request_url_dict = request_dict.get('_url')
275
+ if request_url_dict:
276
+ path = '{parent}/documents'.format_map(request_url_dict)
277
+ else:
278
+ path = '{parent}/documents'
279
+
280
+ query_params = request_dict.get('_query')
281
+ if query_params:
282
+ path = f'{path}?{urlencode(query_params)}'
283
+ # TODO: remove the hack that pops config.
284
+ request_dict.pop('config', None)
285
+
286
+ http_options: Optional[types.HttpOptions] = None
287
+ if (
288
+ parameter_model.config is not None
289
+ and parameter_model.config.http_options is not None
290
+ ):
291
+ http_options = parameter_model.config.http_options
292
+
293
+ request_dict = _common.convert_to_dict(request_dict)
294
+ request_dict = _common.encode_unserializable_types(request_dict)
295
+
296
+ response = self._api_client.request('get', path, request_dict, http_options)
297
+
298
+ response_dict = {} if not response.body else json.loads(response.body)
299
+
300
+ if not self._api_client.vertexai:
301
+ response_dict = _ListDocumentsResponse_from_mldev(response_dict)
302
+
303
+ return_value = types.ListDocumentsResponse._from_response(
304
+ response=response_dict, kwargs=parameter_model.model_dump()
305
+ )
306
+
307
+ self._api_client._verify_response(return_value)
308
+ return return_value
309
+
310
+ def list(
311
+ self,
312
+ *,
313
+ parent: str,
314
+ config: Optional[types.ListDocumentsConfigOrDict] = None,
315
+ ) -> Pager[types.Document]:
316
+ """Lists documents.
317
+
318
+ Args:
319
+ parent (str): The name of the RagStore containing the Documents.
320
+ config (ListDocumentsConfig): Optional configuration for the list request.
321
+
322
+ Returns:
323
+ A Pager object that contains one page of documents. When iterating over
324
+ the pager, it automatically fetches the next page if there are more.
325
+ Usage:
326
+ .. code-block:: python
327
+ for document in client.documents.list(parent='rag_store_name'):
328
+ print(f"document: {document.name} - {document.display_name}")
329
+ """
330
+ return Pager(
331
+ 'documents',
332
+ self._list,
333
+ self._list(parent=parent, config=config),
334
+ config,
335
+ )
336
+
337
+
338
+ class AsyncDocuments(_api_module.BaseModule):
339
+
340
+ async def get(
341
+ self, *, name: str, config: Optional[types.GetDocumentConfigOrDict] = None
342
+ ) -> types.Document:
343
+ """Gets metadata about a Document.
344
+
345
+ Args:
346
+ name (str): The resource name of the Document.
347
+ Example: ragStores/rag-store-foo/documents/documents-bar
348
+ config (GetDocumentConfig | None): Optional parameters for the request.
349
+
350
+ Returns:
351
+ The Document.
352
+ """
353
+
354
+ parameter_model = types._GetDocumentParameters(
355
+ name=name,
356
+ config=config,
357
+ )
358
+
359
+ request_url_dict: Optional[dict[str, str]]
360
+ if self._api_client.vertexai:
361
+ raise ValueError(
362
+ 'This method is only supported in the Gemini Developer client.'
363
+ )
364
+ else:
365
+ request_dict = _GetDocumentParameters_to_mldev(parameter_model)
366
+ request_url_dict = request_dict.get('_url')
367
+ if request_url_dict:
368
+ path = '{name}'.format_map(request_url_dict)
369
+ else:
370
+ path = '{name}'
371
+
372
+ query_params = request_dict.get('_query')
373
+ if query_params:
374
+ path = f'{path}?{urlencode(query_params)}'
375
+ # TODO: remove the hack that pops config.
376
+ request_dict.pop('config', None)
377
+
378
+ http_options: Optional[types.HttpOptions] = None
379
+ if (
380
+ parameter_model.config is not None
381
+ and parameter_model.config.http_options is not None
382
+ ):
383
+ http_options = parameter_model.config.http_options
384
+
385
+ request_dict = _common.convert_to_dict(request_dict)
386
+ request_dict = _common.encode_unserializable_types(request_dict)
387
+
388
+ response = await self._api_client.async_request(
389
+ 'get', path, request_dict, http_options
390
+ )
391
+
392
+ response_dict = {} if not response.body else json.loads(response.body)
393
+
394
+ return_value = types.Document._from_response(
395
+ response=response_dict, kwargs=parameter_model.model_dump()
396
+ )
397
+
398
+ self._api_client._verify_response(return_value)
399
+ return return_value
400
+
401
+ async def delete(
402
+ self,
403
+ *,
404
+ name: str,
405
+ config: Optional[types.DeleteDocumentConfigOrDict] = None,
406
+ ) -> None:
407
+ """Deletes a Document.
408
+
409
+ Args:
410
+ name (str): The resource name of the Document.
411
+ Example: ragStores/rag-store-foo/documents/documents-bar
412
+ config (DeleteDocumentConfig | None): Optional parameters for the request.
413
+
414
+ Returns:
415
+ None
416
+ """
417
+
418
+ parameter_model = types._DeleteDocumentParameters(
419
+ name=name,
420
+ config=config,
421
+ )
422
+
423
+ request_url_dict: Optional[dict[str, str]]
424
+ if self._api_client.vertexai:
425
+ raise ValueError(
426
+ 'This method is only supported in the Gemini Developer client.'
427
+ )
428
+ else:
429
+ request_dict = _DeleteDocumentParameters_to_mldev(parameter_model)
430
+ request_url_dict = request_dict.get('_url')
431
+ if request_url_dict:
432
+ path = '{name}'.format_map(request_url_dict)
433
+ else:
434
+ path = '{name}'
435
+
436
+ query_params = request_dict.get('_query')
437
+ if query_params:
438
+ path = f'{path}?{urlencode(query_params)}'
439
+ # TODO: remove the hack that pops config.
440
+ request_dict.pop('config', None)
441
+
442
+ http_options: Optional[types.HttpOptions] = None
443
+ if (
444
+ parameter_model.config is not None
445
+ and parameter_model.config.http_options is not None
446
+ ):
447
+ http_options = parameter_model.config.http_options
448
+
449
+ request_dict = _common.convert_to_dict(request_dict)
450
+ request_dict = _common.encode_unserializable_types(request_dict)
451
+
452
+ response = await self._api_client.async_request(
453
+ 'delete', path, request_dict, http_options
454
+ )
455
+
456
+ async def _list(
457
+ self,
458
+ *,
459
+ parent: str,
460
+ config: Optional[types.ListDocumentsConfigOrDict] = None,
461
+ ) -> types.ListDocumentsResponse:
462
+ """Lists all Documents in a FileSearchStore.
463
+
464
+ Args:
465
+ parent (str): The name of the FileSearchStore containing the Documents.
466
+ config (ListDocumentsConfig | None): Optional parameters for the request,
467
+ such as page_size.
468
+
469
+ Returns:
470
+ ListDocumentsResponse: A paginated list of Documents.
471
+ """
472
+
473
+ parameter_model = types._ListDocumentsParameters(
474
+ parent=parent,
475
+ config=config,
476
+ )
477
+
478
+ request_url_dict: Optional[dict[str, str]]
479
+ if self._api_client.vertexai:
480
+ raise ValueError(
481
+ 'This method is only supported in the Gemini Developer client.'
482
+ )
483
+ else:
484
+ request_dict = _ListDocumentsParameters_to_mldev(parameter_model)
485
+ request_url_dict = request_dict.get('_url')
486
+ if request_url_dict:
487
+ path = '{parent}/documents'.format_map(request_url_dict)
488
+ else:
489
+ path = '{parent}/documents'
490
+
491
+ query_params = request_dict.get('_query')
492
+ if query_params:
493
+ path = f'{path}?{urlencode(query_params)}'
494
+ # TODO: remove the hack that pops config.
495
+ request_dict.pop('config', None)
496
+
497
+ http_options: Optional[types.HttpOptions] = None
498
+ if (
499
+ parameter_model.config is not None
500
+ and parameter_model.config.http_options is not None
501
+ ):
502
+ http_options = parameter_model.config.http_options
503
+
504
+ request_dict = _common.convert_to_dict(request_dict)
505
+ request_dict = _common.encode_unserializable_types(request_dict)
506
+
507
+ response = await self._api_client.async_request(
508
+ 'get', path, request_dict, http_options
509
+ )
510
+
511
+ response_dict = {} if not response.body else json.loads(response.body)
512
+
513
+ if not self._api_client.vertexai:
514
+ response_dict = _ListDocumentsResponse_from_mldev(response_dict)
515
+
516
+ return_value = types.ListDocumentsResponse._from_response(
517
+ response=response_dict, kwargs=parameter_model.model_dump()
518
+ )
519
+
520
+ self._api_client._verify_response(return_value)
521
+ return return_value
522
+
523
+ async def list(
524
+ self,
525
+ *,
526
+ parent: str,
527
+ config: Optional[types.ListDocumentsConfigOrDict] = None,
528
+ ) -> AsyncPager[types.Document]:
529
+ """Lists documents asynchronously.
530
+
531
+ Args:
532
+ parent (str): The name of the RagStore containing the Documents.
533
+ config (ListDocumentsConfig): Optional configuration for the list request.
534
+
535
+ Returns:
536
+ A Pager object that contains one page of documents. When iterating over
537
+ the pager, it automatically fetches the next page if there are more.
538
+ Usage:
539
+ .. code-block:: python
540
+ async for document in await
541
+ client.documents.list(parent='rag_store_name'):
542
+ print(f"document: {document.name} - {document.display_name}")
543
+ """
544
+ return AsyncPager(
545
+ 'documents',
546
+ self._list,
547
+ await self._list(parent=parent, config=config),
548
+ config,
549
+ )
google/genai/errors.py CHANGED
@@ -29,7 +29,7 @@ if TYPE_CHECKING:
29
29
  class APIError(Exception):
30
30
  """General errors raised by the GenAI API."""
31
31
  code: int
32
- response: Union['ReplayResponse', httpx.Response]
32
+ response: Union['ReplayResponse', httpx.Response, 'aiohttp.ClientResponse']
33
33
 
34
34
  status: Optional[str] = None
35
35
  message: Optional[str] = None