Look at this with fresh eye: bound_context for deep sub-agents #1725
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change introduces a new optional
bound_context
parameter toAgent.as_tool
so callers can pass a PydanticBaseModel
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
TradeAgentContext
(email/name) to correctly save trade signals.What changed
src/agents/agent.py
bound_context: BaseModel | None = None
toAgent.as_tool(...)
.run_agent
now callsRunner.run(..., context=bound_context or context.context)
so the providedbound_context
is used when present; otherwise the original behavior is retained.bound_context
describing its behavior and that it should be a PydanticBaseModel
.This is a backward-compatible change: default is
None
, so existing uses ofas_tool
are unaffected.Example usage