google-genai 1.7.0__py3-none-any.whl → 1.53.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.
Files changed (42) hide show
  1. google/genai/__init__.py +4 -2
  2. google/genai/_adapters.py +55 -0
  3. google/genai/_api_client.py +1301 -299
  4. google/genai/_api_module.py +1 -1
  5. google/genai/_automatic_function_calling_util.py +54 -33
  6. google/genai/_base_transformers.py +26 -0
  7. google/genai/_base_url.py +50 -0
  8. google/genai/_common.py +560 -59
  9. google/genai/_extra_utils.py +371 -38
  10. google/genai/_live_converters.py +1467 -0
  11. google/genai/_local_tokenizer_loader.py +214 -0
  12. google/genai/_mcp_utils.py +117 -0
  13. google/genai/_operations_converters.py +394 -0
  14. google/genai/_replay_api_client.py +204 -92
  15. google/genai/_test_api_client.py +1 -1
  16. google/genai/_tokens_converters.py +520 -0
  17. google/genai/_transformers.py +633 -233
  18. google/genai/batches.py +1733 -538
  19. google/genai/caches.py +678 -1012
  20. google/genai/chats.py +48 -38
  21. google/genai/client.py +142 -15
  22. google/genai/documents.py +532 -0
  23. google/genai/errors.py +141 -35
  24. google/genai/file_search_stores.py +1296 -0
  25. google/genai/files.py +312 -744
  26. google/genai/live.py +617 -367
  27. google/genai/live_music.py +197 -0
  28. google/genai/local_tokenizer.py +395 -0
  29. google/genai/models.py +3598 -3116
  30. google/genai/operations.py +201 -362
  31. google/genai/pagers.py +23 -7
  32. google/genai/py.typed +1 -0
  33. google/genai/tokens.py +362 -0
  34. google/genai/tunings.py +1274 -496
  35. google/genai/types.py +14535 -5454
  36. google/genai/version.py +2 -2
  37. {google_genai-1.7.0.dist-info → google_genai-1.53.0.dist-info}/METADATA +736 -234
  38. google_genai-1.53.0.dist-info/RECORD +41 -0
  39. {google_genai-1.7.0.dist-info → google_genai-1.53.0.dist-info}/WHEEL +1 -1
  40. google_genai-1.7.0.dist-info/RECORD +0 -27
  41. {google_genai-1.7.0.dist-info → google_genai-1.53.0.dist-info/licenses}/LICENSE +0 -0
  42. {google_genai-1.7.0.dist-info → google_genai-1.53.0.dist-info}/top_level.txt +0 -0
@@ -1,4 +1,4 @@
1
- # Copyright 2024 Google LLC
1
+ # Copyright 2025 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright 2024 Google LLC
1
+ # Copyright 2025 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -28,7 +28,19 @@ from . import types
28
28
  if sys.version_info >= (3, 10):
29
29
  VersionedUnionType = builtin_types.UnionType
30
30
  else:
31
- VersionedUnionType = typing._UnionGenericAlias
31
+ VersionedUnionType = typing._UnionGenericAlias # type: ignore[attr-defined]
32
+
33
+
34
+ __all__ = [
35
+ '_py_builtin_type_to_schema_type',
36
+ '_raise_for_unsupported_param',
37
+ '_handle_params_as_deferred_annotations',
38
+ '_add_unevaluated_items_to_fixed_len_tuple_schema',
39
+ '_is_builtin_primitive_or_compound',
40
+ '_is_default_value_compatible',
41
+ '_parse_schema_from_parameter',
42
+ '_get_required_fields',
43
+ ]
32
44
 
33
45
  _py_builtin_type_to_schema_type = {
34
46
  str: types.Type.STRING,
@@ -37,26 +49,47 @@ _py_builtin_type_to_schema_type = {
37
49
  bool: types.Type.BOOLEAN,
38
50
  list: types.Type.ARRAY,
39
51
  dict: types.Type.OBJECT,
52
+ None: types.Type.NULL,
40
53
  }
41
54
 
42
55
 
43
- def _is_builtin_primitive_or_compound(
44
- annotation: inspect.Parameter.annotation, # type: ignore[valid-type]
45
- ) -> bool:
46
- return annotation in _py_builtin_type_to_schema_type.keys()
56
+ def _raise_for_unsupported_param(
57
+ param: inspect.Parameter, func_name: str, exception: Union[Exception, type[Exception]]
58
+ ) -> None:
59
+ raise ValueError(
60
+ f'Failed to parse the parameter {param} of function {func_name} for'
61
+ ' automatic function calling.Automatic function calling works best with'
62
+ ' simpler function signature schema, consider manually parsing your'
63
+ f' function declaration for function {func_name}.'
64
+ ) from exception
47
65
 
48
66
 
49
- def _raise_for_default_if_mldev(schema: types.Schema):
50
- if schema.default is not None:
51
- raise ValueError(
52
- 'Default value is not supported in function declaration schema for'
53
- ' the Gemini API.'
54
- )
67
+ def _handle_params_as_deferred_annotations(param: inspect.Parameter, annotation_under_future: dict[str, Any], name: str) -> inspect.Parameter:
68
+ """Catches the case when type hints are stored as strings."""
69
+ if isinstance(param.annotation, str):
70
+ param = param.replace(annotation=annotation_under_future[name])
71
+ return param
55
72
 
56
73
 
57
- def _raise_if_schema_unsupported(api_option: Literal['VERTEX_AI', 'GEMINI_API'], schema: types.Schema):
58
- if api_option == 'GEMINI_API':
59
- _raise_for_default_if_mldev(schema)
74
+ def _add_unevaluated_items_to_fixed_len_tuple_schema(
75
+ json_schema: dict[str, Any]
76
+ ) -> dict[str, Any]:
77
+ if (
78
+ json_schema.get('maxItems')
79
+ and (
80
+ json_schema.get('prefixItems')
81
+ and len(json_schema['prefixItems']) == json_schema['maxItems']
82
+ )
83
+ and json_schema.get('type') == 'array'
84
+ ):
85
+ json_schema['unevaluatedItems'] = False
86
+ return json_schema
87
+
88
+
89
+ def _is_builtin_primitive_or_compound(
90
+ annotation: inspect.Parameter.annotation, # type: ignore[valid-type]
91
+ ) -> bool:
92
+ return annotation in _py_builtin_type_to_schema_type.keys()
60
93
 
61
94
 
62
95
  def _is_default_value_compatible(
@@ -72,16 +105,16 @@ def _is_default_value_compatible(
72
105
  or isinstance(annotation, VersionedUnionType)
73
106
  ):
74
107
  origin = get_origin(annotation)
75
- if origin in (Union, VersionedUnionType):
108
+ if origin in (Union, VersionedUnionType): # type: ignore[comparison-overlap]
76
109
  return any(
77
110
  _is_default_value_compatible(default_value, arg)
78
111
  for arg in get_args(annotation)
79
112
  )
80
113
 
81
- if origin is dict:
114
+ if origin is dict: # type: ignore[comparison-overlap]
82
115
  return isinstance(default_value, dict)
83
116
 
84
- if origin is list:
117
+ if origin is list: # type: ignore[comparison-overlap]
85
118
  if not isinstance(default_value, list):
86
119
  return False
87
120
  # most tricky case, element in list is union type
@@ -97,14 +130,14 @@ def _is_default_value_compatible(
97
130
  for item in default_value
98
131
  )
99
132
 
100
- if origin is Literal:
133
+ if origin is Literal: # type: ignore[comparison-overlap]
101
134
  return default_value in get_args(annotation)
102
135
 
103
136
  # return False for any other unrecognized annotation
104
137
  return False
105
138
 
106
139
 
107
- def _parse_schema_from_parameter(
140
+ def _parse_schema_from_parameter( # type: ignore[return]
108
141
  api_option: Literal['VERTEX_AI', 'GEMINI_API'],
109
142
  param: inspect.Parameter,
110
143
  func_name: str,
@@ -125,7 +158,6 @@ def _parse_schema_from_parameter(
125
158
  raise ValueError(default_value_error_msg)
126
159
  schema.default = param.default
127
160
  schema.type = _py_builtin_type_to_schema_type[param.annotation]
128
- _raise_if_schema_unsupported(api_option, schema)
129
161
  return schema
130
162
  if (
131
163
  isinstance(param.annotation, VersionedUnionType)
@@ -166,7 +198,6 @@ def _parse_schema_from_parameter(
166
198
  if not _is_default_value_compatible(param.default, param.annotation):
167
199
  raise ValueError(default_value_error_msg)
168
200
  schema.default = param.default
169
- _raise_if_schema_unsupported(api_option, schema)
170
201
  return schema
171
202
  if isinstance(param.annotation, _GenericAlias) or isinstance(
172
203
  param.annotation, builtin_types.GenericAlias
@@ -179,7 +210,6 @@ def _parse_schema_from_parameter(
179
210
  if not _is_default_value_compatible(param.default, param.annotation):
180
211
  raise ValueError(default_value_error_msg)
181
212
  schema.default = param.default
182
- _raise_if_schema_unsupported(api_option, schema)
183
213
  return schema
184
214
  if origin is Literal:
185
215
  if not all(isinstance(arg, str) for arg in args):
@@ -192,7 +222,6 @@ def _parse_schema_from_parameter(
192
222
  if not _is_default_value_compatible(param.default, param.annotation):
193
223
  raise ValueError(default_value_error_msg)
194
224
  schema.default = param.default
195
- _raise_if_schema_unsupported(api_option, schema)
196
225
  return schema
197
226
  if origin is list:
198
227
  schema.type = _py_builtin_type_to_schema_type[list]
@@ -209,7 +238,6 @@ def _parse_schema_from_parameter(
209
238
  if not _is_default_value_compatible(param.default, param.annotation):
210
239
  raise ValueError(default_value_error_msg)
211
240
  schema.default = param.default
212
- _raise_if_schema_unsupported(api_option, schema)
213
241
  return schema
214
242
  if origin is Union:
215
243
  schema.any_of = []
@@ -259,7 +287,6 @@ def _parse_schema_from_parameter(
259
287
  if not _is_default_value_compatible(param.default, param.annotation):
260
288
  raise ValueError(default_value_error_msg)
261
289
  schema.default = param.default
262
- _raise_if_schema_unsupported(api_option, schema)
263
290
  return schema
264
291
  # all other generic alias will be invoked in raise branch
265
292
  if (
@@ -284,14 +311,8 @@ def _parse_schema_from_parameter(
284
311
  func_name,
285
312
  )
286
313
  schema.required = _get_required_fields(schema)
287
- _raise_if_schema_unsupported(api_option, schema)
288
314
  return schema
289
- raise ValueError(
290
- f'Failed to parse the parameter {param} of function {func_name} for'
291
- ' automatic function calling.Automatic function calling works best with'
292
- ' simpler function signature schema,consider manually parse your'
293
- f' function declaration for function {func_name}.'
294
- )
315
+ _raise_for_unsupported_param(param, func_name, ValueError)
295
316
 
296
317
 
297
318
  def _get_required_fields(schema: types.Schema) -> Optional[list[str]]:
@@ -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,50 @@
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 os
17
+ from typing import Optional
18
+
19
+ from .types import HttpOptions
20
+
21
+ _default_base_gemini_url = None
22
+ _default_base_vertex_url = None
23
+
24
+
25
+ def set_default_base_urls(
26
+ gemini_url: Optional[str], vertex_url: Optional[str]
27
+ ) -> None:
28
+ """Overrides the base URLs for the Gemini API and Vertex AI API."""
29
+ global _default_base_gemini_url, _default_base_vertex_url
30
+ _default_base_gemini_url = gemini_url
31
+ _default_base_vertex_url = vertex_url
32
+
33
+
34
+ def get_base_url(
35
+ vertexai: bool,
36
+ http_options: Optional[HttpOptions] = None,
37
+ ) -> Optional[str]:
38
+ """Returns the default base URL based on the following priority.
39
+
40
+ 1. Base URLs set via HttpOptions.
41
+ 2. Base URLs set via the latest call to setDefaultBaseUrls.
42
+ 3. Base URLs set via environment variables.
43
+ """
44
+ if http_options and http_options.base_url:
45
+ return http_options.base_url
46
+
47
+ if vertexai:
48
+ return _default_base_vertex_url or os.getenv('GOOGLE_VERTEX_BASE_URL')
49
+ else:
50
+ return _default_base_gemini_url or os.getenv('GOOGLE_GEMINI_BASE_URL')