Skip to content

Triage agent can not delegate task to handoff agent #575

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

Closed
eccozhou opened this issue Apr 23, 2025 · 6 comments
Closed

Triage agent can not delegate task to handoff agent #575

eccozhou opened this issue Apr 23, 2025 · 6 comments
Labels
question Question about using the SDK stale

Comments

@eccozhou
Copy link

eccozhou commented Apr 23, 2025

Question

I want to output which agent is used for reponse, but it seems not working.

It seems triage_agent is not delegate task to any handoff agent, he output the response by himself.

Code

class HomeworkOutput(BaseModel):
    is_homework: bool
    reasoning: str

guardrail_agent = Agent(
    name="Guardrail check",
    instructions="Check if the user is asking about homework.",
    output_type=HomeworkOutput,
)

math_tutor_agent = Agent(
    name="Math Tutor",
    handoff_description="Specialist agent for math questions",
    instructions="You provide help with math problems. Explain your reasoning at each step and include examples",
)

history_tutor_agent = Agent(
    name="History Tutor",
    handoff_description="Specialist agent for historical questions",
    instructions="You provide assistance with historical queries. Explain important events and context clearly.",
)

def on_handoff(ctx: RunContextWrapper):
    print("Math handoff triggered")

math_handoff = handoff(
    agent=math_tutor_agent,
    on_handoff=on_handoff,
)

triage_agent = Agent(
    name="Triage Agent",
    instructions="You determine which agent to use based on the user's homework question",
    handoffs=[history_tutor_agent, math_handoff],
)

async def main():
    result = await Runner.run(triage_agent, "who was the first president of the united states?")
    print(result.final_output)

if __name__ == "__main__":
    asyncio.run(main())
@eccozhou eccozhou added the question Question about using the SDK label Apr 23, 2025
@rm-openai
Copy link
Collaborator

Can you try using the recommended prompts? that may help

https://openai.github.io/openai-agents-python/handoffs/#recommended-prompts

@eccozhou
Copy link
Author

It's still not working.

@rm-openai
Copy link
Collaborator

It works fine for me, see script below. I think the problem may be your on_handoff - it is only provided to the math_handoff, but the question is a history question.

import asyncio

from pydantic import BaseModel

from agents import Agent, RunContextWrapper, Runner, handoff
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions


class HomeworkOutput(BaseModel):
    is_homework: bool
    reasoning: str


guardrail_agent = Agent(
    name="Guardrail check",
    instructions="Check if the user is asking about homework.",
    output_type=HomeworkOutput,
)

math_tutor_agent = Agent(
    name="Math Tutor",
    handoff_description="Specialist agent for math questions",
    instructions=prompt_with_handoff_instructions(
        "You provide help with math problems. Explain your reasoning at each step and include examples"
    ),
)

history_tutor_agent = Agent(
    name="History Tutor",
    handoff_description="Specialist agent for historical questions",
    instructions=prompt_with_handoff_instructions(
        "You provide assistance with historical queries. Explain important events and context clearly."
    ),
)


def on_handoff(ctx: RunContextWrapper):
    print("Math handoff triggered")


math_handoff = handoff(
    agent=math_tutor_agent,
    on_handoff=on_handoff,
)

triage_agent = Agent(
    name="Triage Agent",
    instructions=prompt_with_handoff_instructions(
        "You determine which agent to use based on the user's homework question"
    ),
    handoffs=[history_tutor_agent, math_handoff],
)


async def main():
    result = await Runner.run(triage_agent, "who was the first president of the united states?")
    print(result.last_agent.name)
    print(result.final_output)


if __name__ == "__main__":
    asyncio.run(main())

Output


History Tutor
The first president of the United States was George Washington. He served from 1789 to 1797 and is often called the "Father of His Country" for his pivotal role in the founding of the nation. Washington set many important precedents for the national government and the presidency in particular.

@nvenkat94
Copy link

Hi @eccozhou ,

Could you try result.last_agent.name instead of result.final_output. This will print the last used agent.

Copy link

github-actions bot commented May 6, 2025

This issue is stale because it has been open for 7 days with no activity.

@github-actions github-actions bot added the stale label May 6, 2025
Copy link

github-actions bot commented May 9, 2025

This issue was closed because it has been inactive for 3 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about using the SDK stale
Projects
None yet
Development

No branches or pull requests

3 participants