1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
+ import contextvars
4
5
import copy
5
6
import inspect
6
7
from dataclasses import dataclass , field
51
52
from .stream_events import AgentUpdatedStreamEvent , RawResponsesStreamEvent
52
53
from .tool import Tool
53
54
from .tracing import Span , SpanError , agent_span , get_current_trace , trace
54
- from .tracing .scope import Scope
55
55
from .tracing .span_data import AgentSpanData
56
56
from .usage import Usage
57
57
from .util import _coro , _error_tracing
58
58
59
+ _current_run_config : contextvars .ContextVar [RunConfig | None ] = contextvars .ContextVar (
60
+ "current_run_config" , default = None
61
+ )
62
+
59
63
DEFAULT_MAX_TURNS = 10
60
64
61
65
DEFAULT_AGENT_RUNNER : AgentRunner = None # type: ignore
@@ -80,6 +84,21 @@ def get_default_agent_runner() -> AgentRunner:
80
84
return DEFAULT_AGENT_RUNNER
81
85
82
86
87
+ def get_current_run_config () -> RunConfig | None :
88
+ """Get the current run config from context."""
89
+ return _current_run_config .get ()
90
+
91
+
92
+ def set_current_run_config (run_config : RunConfig | None ) -> contextvars .Token [RunConfig | None ]:
93
+ """Set the current run config in context."""
94
+ return _current_run_config .set (run_config )
95
+
96
+
97
+ def reset_current_run_config (token : contextvars .Token [RunConfig | None ]) -> None :
98
+ """Reset the current run config in context."""
99
+ _current_run_config .reset (token )
100
+
101
+
83
102
@dataclass
84
103
class RunConfig :
85
104
"""Configures settings for the entire agent run."""
@@ -341,7 +360,7 @@ async def run(
341
360
# Set the run_config context variable if enabled
342
361
run_config_token = None
343
362
if run_config .pass_run_config_to_sub_agents :
344
- run_config_token = Scope . set_current_run_config (run_config )
363
+ run_config_token = set_current_run_config (run_config )
345
364
346
365
try :
347
366
with TraceCtxManager (
@@ -495,7 +514,7 @@ async def run(
495
514
finally :
496
515
# Always clean up the context variable
497
516
if run_config_token is not None :
498
- Scope . reset_current_run_config (run_config_token )
517
+ reset_current_run_config (run_config_token )
499
518
500
519
def run_sync (
501
520
self ,
@@ -539,7 +558,7 @@ def run_streamed(
539
558
# Set the run_config context variable if enabled
540
559
run_config_token = None
541
560
if run_config .pass_run_config_to_sub_agents :
542
- run_config_token = Scope . set_current_run_config (run_config )
561
+ run_config_token = set_current_run_config (run_config )
543
562
544
563
try :
545
564
# If there's already a trace, we don't create a new one. In addition, we can't end the
@@ -596,7 +615,7 @@ def run_streamed(
596
615
finally :
597
616
# Always reset the context variable
598
617
if run_config_token is not None :
599
- Scope . reset_current_run_config (run_config_token )
618
+ reset_current_run_config (run_config_token )
600
619
601
620
@classmethod
602
621
async def _run_input_guardrails_with_queue (
0 commit comments