@@ -187,8 +187,57 @@ def mock_execute_generator(action_step):
187187 assert isinstance (result [2 ], MagicMock ) # Final answer step with "<user_break>"
188188
189189
190- def test_run_with_agent_error (core_agent_instance ):
191- """Test _run method when AgentError occurs."""
190+ def test_run_with_final_answer_error (core_agent_instance ):
191+ """Test _run method when FinalAnswerError occurs in _step_stream."""
192+ # Setup
193+ task = "test task"
194+ max_steps = 3
195+
196+ # Mock _execute_step to raise FinalAnswerError
197+ with patch .object (core_agent_instance , '_execute_step' ,
198+ side_effect = core_agent_module .FinalAnswerError ()) as mock_execute_step , \
199+ patch .object (core_agent_instance , '_finalize_step' ):
200+ # Execute
201+ result = list (core_agent_instance ._run_stream (task , max_steps ))
202+
203+ # Assertions
204+ # When FinalAnswerError occurs, it should yield action step + final answer step
205+ assert len (result ) == 2
206+ assert isinstance (result [0 ], MagicMock ) # Action step
207+ assert isinstance (result [1 ], MagicMock ) # Final answer step
208+
209+
210+ def test_run_with_final_answer_error_and_model_output (core_agent_instance ):
211+ """Test _run method when FinalAnswerError occurs with model_output conversion."""
212+ # Setup
213+ task = "test task"
214+ max_steps = 3
215+
216+ # Create a mock action step with model_output
217+ mock_action_step = MagicMock ()
218+ mock_action_step .model_output = "```code:python\n print('hello')\n ```"
219+
220+ # Mock _execute_step to set model_output and then raise FinalAnswerError
221+ def mock_execute_step (action_step ):
222+ action_step .model_output = "```code:python\n print('hello')\n ```"
223+ raise core_agent_module .FinalAnswerError ()
224+
225+ with patch .object (core_agent_instance , '_execute_step' , side_effect = mock_execute_step ), \
226+ patch .object (core_agent_module , 'convert_code_format' , return_value = "```python\n print('hello')\n ```" ) as mock_convert , \
227+ patch .object (core_agent_instance , '_finalize_step' ):
228+ # Execute
229+ result = list (core_agent_instance ._run_stream (task , max_steps ))
230+
231+ # Assertions
232+ assert len (result ) == 2
233+ assert isinstance (result [0 ], MagicMock ) # Action step
234+ assert isinstance (result [1 ], MagicMock ) # Final answer step
235+ # Verify convert_code_format was called
236+ mock_convert .assert_called_once_with ("```code:python\n print('hello')\n ```" )
237+
238+
239+ def test_run_with_agent_error_updated (core_agent_instance ):
240+ """Test _run method when AgentError occurs (updated to handle FinalAnswerError separately)."""
192241 # Setup
193242 task = "test task"
194243 max_steps = 3
@@ -209,32 +258,27 @@ def test_run_with_agent_error(core_agent_instance):
209258 assert isinstance (result [- 1 ], MagicMock ) # Final answer step
210259
211260
212- def test_run_with_agent_parse_error_branch (core_agent_instance ):
213- """Ensure branch that converts model_output via convert_code_format is covered."""
214-
261+ def test_run_with_agent_parse_error_branch_updated (core_agent_instance ):
262+ """Test the branch that handles FinalAnswerError with model_output conversion."""
215263 task = "parse task"
216264 max_steps = 1
217265
218- parse_hint = "Make sure to include code with the correct pattern, for instance"
219-
220- # Create a mock action step that will be used in the error handling
221- mock_action_step = MagicMock ()
222- mock_action_step .model_output = "unformatted answer"
266+ # Mock _execute_step to set model_output and then raise FinalAnswerError
267+ def mock_execute_step (action_step ):
268+ action_step .model_output = "```code:python\n print('hello')\n ```"
269+ raise core_agent_module .FinalAnswerError ()
223270
224- # Mock ActionStep constructor to return our mock
225- with patch .object (mock_smolagents .memory , 'ActionStep' , return_value = mock_action_step ), \
226- patch .object (core_agent_instance , '_execute_step' , side_effect = MockAgentError (f"{ parse_hint } - error" )), \
227- patch .object (core_agent_module , 'convert_code_format' , return_value = "formatted answer" ) as mock_convert , \
271+ with patch .object (core_agent_instance , '_execute_step' , side_effect = mock_execute_step ), \
272+ patch .object (core_agent_module , 'convert_code_format' , return_value = "```python\n print('hello')\n ```" ) as mock_convert , \
228273 patch .object (core_agent_instance , '_finalize_step' ):
229274 results = list (core_agent_instance ._run_stream (task , max_steps ))
230275
231- # _run 应该产出 action step + 处理后的结果
232- assert len (results ) > = 2
276+ # _run should yield action step + final answer step
277+ assert len (results ) = = 2
233278 assert isinstance (results [0 ], MagicMock ) # Action step
234- assert isinstance (results [- 1 ], MagicMock ) # Final answer step
235-
236-
237-
279+ assert isinstance (results [1 ], MagicMock ) # Final answer step
280+ # Verify convert_code_format was called
281+ mock_convert .assert_called_once_with ("```code:python\n print('hello')\n ```" )
238282
239283
240284def test_convert_code_format_replacements ():
0 commit comments