@@ -359,6 +359,14 @@ def __init__(
359
359
self ._entered_count = 0
360
360
self ._exit_stack = None
361
361
362
+ def _get_instructions_literal_and_functions (
363
+ self ,
364
+ ) -> tuple [str | None , list [_system_prompt .SystemPromptRunner [AgentDepsT ]]]:
365
+ instructions , instructions_functions = self ._instructions , self ._instructions_functions
366
+ if override_instructions := self ._override_instructions .get ():
367
+ instructions , instructions_functions = self ._instructions_literal_and_functions (override_instructions .value )
368
+ return instructions , instructions_functions
369
+
362
370
def _instructions_literal_and_functions (
363
371
self ,
364
372
instructions : InstructionsInput ,
@@ -597,10 +605,7 @@ async def main():
597
605
usage_limits = usage_limits or _usage .UsageLimits ()
598
606
599
607
async def get_instructions (run_context : RunContext [AgentDepsT ]) -> str | None :
600
- literal , functions = self ._instructions , self ._instructions_functions
601
- if override := self ._override_instructions .get ():
602
- literal , functions = self ._instructions_literal_and_functions (override .value )
603
-
608
+ literal , functions = self ._get_instructions_literal_and_functions ()
604
609
parts = [
605
610
literal ,
606
611
* [await func .run (run_context ) for func in functions ],
@@ -642,12 +647,7 @@ async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
642
647
instrumentation_settings = instrumentation_settings ,
643
648
)
644
649
645
- instructions_for_node , instructions_functions_for_node = self ._instructions , self ._instructions_functions
646
- if override_instructions := self ._override_instructions .get ():
647
- instructions_for_node , instructions_functions_for_node = self ._instructions_literal_and_functions (
648
- override_instructions .value
649
- )
650
-
650
+ instructions_for_node , instructions_functions_for_node = self ._get_instructions_literal_and_functions ()
651
651
start_node = _agent_graph .UserPromptNode [AgentDepsT ](
652
652
user_prompt = user_prompt ,
653
653
deferred_tool_results = deferred_tool_results ,
@@ -771,9 +771,9 @@ def override(
771
771
tools_token = None
772
772
773
773
if _utils .is_set (instructions ):
774
- ins_token = self ._override_instructions .set (_utils .Some (instructions ))
774
+ instructions_token = self ._override_instructions .set (_utils .Some (instructions ))
775
775
else :
776
- ins_token = None
776
+ instructions_token = None
777
777
778
778
try :
779
779
yield
@@ -786,8 +786,8 @@ def override(
786
786
self ._override_toolsets .reset (toolsets_token )
787
787
if tools_token is not None :
788
788
self ._override_tools .reset (tools_token )
789
- if ins_token is not None :
790
- self ._override_instructions .reset (ins_token )
789
+ if instructions_token is not None :
790
+ self ._override_instructions .reset (instructions_token )
791
791
792
792
@overload
793
793
def instructions (
0 commit comments