Skip to content

handoff() helper crashes when passed a callable (lazy loader) #2115

@ohcedar

Description

@ohcedar

helper crashes when passed a callable (lazy loader)
SDK Version: openai-agents==0.6.1

Summary
The handoff() helper function documentation states that the agent argument can be "The agent to handoff to, or a function that returns an agent." However, passing a function causes an AttributeError because the code attempts to access .name on the function object itself, rather than on the agent returned by the function.

Steps to Reproduce

from agents import Agent, handoff
def get_target_agent():
    return Agent(name="Target", instructions="You are a target.")
# This crashes
h = handoff(get_target_agent)

Traceback:

File ".../agents/handoffs/__init__.py", line 302, in handoff
    agent_name=agent.name,
               ^^^^^^^^^^
AttributeError: 'function' object has no attribute 'name'

Cause In agents/handoffs/init.py, the handoff function implementation accesses agent.name at line 302 (in version 0.6.1) to set the agent_name field of the Handoff object.

return Handoff(
    # ...
    agent_name=agent.name,  # <--- BUG: Assumes 'agent' is an Agent instance
    # ...
)

If agent is a callable (as permitted by the type hint and docstring), this line fails.

Suggested Fix
The helper should check if agent is callable. If it is, it cannot determine the agent's name immediately. It should either:

Require tool_name_override and a new agent_name_override argument when a callable is used.
Or, accept that agent_name might be deferred or require the user to supply it explicitly.
Currently, the only workaround is to instantiate the Handoff class directly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions