Skip to content

Commit 843b081

Browse files
committed
Added support for "return" handoffs (#1)
1 parent b58b039 commit 843b081

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/agents/handoffs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ class Handoff(Generic[TContext]):
111111
agent and returns whether the handoff is enabled. You can use this to dynamically enable/disable
112112
a handoff based on your context/state."""
113113

114+
should_return_control: bool = False
115+
"""Whether the Agent that receives control during a handoff should return control to the
116+
original (previous) Agent upon completion of its work. If False, after the Agent that received
117+
the handoff completes its work, the interaction will end.
118+
"""
119+
114120
def get_transfer_message(self, agent: Agent[Any]) -> str:
115121
return json.dumps({"assistant": agent.name})
116122

@@ -135,6 +141,7 @@ def handoff(
135141
input_filter: Callable[[HandoffInputData], HandoffInputData] | None = None,
136142
should_return_control: bool = False,
137143
is_enabled: bool | Callable[[RunContextWrapper[Any], Agent[Any]], MaybeAwaitable[bool]] = True,
144+
should_return_control: bool = False,
138145
) -> Handoff[TContext]: ...
139146

140147

@@ -149,6 +156,7 @@ def handoff(
149156
input_filter: Callable[[HandoffInputData], HandoffInputData] | None = None,
150157
should_return_control: bool = False,
151158
is_enabled: bool | Callable[[RunContextWrapper[Any], Agent[Any]], MaybeAwaitable[bool]] = True,
159+
should_return_control: bool = False,
152160
) -> Handoff[TContext]: ...
153161

154162

@@ -162,6 +170,7 @@ def handoff(
162170
input_filter: Callable[[HandoffInputData], HandoffInputData] | None = None,
163171
should_return_control: bool = False,
164172
is_enabled: bool | Callable[[RunContextWrapper[Any], Agent[Any]], MaybeAwaitable[bool]] = True,
173+
should_return_control: bool = False,
165174
) -> Handoff[TContext]: ...
166175

167176

@@ -174,6 +183,7 @@ def handoff(
174183
input_filter: Callable[[HandoffInputData], HandoffInputData] | None = None,
175184
should_return_control: bool = False,
176185
is_enabled: bool | Callable[[RunContextWrapper[Any], Agent[Any]], MaybeAwaitable[bool]] = True,
186+
should_return_control: bool = False,
177187
) -> Handoff[TContext]:
178188
"""Create a handoff from an agent.
179189
@@ -258,4 +268,5 @@ async def _invoke_handoff(
258268
agent_name=agent.name,
259269
should_return_control=should_return_control,
260270
is_enabled=is_enabled,
271+
should_return_control=should_return_control,
261272
)

tests/test_run_step_execution.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ async def get_execute_result(
371371
output_schema = AgentRunner._get_output_schema(agent)
372372
previous_agents = previous_agents if previous_agents is not None else []
373373
handoffs = await AgentRunner._get_handoffs(agent, context_wrapper or RunContextWrapper(None))
374+
previous_agents = previous_agents if previous_agents is not None else []
374375

375376
processed_response = RunImpl.process_model_response(
376377
agent=agent,

0 commit comments

Comments
 (0)