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
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 | |
tool_input_guardrail
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(
*, 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.