Skip to content

approval_required

autogen.beta.middleware.builtin.tools.approval.approval_required #

approval_required(message='Agent wants to call the tool:\n`{tool_name}`, {tool_arguments}\nPlease approve or deny this request.\nY/N?\n', denied_message='User denied the tool call request', timeout=30)

Tool middleware that requests human approval before executing a tool call.

PARAMETER DESCRIPTION
message

Prompt template shown to the user. Supports {tool_name} and {tool_arguments} placeholders.

TYPE: str DEFAULT: 'Agent wants to call the tool:\n`{tool_name}`, {tool_arguments}\nPlease approve or deny this request.\nY/N?\n'

denied_message

Message shown to the LLM after the tool call is denied.

TYPE: str DEFAULT: 'User denied the tool call request'

timeout

Seconds to wait for user input before timing out.

TYPE: int DEFAULT: 30

RETURNS DESCRIPTION
ToolMiddleware

A tool middleware hook that can be passed to the middleware

ToolMiddleware

parameter of :func:~autogen.beta.tool.

Source code in autogen/beta/middleware/builtin/tools/approval.py
def approval_required(
    message: str = "Agent wants to call the tool:\n`{tool_name}`, {tool_arguments}\nPlease approve or deny this request.\nY/N?\n",
    denied_message: str = "User denied the tool call request",
    timeout: int = 30,
) -> ToolMiddleware:
    """Tool middleware that requests human approval before executing a tool call.

    Args:
        message: Prompt template shown to the user. Supports ``{tool_name}`` and
            ``{tool_arguments}`` placeholders.
        denied_message: Message shown to the LLM after the tool call is denied.
        timeout: Seconds to wait for user input before timing out.

    Returns:
        A tool middleware hook that can be passed to the ``middleware``
        parameter of :func:`~autogen.beta.tool`.
    """

    async def hitl_hook(
        call_next: ToolExecution,
        event: ToolCallEvent,
        context: Context,
    ) -> ToolResultType:
        user_result = await context.input(
            message.format(tool_name=event.name, tool_arguments=event.arguments),
            timeout=timeout,
        )

        if user_result.lower() in ("y", "yes", "1"):
            return await call_next(event, context)

        return ToolResultEvent.from_call(event, result=denied_message)

    return hitl_hook