1- from dataclasses import dataclass
2-
31from pydantic import BaseModel
42
53import dspy
6- from dspy .predict import react
7- from dspy .utils .dummies import DummyLM , dummy_rm
4+ from dspy .utils .dummies import DummyLM
85import litellm
96
107# def test_example_no_tools():
@@ -276,3 +273,38 @@ def mock_react(**kwargs):
276273 assert "thought_0" not in result .trajectory
277274 assert "thought_2" in result .trajectory
278275 assert result .output_text == "Final output"
276+
277+
278+ def test_error_retry ():
279+ def foo (a , b ):
280+ raise Exception ("tool error" )
281+
282+ react = dspy .ReAct ("a, b -> c:int" , tools = [foo ])
283+ max_iters = 2
284+ lm = DummyLM (
285+ [
286+ {"next_thought" : "I need to add two numbers." , "next_tool_name" : "foo" , "next_tool_args" : {"a" : 1 , "b" : 2 }},
287+ {"next_thought" : "I need to add two numbers." , "next_tool_name" : "foo" , "next_tool_args" : {"a" : 1 , "b" : 2 }},
288+ {"reasoning" : "I added the numbers successfully" , "c" : 3 },
289+ ]
290+ )
291+ dspy .settings .configure (lm = lm )
292+
293+ outputs = react (a = 1 , b = 2 , max_iters = max_iters )
294+ expected_trajectory = {
295+ "thought_0" : "I need to add two numbers." ,
296+ "tool_name_0" : "foo" ,
297+ "tool_args_0" : {
298+ "a" : 1 ,
299+ "b" : 2 ,
300+ },
301+ 'observation_0' : 'Failed to execute: tool error' ,
302+ 'thought_1' : 'I need to add two numbers.' ,
303+ 'tool_name_1' : 'foo' ,
304+ "tool_args_1" : {
305+ "a" : 1 ,
306+ "b" : 2 ,
307+ },
308+ 'observation_1' : 'Failed to execute: tool error' ,
309+ }
310+ assert outputs .trajectory == expected_trajectory
0 commit comments