Skip to content

How can I pass dynamic instruction to the agent #482

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
Salaudev opened this issue Apr 11, 2025 · 9 comments
Closed

How can I pass dynamic instruction to the agent #482

Salaudev opened this issue Apr 11, 2025 · 9 comments
Labels
question Question about using the SDK stale

Comments

@Salaudev
Copy link

I want to implement a handoff agent, but in this agent I have dynamic data in the system prompt.

Image

i was thinking to do smth like that

def run_rate_negotiator(ctx: RunContextWrapper[Any]):
    conversation = ctx.context["conversation_history"]
    query = ctx.context["text_query"]
    conversation.append({"role": "user", "content": query})

    rate_negotiator = Agent(
        name="rate_negotiator",
        instructions=get_rate_negotiator_instructions(ctx.context["company_info"], ctx.context["load_context"]),
        model=OpenAIChatCompletionsModel(model=openai_deployment_name, openai_client=azure_client),
        tools=[...]
    )

    try:
        result = Runner.run(rate_negotiator, input=conversation, context=ctx.context)
        return result
    except Exception as e:
        print(f"Error in run_rate_negotiator: {str(e)}")
        error_json = '{"error": "Failed to process rate negotiation: ' + str(e) + '"}'
        return error_json

To pass handoff agent to a main agent, I need the following
Image

Can anyone help me with this?

@Salaudev Salaudev added the question Question about using the SDK label Apr 11, 2025
@DanieleMorotti
Copy link
Contributor

Hi, there is a section in the documentation that explains how to use dynamic data in the prompt.

@Salaudev
Copy link
Author

Hi, there is a section in the documentation that explains how to use dynamic data in the prompt.

@DanieleMorotti Hi! Thanks.
Also, I have several questions about handoff logic

How can I implement logic that will stop the main agent after it is handoff to another agent?

@DanieleMorotti
Copy link
Contributor

I'm not entirely sure what you mean by "stop the main agent."

When using handoffs, the main agent delegates the task to another agent, which then continues the conversation. Could you please provide more details if this doesn't address your question?

@Salaudev
Copy link
Author

@DanieleMorotti

okay, I have orchestrator agent that has multiple tools and rate_negotiator agent (handoff)
orchestrator agent should handoff to rate_negotiator
and should be executed rate_negotiator's tools

After handing off orchestrator agent should stop running
now it is waiting for response from rate_negotiator

@DanieleMorotti
Copy link
Contributor

If you pass the rate_negotiator as handoff to the orchestrator, when the handoff occurs, then the rate_negotiatior leads the conversation. This is a very simple example:

import asyncio
import traceback

from agents import Agent, Runner, handoff
from agents.extensions import handoff_filters
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
from agents.model_settings import ModelSettings
from openai.types.responses import ResponseTextDeltaEvent


async def main():
    input_items = []

    # Agents
    english_agent = Agent(
        name="english interpreter",
        model="gpt-4o-mini",
        instructions="You are in charge of speaking with an english user.",
        model_settings=ModelSettings(temperature=0.4, max_tokens=2048)
    )
    italian_agent = Agent(
        name="italian interpreter",
        model="gpt-4o-mini",
        instructions="Tu sei incaricato di parlare con gli utenti italiani.",
        model_settings=ModelSettings(temperature=0.4, max_tokens=2048),
    )
    mn_agent = Agent(
        name="manager agent",
        model="gpt-4o-mini",
        instructions=prompt_with_handoff_instructions("Your task is to pass the control of the conversation to the english or italian agents based on the user's nationality. You can't directly respond to the user."),
        model_settings=ModelSettings(temperature=0.3, max_tokens=2048),
        handoffs=[
            handoff(english_agent, input_filter=handoff_filters.remove_all_tools),
            handoff(italian_agent, input_filter=handoff_filters.remove_all_tools)
        ]
    )
    
    current_agent = mn_agent
    while True:
        user_input = input("> User: ")

        if user_input == "quit":
            print("\n> Stopping Agent ...")
            break
        
        input_items.append({"content": user_input, "role": "user"})
        result = Runner.run_streamed(
            current_agent,
            input=input_items
            #run_config=RunConfig(tracing_disabled=True)
        )

        print(f"> Agent: ")
        async for event in result.stream_events():
            if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
                print(event.data.delta, end="", flush=True)

        print("\n")
        input_items = result.to_input_list()
        current_agent = result.last_agent

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except Exception as exc:
        print(f"Error while agent execution: {traceback.format_exc()}")

In this example, when control is transferred to one of the two agents, that agent leads the conversation. If you need the capability to hand off from secondary agents to the main one, you should include the main agent as an handoff option for the other two.

Hope this is helpful.

@Salaudev
Copy link
Author

handoff(english_agent, input_filter=handoff_filters.remove_all_tools),
this is for removing tools of the main agent ?

@DanieleMorotti
Copy link
Contributor

It removes all tool calls from the history. You can also remove the input filter if you want, it's optional

Copy link

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

@github-actions github-actions bot added the stale label Apr 22, 2025
Copy link

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 Apr 25, 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

2 participants