google-genai 1.15.0__py3-none-any.whl → 1.16.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/__init__.py +5 -3
- google/genai/_adapters.py +55 -0
- google/genai/_api_client.py +3 -3
- google/genai/_api_module.py +1 -1
- google/genai/_automatic_function_calling_util.py +1 -1
- google/genai/_common.py +1 -1
- google/genai/_extra_utils.py +117 -9
- google/genai/_live_converters.py +1295 -20
- google/genai/_mcp_utils.py +117 -0
- google/genai/_replay_api_client.py +1 -1
- google/genai/_test_api_client.py +1 -1
- google/genai/_tokens_converters.py +1701 -0
- google/genai/_transformers.py +66 -33
- google/genai/caches.py +223 -20
- google/genai/chats.py +1 -1
- google/genai/client.py +12 -1
- google/genai/errors.py +1 -1
- google/genai/live.py +218 -35
- google/genai/live_music.py +201 -0
- google/genai/models.py +505 -44
- google/genai/pagers.py +1 -1
- google/genai/tokens.py +357 -0
- google/genai/types.py +7887 -6765
- google/genai/version.py +2 -2
- {google_genai-1.15.0.dist-info → google_genai-1.16.0.dist-info}/METADATA +8 -4
- google_genai-1.16.0.dist-info/RECORD +35 -0
- {google_genai-1.15.0.dist-info → google_genai-1.16.0.dist-info}/WHEEL +1 -1
- google_genai-1.15.0.dist-info/RECORD +0 -30
- {google_genai-1.15.0.dist-info → google_genai-1.16.0.dist-info}/licenses/LICENSE +0 -0
- {google_genai-1.15.0.dist-info → google_genai-1.16.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,117 @@
|
|
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
|
+
"""Utils for working with MCP tools."""
|
17
|
+
|
18
|
+
from importlib.metadata import PackageNotFoundError, version
|
19
|
+
import typing
|
20
|
+
from typing import Any
|
21
|
+
|
22
|
+
from . import types
|
23
|
+
|
24
|
+
if typing.TYPE_CHECKING:
|
25
|
+
from mcp.types import Tool as McpTool
|
26
|
+
from mcp import ClientSession as McpClientSession
|
27
|
+
else:
|
28
|
+
McpClientSession: typing.Type = Any
|
29
|
+
McpTool: typing.Type = Any
|
30
|
+
try:
|
31
|
+
from mcp.types import Tool as McpTool
|
32
|
+
from mcp import ClientSession as McpClientSession
|
33
|
+
except ImportError:
|
34
|
+
McpTool = None
|
35
|
+
McpClientSession = None
|
36
|
+
|
37
|
+
|
38
|
+
def mcp_to_gemini_tool(tool: McpTool) -> types.Tool:
|
39
|
+
"""Translates an MCP tool to a Google GenAI tool."""
|
40
|
+
return types.Tool(
|
41
|
+
function_declarations=[{
|
42
|
+
"name": tool.name,
|
43
|
+
"description": tool.description,
|
44
|
+
"parameters": types.Schema.from_json_schema(
|
45
|
+
json_schema=types.JSONSchema(
|
46
|
+
**_filter_to_supported_schema(tool.inputSchema)
|
47
|
+
)
|
48
|
+
),
|
49
|
+
}]
|
50
|
+
)
|
51
|
+
|
52
|
+
|
53
|
+
def mcp_to_gemini_tools(tools: list[McpTool]) -> list[types.Tool]:
|
54
|
+
"""Translates a list of MCP tools to a list of Google GenAI tools."""
|
55
|
+
return [mcp_to_gemini_tool(tool) for tool in tools]
|
56
|
+
|
57
|
+
|
58
|
+
def has_mcp_tool_usage(tools: types.ToolListUnion) -> bool:
|
59
|
+
"""Checks whether the list of tools contains any MCP tools or sessions."""
|
60
|
+
if McpClientSession is None:
|
61
|
+
return False
|
62
|
+
for tool in tools:
|
63
|
+
if isinstance(tool, McpTool) or isinstance(tool, McpClientSession):
|
64
|
+
return True
|
65
|
+
return False
|
66
|
+
|
67
|
+
|
68
|
+
def has_mcp_session_usage(tools: types.ToolListUnion) -> bool:
|
69
|
+
"""Checks whether the list of tools contains any MCP sessions."""
|
70
|
+
if McpClientSession is None:
|
71
|
+
return False
|
72
|
+
for tool in tools:
|
73
|
+
if isinstance(tool, McpClientSession):
|
74
|
+
return True
|
75
|
+
return False
|
76
|
+
|
77
|
+
|
78
|
+
def set_mcp_usage_header(headers: dict[str, str]) -> None:
|
79
|
+
"""Sets the MCP version label in the Google API client header."""
|
80
|
+
if McpClientSession is None:
|
81
|
+
return
|
82
|
+
try:
|
83
|
+
version_label = version("mcp")
|
84
|
+
except PackageNotFoundError:
|
85
|
+
version_label = "0.0.0"
|
86
|
+
# TODO: b/418827318 - Investigate weather the duplicate mcp label check is
|
87
|
+
# necessary.
|
88
|
+
mcp_label = f"mcp_used/{version_label}"
|
89
|
+
existing_header = headers.get("x-goog-api-client", "")
|
90
|
+
if mcp_label in existing_header:
|
91
|
+
return
|
92
|
+
headers["x-goog-api-client"] = (existing_header + f" {mcp_label}").lstrip()
|
93
|
+
|
94
|
+
|
95
|
+
def _filter_to_supported_schema(schema: dict[str, Any]) -> dict[str, Any]:
|
96
|
+
"""Filters the schema to only include fields that are supported by JSONSchema."""
|
97
|
+
supported_fields: set[str] = set(types.JSONSchema.model_fields.keys())
|
98
|
+
schema_field_names: tuple[str] = ("items",) # 'additional_properties' to come
|
99
|
+
list_schema_field_names: tuple[str] = (
|
100
|
+
"any_of", # 'one_of', 'all_of', 'not' to come
|
101
|
+
)
|
102
|
+
dict_schema_field_names: tuple[str] = ("properties",) # 'defs' to come
|
103
|
+
for field_name, field_value in schema.items():
|
104
|
+
if field_name in schema_field_names:
|
105
|
+
schema[field_name] = _filter_to_supported_schema(field_value)
|
106
|
+
elif field_name in list_schema_field_names:
|
107
|
+
schema[field_name] = [
|
108
|
+
_filter_to_supported_schema(value) for value in field_value
|
109
|
+
]
|
110
|
+
elif field_name in dict_schema_field_names:
|
111
|
+
schema[field_name] = {
|
112
|
+
key: _filter_to_supported_schema(value)
|
113
|
+
for key, value in field_value.items()
|
114
|
+
}
|
115
|
+
return {
|
116
|
+
key: value for key, value in schema.items() if key in supported_fields
|
117
|
+
}
|
google/genai/_test_api_client.py
CHANGED