Transform this agent into a tool, callable by other agents. This is different from handoffs in two ways: 1. In handoffs, the new agent receives the conversation history. In this tool, the new agent receives generated input. 2. In handoffs, the new agent takes over
(
self,
tool_name: str | None,
tool_description: str | None,
custom_output_extractor: (
Callable[[RunResult | RunResultStreaming], Awaitable[str]] | None
) = None,
is_enabled: bool
| Callable[[RunContextWrapper[Any], AgentBase[Any]], MaybeAwaitable[bool]] = True,
on_stream: Callable[[AgentToolStreamEvent], MaybeAwaitable[None]] | None = None,
run_config: RunConfig | None = None,
max_turns: int | None = None,
hooks: RunHooks[TContext] | None = None,
previous_response_id: str | None = None,
conversation_id: str | None = None,
session: Session | None = None,
failure_error_function: ToolErrorFunction | None = default_tool_error_function,
needs_approval: bool
| Callable[[RunContextWrapper[Any], dict[str, Any], str], Awaitable[bool]] = False,
parameters: type[Any] | None = None,
input_builder: StructuredToolInputBuilder | None = None,
include_input_schema: bool = False,
)
| 506 | return dataclasses.replace(self, **kwargs) |
| 507 | |
| 508 | def as_tool( |
| 509 | self, |
| 510 | tool_name: str | None, |
| 511 | tool_description: str | None, |
| 512 | custom_output_extractor: ( |
| 513 | Callable[[RunResult | RunResultStreaming], Awaitable[str]] | None |
| 514 | ) = None, |
| 515 | is_enabled: bool |
| 516 | | Callable[[RunContextWrapper[Any], AgentBase[Any]], MaybeAwaitable[bool]] = True, |
| 517 | on_stream: Callable[[AgentToolStreamEvent], MaybeAwaitable[None]] | None = None, |
| 518 | run_config: RunConfig | None = None, |
| 519 | max_turns: int | None = None, |
| 520 | hooks: RunHooks[TContext] | None = None, |
| 521 | previous_response_id: str | None = None, |
| 522 | conversation_id: str | None = None, |
| 523 | session: Session | None = None, |
| 524 | failure_error_function: ToolErrorFunction | None = default_tool_error_function, |
| 525 | needs_approval: bool |
| 526 | | Callable[[RunContextWrapper[Any], dict[str, Any], str], Awaitable[bool]] = False, |
| 527 | parameters: type[Any] | None = None, |
| 528 | input_builder: StructuredToolInputBuilder | None = None, |
| 529 | include_input_schema: bool = False, |
| 530 | ) -> FunctionTool: |
| 531 | """Transform this agent into a tool, callable by other agents. |
| 532 | |
| 533 | This is different from handoffs in two ways: |
| 534 | 1. In handoffs, the new agent receives the conversation history. In this tool, the new agent |
| 535 | receives generated input. |
| 536 | 2. In handoffs, the new agent takes over the conversation. In this tool, the new agent is |
| 537 | called as a tool, and the conversation is continued by the original agent. |
| 538 | |
| 539 | Args: |
| 540 | tool_name: The name of the tool. If not provided, the agent's name will be used. |
| 541 | tool_description: The description of the tool, which should indicate what it does and |
| 542 | when to use it. |
| 543 | custom_output_extractor: A function that extracts the output from the agent. If not |
| 544 | provided, the last message from the agent will be used. Nested run results expose |
| 545 | `agent_tool_invocation` metadata when this agent is invoked via `as_tool()`. |
| 546 | is_enabled: Whether the tool is enabled. Can be a bool or a callable that takes the run |
| 547 | context and agent and returns whether the tool is enabled. Disabled tools are hidden |
| 548 | from the LLM at runtime. |
| 549 | on_stream: Optional callback (sync or async) to receive streaming events from the nested |
| 550 | agent run. The callback receives an `AgentToolStreamEvent` containing the nested |
| 551 | agent, the originating tool call (when available), and each stream event. When |
| 552 | provided, the nested agent is executed in streaming mode. |
| 553 | failure_error_function: If provided, generate an error message when the tool (agent) run |
| 554 | fails. The message is sent to the LLM. If None, the exception is raised instead. |
| 555 | needs_approval: Bool or callable to decide if this agent tool should pause for approval. |
| 556 | parameters: Structured input type for the tool arguments (dataclass or Pydantic model). |
| 557 | input_builder: Optional function to build the nested agent input from structured data. |
| 558 | include_input_schema: Whether to include the full JSON schema in structured input. |
| 559 | """ |
| 560 | |
| 561 | def _is_supported_parameters(value: Any) -> bool: |
| 562 | if not isinstance(value, type): |
| 563 | return False |
| 564 | if dataclasses.is_dataclass(value): |
| 565 | return True |