Handoffs
HandoffInputFiltermodule-attribute
HandoffInputFilter:TypeAlias=Callable[[HandoffInputData],MaybeAwaitable[HandoffInputData]]A function that filters the input data passed to the next agent.
HandoffHistoryMappermodule-attribute
HandoffHistoryMapper:TypeAlias=Callable[[list[TResponseInputItem]],list[TResponseInputItem]]A function that maps the previous transcript to the nested summary payload.
HandoffInputDatadataclass
Source code insrc/agents/handoffs/__init__.py
input_historyinstance-attribute
input_history:str|tuple[TResponseInputItem,...]The input history beforeRunner.run() was called.
pre_handoff_itemsinstance-attribute
pre_handoff_items:tuple[RunItem,...]The items generated before the agent turn where the handoff was invoked.
new_itemsinstance-attribute
new_items:tuple[RunItem,...]The new items generated during the current agent turn, including the item that triggered thehandoff and the tool output message representing the response from the handoff output.
run_contextclass-attributeinstance-attribute
run_context:RunContextWrapper[Any]|None=NoneThe run context at the time the handoff was invoked. Note that, since this property was addedlater on, it is optional for backwards compatibility.
clone
clone(**kwargs:Any)->HandoffInputDataMake a copy of the handoff input data, with the given arguments changed. For example, youcould do:
Source code insrc/agents/handoffs/__init__.py
Handoffdataclass
Bases:Generic[TContext,TAgent]
A handoff is when an agent delegates a task to another agent.
For example, in a customer support scenario you might have a "triage agent" that determineswhich agent should handle the user's request, and sub-agents that specialize in different areaslike billing, account management, etc.
Source code insrc/agents/handoffs/__init__.py
tool_descriptioninstance-attribute
The description of the tool that represents the handoff.
input_json_schemainstance-attribute
The JSON schema for the handoff input. Can be empty if the handoff does not take an input.
on_invoke_handoffinstance-attribute
on_invoke_handoff:Callable[[RunContextWrapper[Any],str],Awaitable[TAgent]]The function that invokes the handoff.
The parameters passed are: (1) the handoff run context, (2) the arguments from the LLM as aJSON string (or an empty string ifinput_json_schema is empty). Must return an agent.
input_filterclass-attributeinstance-attribute
input_filter:HandoffInputFilter|None=NoneA function that filters the inputs that are passed to the next agent.
By default, the new agent sees the entire conversation history. In some cases, you may want tofilter inputs (for example, to remove older inputs or remove tools from existing inputs). Thefunction receives the entire conversation history so far, including the input item thattriggered the handoff and a tool call output item representing the handoff tool's output. Youare free to modify the input history or new items as you see fit. The next agent that runs willreceivehandoff_input_data.all_items. IMPORTANT: in streaming mode, we will not streamanything as a result of this function. The items generated before will already have beenstreamed.
nest_handoff_historyclass-attributeinstance-attribute
Override the run-levelnest_handoff_history behavior for this handoff only.
strict_json_schemaclass-attributeinstance-attribute
Whether the input JSON schema is in strict mode. We strongly recommend setting this to Truebecause it increases the likelihood of correct JSON input.
is_enabledclass-attributeinstance-attribute
is_enabled:(bool|Callable[[RunContextWrapper[Any],AgentBase[Any]],MaybeAwaitable[bool],])=TrueWhether the handoff is enabled.
Either a bool or a callable that takes the run context and agent and returns whether thehandoff is enabled. You can use this to dynamically enable or disable a handoff based on yourcontext or state.
default_handoff_history_mapper
default_handoff_history_mapper(transcript:list[TResponseInputItem],)->list[TResponseInputItem]Return a single assistant message summarizing the transcript.
get_conversation_history_wrappers
Return the current start/end markers used for the nested conversation summary.
nest_handoff_history
nest_handoff_history(handoff_input_data:HandoffInputData,*,history_mapper:HandoffHistoryMapper|None=None,)->HandoffInputDataSummarize the previous transcript for the next agent.
Source code insrc/agents/handoffs/history.py
reset_conversation_history_wrappers
Restore the default<CONVERSATION HISTORY> markers.
Source code insrc/agents/handoffs/history.py
set_conversation_history_wrappers
Override the markers that wrap the generated conversation summary.
PassNone to leave either side unchanged.
Source code insrc/agents/handoffs/history.py
handoff
handoff(agent:Agent[TContext],*,tool_name_override:str|None=None,tool_description_override:str|None=None,input_filter:Callable[[HandoffInputData],HandoffInputData]|None=None,nest_handoff_history:bool|None=None,is_enabled:bool|Callable[[RunContextWrapper[Any],Agent[Any]],MaybeAwaitable[bool],]=True,)->Handoff[TContext,Agent[TContext]]handoff(agent:Agent[TContext],*,on_handoff:OnHandoffWithInput[THandoffInput],input_type:type[THandoffInput],tool_description_override:str|None=None,tool_name_override:str|None=None,input_filter:Callable[[HandoffInputData],HandoffInputData]|None=None,nest_handoff_history:bool|None=None,is_enabled:bool|Callable[[RunContextWrapper[Any],Agent[Any]],MaybeAwaitable[bool],]=True,)->Handoff[TContext,Agent[TContext]]handoff(agent:Agent[TContext],*,on_handoff:OnHandoffWithoutInput,tool_description_override:str|None=None,tool_name_override:str|None=None,input_filter:Callable[[HandoffInputData],HandoffInputData]|None=None,nest_handoff_history:bool|None=None,is_enabled:bool|Callable[[RunContextWrapper[Any],Agent[Any]],MaybeAwaitable[bool],]=True,)->Handoff[TContext,Agent[TContext]]handoff(agent:Agent[TContext],tool_name_override:str|None=None,tool_description_override:str|None=None,on_handoff:OnHandoffWithInput[THandoffInput]|OnHandoffWithoutInput|None=None,input_type:type[THandoffInput]|None=None,input_filter:Callable[[HandoffInputData],HandoffInputData]|None=None,nest_handoff_history:bool|None=None,is_enabled:bool|Callable[[RunContextWrapper[Any],Agent[TContext]],MaybeAwaitable[bool],]=True,)->Handoff[TContext,Agent[TContext]]Create a handoff from an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent | Agent[TContext] | The agent to handoff to. | required |
tool_name_override | str | None | Optional override for the name of the tool that represents the handoff. | None |
tool_description_override | str | None | Optional override for the description of the tool thatrepresents the handoff. | None |
on_handoff | OnHandoffWithInput[THandoffInput] |OnHandoffWithoutInput | None | A function that runs when the handoff is invoked. | None |
input_type | type[THandoffInput] | None | The type of the input to the handoff. If provided, the input will be validatedagainst this type. Only relevant if you pass a function that takes an input. | None |
input_filter | Callable[[HandoffInputData],HandoffInputData] | None | A function that filters the inputs that are passed to the next agent. | None |
nest_handoff_history | bool | None | Optional override for the RunConfig-level | None |
is_enabled | bool |Callable[[RunContextWrapper[Any],Agent[TContext]],MaybeAwaitable[bool]] | Whether the handoff is enabled. Can be a bool or a callable that takes the runcontext and agent and returns whether the handoff is enabled. Disabled handoffs arehidden from the LLM at runtime. | True |
Source code insrc/agents/handoffs/__init__.py
197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 | |