@@ -658,6 +658,14 @@ def _execute_code(
658658 except (CodeExecutionError , SyntaxError ) as e :
659659 return f"[Error] { format_error_for_lm (e )} "
660660
661+ def _action_signature (self , repl : CodeInterpreter ) -> Any :
662+ instructions = getattr (repl , "execution_instructions" , "" )
663+ if not instructions or type (self .generate_action ) is not dspy .Predict :
664+ return None
665+ return self .generate_action .signature .append_instructions (
666+ "Code interpreter execution environment:\n " + instructions
667+ )
668+
661669 def _execute_iteration (
662670 self ,
663671 repl : CodeInterpreter ,
@@ -666,13 +674,15 @@ def _execute_iteration(
666674 iteration : int ,
667675 input_args : dict [str , Any ],
668676 output_field_names : list [str ],
677+ action_signature : Any = None ,
669678 ) -> Prediction | REPLHistory :
670679 """Execute one iteration. Returns Prediction if done, else updated REPLHistory."""
671680 variables_info = [variable .format () for variable in variables ]
672681 action = self .generate_action (
673682 variables_info = variables_info ,
674683 repl_history = history ,
675684 iteration = f"{ iteration + 1 } /{ self .max_iters } " ,
685+ ** ({"signature" : action_signature } if action_signature else {}),
676686 )
677687 if self .verbose :
678688 logger .info (
@@ -716,12 +726,13 @@ def forward(self, interpreter: CodeInterpreter | None = None, /, **input_args) -
716726 variables = self ._build_variables (** input_args )
717727
718728 with self ._interpreter_context (execution_tools , interpreter ) as repl :
729+ action_signature = self ._action_signature (repl )
719730 regular_args = self ._prepare_serializable_vars (input_args , repl )
720731 history : REPLHistory = REPLHistory (max_output_chars = self .max_output_chars )
721732
722733 for iteration in range (self .max_iters ):
723734 result : Prediction | REPLHistory = self ._execute_iteration (
724- repl , variables , history , iteration , regular_args , output_field_names
735+ repl , variables , history , iteration , regular_args , output_field_names , action_signature
725736 )
726737 if isinstance (result , Prediction ):
727738 return result
@@ -759,13 +770,15 @@ async def _aexecute_iteration(
759770 iteration : int ,
760771 input_args : dict [str , Any ],
761772 output_field_names : list [str ],
773+ action_signature : Any = None ,
762774 ) -> Prediction | REPLHistory :
763775 """Async version: Execute one iteration."""
764776 variables_info = [variable .format () for variable in variables ]
765777 pred = await self .generate_action .acall (
766778 variables_info = variables_info ,
767779 repl_history = history ,
768780 iteration = f"{ iteration + 1 } /{ self .max_iters } " ,
781+ ** ({"signature" : action_signature } if action_signature else {}),
769782 )
770783 if self .verbose :
771784 logger .info (
@@ -805,12 +818,13 @@ async def aforward(self, interpreter: CodeInterpreter | None = None, /, **input_
805818 variables = self ._build_variables (** input_args )
806819
807820 with self ._interpreter_context (execution_tools , interpreter ) as repl :
821+ action_signature = self ._action_signature (repl )
808822 regular_args = self ._prepare_serializable_vars (input_args , repl )
809823 history = REPLHistory (max_output_chars = self .max_output_chars )
810824
811825 for iteration in range (self .max_iters ):
812826 result = await self ._aexecute_iteration (
813- repl , variables , history , iteration , regular_args , output_field_names
827+ repl , variables , history , iteration , regular_args , output_field_names , action_signature
814828 )
815829 if isinstance (result , Prediction ):
816830 return result
0 commit comments