Skip to content

Commit 6d95a3f

Browse files
authored
Mark handoff span as errored when multiple handoffs are requested (#344)
Also includes the set of requested agents in the error data. <img width="968" alt="image" src="https://github.com/user-attachments/assets/0c5c2e81-08f7-445c-bbb0-3e169ef744a5" />
1 parent 2261aab commit 6d95a3f

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

Diff for: src/agents/_run_impl.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ async def execute_handoffs(
529529
run_config: RunConfig,
530530
) -> SingleStepResult:
531531
# If there is more than one handoff, add tool responses that reject those handoffs
532-
if len(run_handoffs) > 1:
532+
multiple_handoffs = len(run_handoffs) > 1
533+
if multiple_handoffs:
533534
output_message = "Multiple handoffs detected, ignoring this one."
534535
new_step_items.extend(
535536
[
@@ -551,6 +552,16 @@ async def execute_handoffs(
551552
context_wrapper, actual_handoff.tool_call.arguments
552553
)
553554
span_handoff.span_data.to_agent = new_agent.name
555+
if multiple_handoffs:
556+
requested_agents = [handoff.handoff.agent_name for handoff in run_handoffs]
557+
span_handoff.set_error(
558+
SpanError(
559+
message="Multiple handoffs requested",
560+
data={
561+
"requested_agents": requested_agents,
562+
},
563+
)
564+
)
554565

555566
# Append a tool output item for the handoff
556567
new_step_items.append(

Diff for: tests/test_tracing_errors.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,18 @@ async def test_multiple_handoff_doesnt_error():
244244
},
245245
},
246246
{"type": "generation"},
247-
{"type": "handoff", "data": {"from_agent": "test", "to_agent": "test"}},
247+
{"type": "handoff",
248+
"data": {"from_agent": "test", "to_agent": "test"},
249+
"error": {
250+
"data": {
251+
"requested_agents": [
252+
"test",
253+
"test",
254+
],
255+
},
256+
"message": "Multiple handoffs requested",
257+
},
258+
},
248259
],
249260
},
250261
{
@@ -372,7 +383,19 @@ async def test_handoffs_lead_to_correct_agent_spans():
372383
{"type": "generation"},
373384
{
374385
"type": "handoff",
375-
"data": {"from_agent": "test_agent_3", "to_agent": "test_agent_1"},
386+
"data": {
387+
"from_agent": "test_agent_3",
388+
"to_agent": "test_agent_1"
389+
},
390+
"error": {
391+
"data": {
392+
"requested_agents": [
393+
"test_agent_1",
394+
"test_agent_2",
395+
],
396+
},
397+
"message": "Multiple handoffs requested",
398+
},
376399
},
377400
],
378401
},

Diff for: tests/test_tracing_errors_streamed.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,14 @@ async def test_multiple_handoff_doesnt_error():
262262
},
263263
},
264264
{"type": "generation"},
265-
{"type": "handoff", "data": {"from_agent": "test", "to_agent": "test"}},
265+
{
266+
"type": "handoff",
267+
"data": {"from_agent": "test", "to_agent": "test"},
268+
"error": {
269+
"data": {"requested_agents": ["test", "test"]},
270+
"message": "Multiple handoffs requested",
271+
},
272+
},
266273
],
267274
},
268275
{
@@ -396,6 +403,10 @@ async def test_handoffs_lead_to_correct_agent_spans():
396403
{"type": "generation"},
397404
{
398405
"type": "handoff",
406+
"error": {
407+
"message": "Multiple handoffs requested",
408+
"data": {"requested_agents": ["test_agent_1", "test_agent_2"]},
409+
},
399410
"data": {"from_agent": "test_agent_3", "to_agent": "test_agent_1"},
400411
},
401412
],

0 commit comments

Comments
 (0)