Create a FunctionTool with copied-tool-aware failure handling bound in one place.
(
*,
name: str,
description: str,
params_json_schema: dict[str, Any],
invoke_tool_impl: Callable[[ToolContext[Any], str], Awaitable[Any]],
on_handled_error: Callable[[FunctionTool, Exception, str, ToolContext[Any]], None],
failure_error_function: ToolErrorFunction | None | object = _UNSET_FAILURE_ERROR_FUNCTION,
strict_json_schema: bool = True,
is_enabled: bool | Callable[[RunContextWrapper[Any], AgentBase], MaybeAwaitable[bool]] = True,
tool_input_guardrails: list[ToolInputGuardrail[Any]] | None = None,
tool_output_guardrails: list[ToolOutputGuardrail[Any]] | None = None,
needs_approval: (
bool | Callable[[RunContextWrapper[Any], dict[str, Any], str], Awaitable[bool]]
) = False,
timeout_seconds: float | None = None,
timeout_behavior: ToolTimeoutBehavior = "error_as_result",
timeout_error_function: ToolErrorFunction | None = None,
defer_loading: bool = False,
custom_data_extractor: FunctionToolCustomDataExtractor | None = None,
sync_invoker: bool = False,
mcp_title: str | None = None,
tool_origin: ToolOrigin | None = None,
)
| 597 | |
| 598 | |
| 599 | def _build_wrapped_function_tool( |
| 600 | *, |
| 601 | name: str, |
| 602 | description: str, |
| 603 | params_json_schema: dict[str, Any], |
| 604 | invoke_tool_impl: Callable[[ToolContext[Any], str], Awaitable[Any]], |
| 605 | on_handled_error: Callable[[FunctionTool, Exception, str, ToolContext[Any]], None], |
| 606 | failure_error_function: ToolErrorFunction | None | object = _UNSET_FAILURE_ERROR_FUNCTION, |
| 607 | strict_json_schema: bool = True, |
| 608 | is_enabled: bool | Callable[[RunContextWrapper[Any], AgentBase], MaybeAwaitable[bool]] = True, |
| 609 | tool_input_guardrails: list[ToolInputGuardrail[Any]] | None = None, |
| 610 | tool_output_guardrails: list[ToolOutputGuardrail[Any]] | None = None, |
| 611 | needs_approval: ( |
| 612 | bool | Callable[[RunContextWrapper[Any], dict[str, Any], str], Awaitable[bool]] |
| 613 | ) = False, |
| 614 | timeout_seconds: float | None = None, |
| 615 | timeout_behavior: ToolTimeoutBehavior = "error_as_result", |
| 616 | timeout_error_function: ToolErrorFunction | None = None, |
| 617 | defer_loading: bool = False, |
| 618 | custom_data_extractor: FunctionToolCustomDataExtractor | None = None, |
| 619 | sync_invoker: bool = False, |
| 620 | mcp_title: str | None = None, |
| 621 | tool_origin: ToolOrigin | None = None, |
| 622 | ) -> FunctionTool: |
| 623 | """Create a FunctionTool with copied-tool-aware failure handling bound in one place.""" |
| 624 | on_invoke_tool = _with_context_function_tool_failure_error_handler( |
| 625 | invoke_tool_impl, |
| 626 | on_handled_error, |
| 627 | ) |
| 628 | if sync_invoker: |
| 629 | setattr(on_invoke_tool, _SYNC_FUNCTION_TOOL_MARKER, True) |
| 630 | |
| 631 | return set_function_tool_failure_error_function( |
| 632 | FunctionTool( |
| 633 | name=name, |
| 634 | description=description, |
| 635 | params_json_schema=params_json_schema, |
| 636 | on_invoke_tool=on_invoke_tool, |
| 637 | strict_json_schema=strict_json_schema, |
| 638 | is_enabled=is_enabled, |
| 639 | tool_input_guardrails=tool_input_guardrails, |
| 640 | tool_output_guardrails=tool_output_guardrails, |
| 641 | needs_approval=needs_approval, |
| 642 | timeout_seconds=timeout_seconds, |
| 643 | timeout_behavior=timeout_behavior, |
| 644 | timeout_error_function=timeout_error_function, |
| 645 | defer_loading=defer_loading, |
| 646 | custom_data_extractor=custom_data_extractor, |
| 647 | _mcp_title=mcp_title, |
| 648 | _tool_origin=tool_origin, |
| 649 | ), |
| 650 | failure_error_function, |
| 651 | ) |
| 652 | |
| 653 | |
| 654 | def get_function_tool_origin(function_tool: FunctionTool) -> ToolOrigin | None: |
no test coverage detected