Files
adk-python/contributing/samples/human_tool_confirmation/agent.py
T
Shangjie ChenandCopybara-Service a17bcbb2aa feat: Add a tool confirmation flow that can guard tool execution with explicit confirmation and custom input
The existing `LongRunningTool` does not define a programmatic way to provide & validate structured input, also it relies on LLM to reason and parse the user's response.

For a quick start, annotate the function with `FunctionTool(my_function, require_confirmation=True)`. A more advanced flow is shown in the `human_tool_confirmation` sample.

The new flow is similar to the existing Auth flow:
- User request a tool confirmation by calling `tool_context.request_confirmation()` in the tool or `before_tool_callback`, or just using the `require_confirmation` shortcut in FunctionTool.
- User can provide custom validation logic before tool call proceeds.
- ADK creates corresponding RequestConfirmation FunctionCall Event to ask user for confirmation
- User needs to provide the expected tool confirmation to a RequestConfirmation FunctionResponse Event.
- ADK then checks the response and continues the tool call.

PiperOrigin-RevId: 801019917
2025-08-29 13:56:54 -07:00

81 lines
2.7 KiB
Python

# Copyright 2025 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 google.adk import Agent
from google.adk.tools.function_tool import FunctionTool
from google.adk.tools.tool_confirmation import ToolConfirmation
from google.adk.tools.tool_context import ToolContext
from google.genai import types
def reimburse(amount: int, tool_context: ToolContext) -> str:
"""Reimburse the employee for the given amount."""
return {'status': 'ok'}
def request_time_off(days: int, tool_context: ToolContext):
"""Request day off for the employee."""
if days <= 0:
return {'status': 'Invalid days to request.'}
if days <= 2:
return {
'status': 'ok',
'approved_days': days,
}
tool_confirmation = tool_context.tool_confirmation
if not tool_confirmation:
tool_context.request_confirmation(
hint=(
'Please approve or reject the tool call request_time_off() by'
' responding with a FunctionResponse with an expected'
' ToolConfirmation payload.'
),
payload={
'approved_days': 0,
},
)
return {'status': 'Manager approval is required.'}
approved_days = tool_confirmation.payload['approved_days']
approved_days = min(approved_days, days)
if approved_days == 0:
return {'status': 'The time off request is rejected.', 'approved_days': 0}
return {
'status': 'ok',
'approved_days': approved_days,
}
root_agent = Agent(
model='gemini-2.5-flash',
name='time_off_agent',
instruction="""
You are a helpful assistant that can help employees with reimbursement and time off requests.
- Use the `reimburse` tool for reimbursement requests.
- Use the `request_time_off` tool for time off requests.
- Prioritize using tools to fulfill the user's request.
- Always respond to the user with the tool results.
""",
tools=[
# Set require_confirmation to True to require user confirmation for the
# tool call. This is an easier way to get user confirmation if the tool
# just need a boolean confirmation.
FunctionTool(reimburse, require_confirmation=True),
request_time_off,
],
generate_content_config=types.GenerateContentConfig(temperature=0.1),
)