-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Please read this first
- Have you read the docs? Yes - Agents SDK docs
- Have you searched for related issues? Yes - no similar issues found
Describe the bug
When multiple agents are created dynamically (e.g., inside a function or Temporal workflow), the handoff() function routes all handoffs to the last agent created instead of the intended target agent. This completely breaks multi-agent orchestration in systems where agents are created dynamically.
Impact: Critical - makes dynamic multi-agent handoff routing unusable.
Debug information
- Agents SDK version: >=0.6.4
- Python version: Python 3.12.9
Repro steps
How we create bidirectional handoffs in our Temporal workflow:
from agents import Agent, handoff
def setup_agents(workflow_instance, mcp_server, guardrail):
# Create all agents first
agent_manager = create_agent_manager(workflow_instance, mcp_server, guardrail)
agent_planner = create_agent_planner(workflow_instance, mcp_server)
agent_structure_builder = create_structure_builder_agent(workflow_instance, mcp_server)
agent_designer = create_agent_designer(workflow_instance, mcp_server)
agent_content_creator = create_content_creator_agent(workflow_instance, mcp_server)
agent_small_edits = create_agent_small_edits(workflow_instance, mcp_server)
# Configure manager → specialist handoffs
agent_manager.handoffs = [
handoff(agent=agent_planner),
handoff(agent=agent_structure_builder),
handoff(agent=agent_designer),
handoff(agent=agent_content_creator),
handoff(agent=agent_small_edits),
]
# Configure specialist → manager handoffs (bidirectional)
agent_planner.handoffs = [handoff(agent=agent_manager)]
agent_structure_builder.handoffs = [handoff(agent=agent_manager)]
agent_designer.handoffs = [handoff(agent=agent_manager)]
agent_content_creator.handoffs = [handoff(agent=agent_manager)]
agent_small_edits.handoffs = [handoff(agent=agent_manager)]
return agent_managerExpected behavior
Each handoff should route to its designated agent:
transfer_to_planner→ routes to Planner agenttransfer_to_designer→ routes to Designer agenttransfer_to_smalledits→ routes to SmallEdits agent
Actual behavior
All handoffs route to the last agent created (SmallEdits in our case), regardless of which transfer_to_* tool is called.
Diagnostic logs proving the bug:
Add these logs to verify the issue:
- In
agents/handoffs/__init__.py, inside_invoke_handoffclosure:
import logging
logger = logging.getLogger("openai.agents.handoff")
logger.info(f"[HANDOFF_INVOKE] agent_name={agent.name} agent_id={id(agent)}")- In
agents/_run_impl.py, inexecute_handoffs():
logger.info(f"[EXECUTE_HANDOFFS_INVOKE_BEFORE] from_agent={agent.name} "
f"tool_name={handoff.tool_name} target_agent_name={handoff.agent_name}")
new_agent = await handoff.on_invoke_handoff(context_wrapper, actual_handoff.tool_call.arguments)
logger.info(f"[EXECUTE_HANDOFFS_INVOKE_AFTER] returned_agent_name={new_agent.name}")Output showing the bug:
[EXECUTE_HANDOFFS_INVOKE_BEFORE] from_agent=Manager tool_name=transfer_to_planner target_agent_name=Planner
[HANDOFF_INVOKE] agent_name=SmallEdits agent_id=4524224448
[EXECUTE_HANDOFFS_INVOKE_AFTER] returned_agent_name=SmallEdits
Manager tries to handoff to Planner, but closure returns SmallEdits.