-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtransfer_to_agent_tool.py
More file actions
87 lines (68 loc) · 2.9 KB
/
transfer_to_agent_tool.py
File metadata and controls
87 lines (68 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import Optional
from google.genai import types
from typing_extensions import override
from .function_tool import FunctionTool
from .tool_context import ToolContext
def transfer_to_agent(agent_name: str, tool_context: ToolContext) -> None:
"""Transfer the question to another agent.
This tool hands off control to another agent when it's more suitable to
answer the user's question according to the agent's description.
Args:
agent_name: the agent name to transfer to.
"""
# Developer note: For most use cases, prefer TransferToAgentTool over
# calling this function directly. TransferToAgentTool adds enum
# constraints that prevent LLMs from hallucinating invalid agent names.
tool_context.actions.transfer_to_agent = agent_name
class TransferToAgentTool(FunctionTool):
"""A specialized FunctionTool for agent transfer with enum constraints.
This tool enhances the base transfer_to_agent function by adding JSON Schema
enum constraints to the agent_name parameter. This prevents LLMs from
hallucinating invalid agent names by restricting choices to only valid agents.
Attributes:
agent_names: List of valid agent names that can be transferred to.
"""
def __init__(
self,
agent_names: list[str],
):
"""Initialize the TransferToAgentTool.
Args:
agent_names: List of valid agent names that can be transferred to.
"""
super().__init__(func=transfer_to_agent)
self._agent_names = agent_names
@override
def _get_declaration(self) -> Optional[types.FunctionDeclaration]:
"""Add enum constraint to the agent_name parameter.
Returns:
FunctionDeclaration with enum constraint on agent_name parameter.
"""
function_decl = super()._get_declaration()
if not function_decl:
return function_decl
# Handle parameters (types.Schema object)
if function_decl.parameters:
agent_name_schema = function_decl.parameters.properties.get('agent_name')
if agent_name_schema:
agent_name_schema.enum = self._agent_names
# Handle parameters_json_schema (dict)
if function_decl.parameters_json_schema:
properties = function_decl.parameters_json_schema.get('properties', {})
if 'agent_name' in properties:
properties['agent_name']['enum'] = self._agent_names
return function_decl