-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add run configuration inheritance for agent-as-tool workflows #1006
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
base: main
Are you sure you want to change the base?
Conversation
I am not sure what @rh-openai thinks but here are my initial thoughts on this:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah using the trace to store this data isn't right. What you could do instead, is use a ContextVar. So:
- Add a
pass_run_config_to_sub_agents: bool
param to the RunConfig, just in case people want to enable/disable this behavior - Set the run_config context var from run.py
- Read the run_config in the agent as tool
- Also ensure that you add tests to make sure the contextvar is unset in all cases (errors, success, cancel etc)
I'm also not entirely sure if it makes sense to have this as part of the SDK, or just something you do. The approach I've taken to as_tool is that if you want customization, you can just create your own tool via:
@function_tool
def run_my_agent(input):
my_agent = Agent(...)
result = Runner.run(my_agent)
return result.foo
Why This Feature is NeededThe
This implementation addresses the OpenAI Agents SDK team's feedback to use ContextVar instead of storing configuration in traces, providing a clean, opt-in solution that maintains backward compatibility while enabling consistent global configuration across agent hierarchies. TestingComprehensive test coverage includes:
This feature addresses a fundamental consistency issue in the SDK where I have tried my best and changes the code according to the recommended way. If it still needs modification, tell me! I will try my best :) |
src/agents/tracing/scope.py
Outdated
@@ -16,6 +17,9 @@ | |||
"current_trace", default=None | |||
) | |||
|
|||
_current_run_config: contextvars.ContextVar["RunConfig | None"] = contextvars.ContextVar( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be easy to add here since ContextVar is already imported in the source file. However, I don't think this is the best place to add it because the run config isn't specifically used for tracing. It's okay to add a new file, but personally, I think adding it to run.py
should be fine in this case. Simply having get|set_current_run_config
methods without Scope class should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the files :)
Problem:
When using the "agents as tools" pattern, tool-agents create their own execution context but don't inherit the parent's run_config.
This creates two problems:
Tool-agents can't access the parent's global model configuration
If tool-agents don't have their own model field set and the parent's run_config.model is unavailable, model resolution fails entirely, causing the orchestrator to fail when calling the tool
Solution:
Modified the agent-as-tool execution to properly handle run_config inheritance while ensuring robust model resolution. This prevents execution failures when tool-agents lack explicit model configuration.
Changes
1. traces.py
_run_config
attribute to bothNoOpTrace
andTraceImpl
classesTYPE_CHECKING
import forRunConfig
type annotation2. run.py
_run_config
attribute (fallback)3. agent.py
get_current_trace
import from tracing moduleas_tool()
method to retrieve and inherit run configuration from trace contextRunner.run()
for consistent behavior