Skip to content

Mark handoff span as errored when multiple handoffs are requested #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/agents/_run_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ async def execute_handoffs(
run_config: RunConfig,
) -> SingleStepResult:
# If there is more than one handoff, add tool responses that reject those handoffs
if len(run_handoffs) > 1:
multiple_handoffs = len(run_handoffs) > 1
if multiple_handoffs:
output_message = "Multiple handoffs detected, ignoring this one."
new_step_items.extend(
[
Expand All @@ -551,6 +552,16 @@ async def execute_handoffs(
context_wrapper, actual_handoff.tool_call.arguments
)
span_handoff.span_data.to_agent = new_agent.name
if multiple_handoffs:
requested_agents = [handoff.handoff.agent_name for handoff in run_handoffs]
span_handoff.set_error(
SpanError(
message="Multiple handoffs requested",
data={
"requested_agents": requested_agents,
},
)
)

# Append a tool output item for the handoff
new_step_items.append(
Expand Down
27 changes: 25 additions & 2 deletions tests/test_tracing_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,18 @@ async def test_multiple_handoff_doesnt_error():
},
},
{"type": "generation"},
{"type": "handoff", "data": {"from_agent": "test", "to_agent": "test"}},
{"type": "handoff",
"data": {"from_agent": "test", "to_agent": "test"},
"error": {
"data": {
"requested_agents": [
"test",
"test",
],
},
"message": "Multiple handoffs requested",
},
},
],
},
{
Expand Down Expand Up @@ -372,7 +383,19 @@ async def test_handoffs_lead_to_correct_agent_spans():
{"type": "generation"},
{
"type": "handoff",
"data": {"from_agent": "test_agent_3", "to_agent": "test_agent_1"},
"data": {
"from_agent": "test_agent_3",
"to_agent": "test_agent_1"
},
"error": {
"data": {
"requested_agents": [
"test_agent_1",
"test_agent_2",
],
},
"message": "Multiple handoffs requested",
},
},
],
},
Expand Down
13 changes: 12 additions & 1 deletion tests/test_tracing_errors_streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,14 @@ async def test_multiple_handoff_doesnt_error():
},
},
{"type": "generation"},
{"type": "handoff", "data": {"from_agent": "test", "to_agent": "test"}},
{
"type": "handoff",
"data": {"from_agent": "test", "to_agent": "test"},
"error": {
"data": {"requested_agents": ["test", "test"]},
"message": "Multiple handoffs requested",
},
},
],
},
{
Expand Down Expand Up @@ -396,6 +403,10 @@ async def test_handoffs_lead_to_correct_agent_spans():
{"type": "generation"},
{
"type": "handoff",
"error": {
"message": "Multiple handoffs requested",
"data": {"requested_agents": ["test_agent_1", "test_agent_2"]},
},
"data": {"from_agent": "test_agent_3", "to_agent": "test_agent_1"},
},
],
Expand Down