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(...): ...
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
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
|
ソースコード位置: 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(...): ...
ソースコード位置: 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.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
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
|
ソースコード位置: src/agents/tool.py
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 2613 2614 2615 2616 2617 2618 2619 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 | |
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.
ソースコード位置: 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.