Decorators
Public decorators for defining Agents SDK components.
tool is an alias for function_tool.
input_guardrail
input_guardrail(
func: _InputGuardrailFuncSync[TContext_co],
) -> InputGuardrail[TContext_co]
input_guardrail(
func: _InputGuardrailFuncAsync[TContext_co],
) -> InputGuardrail[TContext_co]
input_guardrail(
*, name: str | None = None, run_in_parallel: bool = True
) -> Callable[
[
_InputGuardrailFuncSync[TContext_co]
| _InputGuardrailFuncAsync[TContext_co]
],
InputGuardrail[TContext_co],
]
input_guardrail(
func: _InputGuardrailFuncSync[TContext_co]
| _InputGuardrailFuncAsync[TContext_co]
| None = None,
*,
name: str | None = None,
run_in_parallel: bool = True,
) -> (
InputGuardrail[TContext_co]
| Callable[
[
_InputGuardrailFuncSync[TContext_co]
| _InputGuardrailFuncAsync[TContext_co]
],
InputGuardrail[TContext_co],
]
)
Decorator that transforms a sync or async function into an InputGuardrail.
It can be used directly (no parentheses) or with keyword args, e.g.:
@input_guardrail
def my_sync_guardrail(...): ...
@input_guardrail(name="guardrail_name", run_in_parallel=False)
async def my_async_guardrail(...): ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
_InputGuardrailFuncSync[TContext_co] | _InputGuardrailFuncAsync[TContext_co] | None
|
The guardrail function to wrap. |
None
|
name
|
str | None
|
Optional name for the guardrail. If not provided, uses the function's name. |
None
|
run_in_parallel
|
bool
|
Whether to run the guardrail concurrently with the agent (True, default) or before the agent starts (False). |
True
|
Source code in src/agents/guardrail.py
output_guardrail
output_guardrail(
func: _OutputGuardrailFuncSync[TContext_co],
) -> OutputGuardrail[TContext_co]
output_guardrail(
func: _OutputGuardrailFuncAsync[TContext_co],
) -> OutputGuardrail[TContext_co]
output_guardrail(
*, name: str | None = None
) -> Callable[
[
_OutputGuardrailFuncSync[TContext_co]
| _OutputGuardrailFuncAsync[TContext_co]
],
OutputGuardrail[TContext_co],
]
output_guardrail(
func: _OutputGuardrailFuncSync[TContext_co]
| _OutputGuardrailFuncAsync[TContext_co]
| None = None,
*,
name: str | None = None,
) -> (
OutputGuardrail[TContext_co]
| Callable[
[
_OutputGuardrailFuncSync[TContext_co]
| _OutputGuardrailFuncAsync[TContext_co]
],
OutputGuardrail[TContext_co],
]
)
Decorator that transforms a sync or async function into an OutputGuardrail.
It can be used directly (no parentheses) or with keyword args, e.g.:
@output_guardrail
def my_sync_guardrail(...): ...
@output_guardrail(name="guardrail_name")
async def my_async_guardrail(...): ...
Source code in src/agents/guardrail.py
function_tool
function_tool(
func: ToolFunction[...],
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction | None = None,
strict_mode: bool = True,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], AgentBase],
MaybeAwaitable[bool],
] = True,
needs_approval: bool
| Callable[
[RunContextWrapper[Any], dict[str, Any], str],
Awaitable[bool],
] = False,
tool_input_guardrails: list[ToolInputGuardrail[Any]]
| None = None,
tool_output_guardrails: list[ToolOutputGuardrail[Any]]
| None = None,
timeout: 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,
allowed_callers: list[ToolCaller] | None = None,
output_type: Any | None = None,
output_json_schema: dict[str, Any] | None = None,
) -> FunctionTool
function_tool(
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction | None = None,
strict_mode: bool = True,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], AgentBase],
MaybeAwaitable[bool],
] = True,
needs_approval: bool
| Callable[
[RunContextWrapper[Any], dict[str, Any], str],
Awaitable[bool],
] = False,
tool_input_guardrails: list[ToolInputGuardrail[Any]]
| None = None,
tool_output_guardrails: list[ToolOutputGuardrail[Any]]
| None = None,
timeout: 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,
allowed_callers: list[ToolCaller] | None = None,
output_type: Any | None = None,
output_json_schema: dict[str, Any] | None = None,
) -> Callable[[ToolFunction[...]], FunctionTool]
function_tool(
func: ToolFunction[...] | None = None,
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction
| None
| object = _UNSET_FAILURE_ERROR_FUNCTION,
strict_mode: bool = True,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], AgentBase],
MaybeAwaitable[bool],
] = True,
needs_approval: bool
| Callable[
[RunContextWrapper[Any], dict[str, Any], str],
Awaitable[bool],
] = False,
tool_input_guardrails: list[ToolInputGuardrail[Any]]
| None = None,
tool_output_guardrails: list[ToolOutputGuardrail[Any]]
| None = None,
timeout: 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,
allowed_callers: list[ToolCaller] | None = None,
output_type: Any | None = None,
output_json_schema: dict[str, Any] | None = None,
) -> (
FunctionTool
| Callable[[ToolFunction[...]], FunctionTool]
)
Decorator to create a FunctionTool from a function. By default, we will: 1. Parse the function signature to create a JSON schema for the tool's parameters. 2. Use the function's docstring to populate the tool's description. 3. Use the function's docstring to populate argument descriptions. The docstring style is detected automatically, but you can override it.
If the function takes a RunContextWrapper as the first argument, it must match the
context type of the agent that uses the tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
ToolFunction[...] | None
|
The function to wrap. |
None
|
name_override
|
str | None
|
If provided, use this name for the tool instead of the function's name. |
None
|
description_override
|
str | None
|
If provided, use this description for the tool instead of the function's docstring. |
None
|
docstring_style
|
DocstringStyle | None
|
If provided, use this style for the tool's docstring. If not provided, we will attempt to auto-detect the style. |
None
|
use_docstring_info
|
bool
|
If True, use the function's docstring to populate the tool's description and argument descriptions. |
True
|
failure_error_function
|
ToolErrorFunction | None | object
|
If provided, use this function to generate an error message when the tool call fails. The error message is sent to the LLM. If you pass None, then no error message will be sent and instead an Exception will be raised. |
_UNSET_FAILURE_ERROR_FUNCTION
|
strict_mode
|
bool
|
Whether to enable strict mode for the tool's JSON schema. We strongly recommend setting this to True, as it increases the likelihood of correct JSON input. If False, it allows non-strict JSON schemas. For example, if a parameter has a default value, it will be optional, additional properties are allowed, etc. See here for more: https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas |
True
|
is_enabled
|
bool | Callable[[RunContextWrapper[Any], AgentBase], MaybeAwaitable[bool]]
|
Whether the tool is enabled. Can be a bool or a callable that takes the run context and agent and returns whether the tool is enabled. Disabled tools are hidden from the LLM at runtime. |
True
|
needs_approval
|
bool | Callable[[RunContextWrapper[Any], dict[str, Any], str], Awaitable[bool]]
|
Whether the tool needs approval before execution. If True, the run will be interrupted and the tool call will need to be approved using RunState.approve() or rejected using RunState.reject() before continuing. Can be a bool (always/never needs approval) or a function that takes (run_context, tool_parameters, call_id) and returns whether this specific call needs approval. |
False
|
tool_input_guardrails
|
list[ToolInputGuardrail[Any]] | None
|
Optional list of guardrails to run before invoking the tool. |
None
|
tool_output_guardrails
|
list[ToolOutputGuardrail[Any]] | None
|
Optional list of guardrails to run after the tool returns. |
None
|
timeout
|
float | None
|
Optional timeout in seconds for each tool call. |
None
|
timeout_behavior
|
ToolTimeoutBehavior
|
Timeout handling mode. "error_as_result" returns a model-visible message, while "raise_exception" raises ToolTimeoutError and fails the run. |
'error_as_result'
|
timeout_error_function
|
ToolErrorFunction | None
|
Optional formatter used for timeout messages when timeout_behavior="error_as_result". |
None
|
defer_loading
|
bool
|
Whether to hide this tool definition until Responses API tool search explicitly loads it. |
False
|
custom_data_extractor
|
FunctionToolCustomDataExtractor | None
|
Optional callback that returns SDK-only custom data to attach to
the emitted |
None
|
allowed_callers
|
list[ToolCaller] | None
|
Callers that may invoke the tool on OpenAI Responses models. Include
|
None
|
output_type
|
Any | None
|
Optional Python output type used to generate and validate a strict output schema. For programmatic tools this is inferred from a structured return annotation when omitted. Use this override when the callable has no usable return annotation. |
None
|
output_json_schema
|
dict[str, Any] | None
|
Optional JSON Schema describing the tool's output for programmatic
callers. This low-level escape hatch is mutually exclusive with |
None
|
Source code in src/agents/tool.py
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 | |
tool_input_guardrail
tool_input_guardrail(
func: _ToolInputFuncSync,
) -> ToolInputGuardrail[Any]
tool_input_guardrail(
func: _ToolInputFuncAsync,
) -> ToolInputGuardrail[Any]
tool_input_guardrail(
*, name: str | None = None
) -> Callable[
[_ToolInputFuncSync | _ToolInputFuncAsync],
ToolInputGuardrail[Any],
]
tool_input_guardrail(
func: _ToolInputFuncSync
| _ToolInputFuncAsync
| None = None,
*,
name: str | None = None,
) -> (
ToolInputGuardrail[Any]
| Callable[
[_ToolInputFuncSync | _ToolInputFuncAsync],
ToolInputGuardrail[Any],
]
)
Decorator to create a ToolInputGuardrail from a function.
Source code in src/agents/tool_guardrails.py
tool_output_guardrail
tool_output_guardrail(
func: _ToolOutputFuncSync,
) -> ToolOutputGuardrail[Any]
tool_output_guardrail(
func: _ToolOutputFuncAsync,
) -> ToolOutputGuardrail[Any]
tool_output_guardrail(
*, name: str | None = None
) -> Callable[
[_ToolOutputFuncSync | _ToolOutputFuncAsync],
ToolOutputGuardrail[Any],
]
tool_output_guardrail(
func: _ToolOutputFuncSync
| _ToolOutputFuncAsync
| None = None,
*,
name: str | None = None,
) -> (
ToolOutputGuardrail[Any]
| Callable[
[_ToolOutputFuncSync | _ToolOutputFuncAsync],
ToolOutputGuardrail[Any],
]
)
Decorator to create a ToolOutputGuardrail from a function.