-
Notifications
You must be signed in to change notification settings - Fork 1.4k
is there a way to block the handoff to an agent based on a custom logic ? #585
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
Labels
question
Question about using the SDK
Comments
class HandoffInput(BaseModel):
agent_to_handoff_to: Literal["triage_agent", "stranger_agent", ...]
other_data: Any
@function_tool
def perform_handoff(to: HandoffInput):
return "ok"
triage_agent = Agent(
name="Triage",
tools=[perform_handoff],
tool_use_behavior={"stop_at_tool_names": ["perform_handoff"]},
)
result = await Runner.run(orchestrator_agent, ...)
if isinstance(result.final_output, HandoffInput):
next_agent = get_agent_from_name(result.final_output.agent_to_handoff_to)
if should_handoff_custom_logic(next_agent):
# Do auth
await Runner.run(next_agent, ...) i.e.
|
Thanks @rm-openai . I'll try both of them. |
@rm-openai is there a proper example of the |
@HG2407 the openai-agents-python/src/agents/handoffs.py Line 233 in 1847008
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please read this first
Question
Describe your question. Provide details if available.
So basically, suppose I have 4 agents, one triage and two child agents and one stranger agent. Usually the conversation will start from triage agent. But sometimes it can start from the stranger agent too, based on some backend logic.
Now my question is, triage agent doesn't have any context about the stranger agent. The stranger agent knows about the triage agent (through the handoff tool). But once a request is handed off, the stranger agent can't get the control back (since triage agent doesn't have handoff tool for stranger agent).
so TL;DR would be: Stranger can handoff to triage (only once). Triage can't handoff to stranger.
Now I want to implement a blocking logic here in this handoff from stranger to triage, which blocks the handoff if certain conditions are not matched (for more deterministic flow and to prevent handoff due to hallucinations).
One thing to note is, this stranger agent is completely independent and has all the tools required. When it performs it's tasks, it needs to handoff to the triage for continuing the conversation.
My use case is similar to the customer service example in the documentation but a little more complex.
I want something similar to
on_handoff
parameter, but it should be able to control whether to allow the handoff to happen or not.This would be preferred. But if not possible then, I have an idea:
Run the stranger agent as a completely separate agent and provide it a tool to end the conversation, add the logic to control whether ending the conversation is allowed or not here. If not allowed, the stranger agent will continue conversing else, I'll take the context / chat history and give it to the separate triage agent to continue the conversation from where the stranger left ?
Edit: On searching further I found:
on_invoke_handoff
exists. Can I use this function in any way, what will happen if I return the stranger agent's name and append an additional message in the context instructing the agent what to do: "Tell the user this action is not allowed before you complete this" or something ?The text was updated successfully, but these errors were encountered: