DSPy: The framework for programming—not prompting—language models
Python
Fix ContextWindowExceededError after 3 retries in react loop (#9110)
Copilotb95e1017ed67
diff --git a/dspy/predict/react.py b/dspy/predict/react.pyindex 5f87879f8..e94b591ce 100644--- a/dspy/predict/react.py+++ b/dspy/predict/react.py@@ -153,6 +153,9 @@ class ReAct(Module):except ContextWindowExceededError:logger.warning("Trajectory exceeded the context window, truncating the oldest tool call information.")trajectory = self.truncate_trajectory(trajectory)+ raise ValueError(+ "The context window was exceeded even after 3 attempts to truncate the trajectory."+ )async def _async_call_with_potential_trajectory_truncation(self, module, trajectory, **input_args):for _ in range(3):@@ -164,6 +167,9 @@ class ReAct(Module):except ContextWindowExceededError:logger.warning("Trajectory exceeded the context window, truncating the oldest tool call information.")trajectory = self.truncate_trajectory(trajectory)+ raise ValueError(+ "The context window was exceeded even after 3 attempts to truncate the trajectory."+ )def truncate_trajectory(self, trajectory):"""Truncates the trajectory so that it fits in the context window.diff --git a/tests/predict/test_react.py b/tests/predict/test_react.pyindex 09fd1c7c8..bd65515fb 100644--- a/tests/predict/test_react.py+++ b/tests/predict/test_react.py@@ -204,6 +204,54 @@ def test_trajectory_truncation():assert result.output_text == "Final output"+async def test_context_window_exceeded_after_retries():+ def echo(text: str) -> str:+ return f"Echoed: {text}"++ react = dspy.ReAct("input_text -> output_text", tools=[echo])++ def mock_react(**kwargs):+ raise litellm.ContextWindowExceededError("Context window exceeded", "dummy_model", "dummy_provider")++ # Test sync version+ extract_calls = []++ def mock_extract(**kwargs):+ extract_calls.append(kwargs)+ return dspy.Prediction(output_text="Fallback output")++ react.react = mock_react+ react.extract = mock_extract++ result = react(input_text="test input")+ assert result.trajectory == {}+ assert result.output_text == "Fallback output"+ assert len(extract_calls) == 1+ assert extract_calls[0]["input_text"] == "test input"+ assert "trajectory" in extract_calls[0]++ # Test async version+ async_extract_calls = []++ async def mock_react_async(**kwargs):+ raise litellm.ContextWindowExceededError("Context window exceeded", "dummy_model", "dummy_provider")++ async def mock_extract_async(**kwargs):+ async_extract_calls.append(kwargs)+ return dspy.Prediction(output_text="Fallback output")++ react.react.acall = mock_react_async+ react.extract.acall = mock_extract_async++ result = await react.acall(input_text="test input")+ assert result.trajectory == {}+ assert result.output_text == "Fallback output"+ assert len(async_extract_calls) == 1+ assert async_extract_calls[0]["input_text"] == "test input"+ assert "trajectory" in async_extract_calls[0]++def test_error_retry():# --- a tiny tool that always fails -------------------------------------def foo(a, b):