Skip to content

Conversation

Yankzy
Copy link

@Yankzy Yankzy commented Sep 12, 2025

This change introduces a new optional bound_context parameter to Agent.as_tool so callers can pass a Pydantic BaseModel instance that will be used as the run context for that tool invocation. When provided, the bound context overrides the parent agent's context for the duration of the tool call; when omitted, existing behavior (inheriting the caller's context) is preserved.

Why

  • Enables safe, explicit context binding for sub-agent tools in orchestrations (e.g., orchestrator → multiple specialized agents).
  • Prevents context leakage between parallel or sequential sub-agent invocations and makes it easy to pass typed metadata (email, agent_name, profile data) to sub-agents.
  • Solves real-world need in the trading orchestrator example where each agent must receive its own TradeAgentContext (email/name) to correctly save trade signals.

What changed

  • src/agents/agent.py
    • Added parameter: bound_context: BaseModel | None = None to Agent.as_tool(...).
    • run_agent now calls Runner.run(..., context=bound_context or context.context) so the provided bound_context is used when present; otherwise the original behavior is retained.
    • Docstring for bound_context describing its behavior and that it should be a Pydantic BaseModel.

This is a backward-compatible change: default is None, so existing uses of as_tool are unaffected.

Example usage

async def generate_trade_signal_for_ticker(ticker: str) -> TradeSignal:
    """
    Runs the sentimental strategist agent for a given stock ticker and saves the result.
    """
    
    try:
        agent_adapter = await AgentAdapter.get_instance()
        agent_tools = []
        for agent in agent_adapter.get_all_agents:
            # Create the context for this specific agent
            sub_agent_context = TradeAgentContext(
                agent_email=agent.email,
                agent_name=agent.name,
            )

            agent_tool = agent.as_tool(
                tool_name=sanitize_tool_name(agent.name),
                tool_description=f"Run {agent.name} to generate a trade signal for {ticker}.",
                bound_context=sub_agent_context, 
            )
            agent_tools.append(agent_tool)

        judge_agent = Agent(
            name="Trading Signal Orchestrator Agent",
            instructions="""
            - You are the main agent in a Collaborative Agentic workflow to generate trading Signals.  """,
            tools=[
                # other tools,
                *agent_tools,
            ],
            output_type=JudgeAgentOutput,
            hooks=Hooks(display_name="trading_signal_orchestrator_agent"),
            model='gpt-4.1',
        )

@seratch seratch added the duplicate This issue or pull request already exists label Sep 12, 2025
@seratch
Copy link
Member

seratch commented Sep 12, 2025

Hi @Yankzy, we really appreciate your interest and effort here. However, as I mentioned at #1719 (comment), we’ll be looking at the broader picture of this type of enhancement along with other use case considerations soon. In the meantime, please hold off on resubmitting the same patch until we agree on moving forward with this approach. Thanks so much for your understanding!

@seratch seratch closed this Sep 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants