-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fix ContextWindowExceededError after 3 retries in react loop #9110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2cc00aa
dc4b402
ae1b52a
2dd54fc
fa8a09b
e95c2dd
18e999f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,6 +204,68 @@ def mock_react(**kwargs): | |
| assert result.output_text == "Final output" | ||
|
|
||
|
|
||
| def test_context_window_exceeded_after_retries(): | ||
|
||
| """Test that context window errors are handled gracefully after exhausting retry attempts. | ||
|
||
|
|
||
| This tests the fix for the bug where returning None after exhausting retries | ||
| caused "'NoneType' object has no attribute 'next_thought'" error. | ||
|
|
||
| The fix raises a ValueError instead, which is caught in forward() and causes | ||
| the loop to break gracefully. | ||
| """ | ||
|
|
||
| def echo(text: str) -> str: | ||
| return f"Echoed: {text}" | ||
|
|
||
| react = dspy.ReAct("input_text -> output_text", tools=[echo]) | ||
|
|
||
| # Always raise context window exceeded - simulating case where prompt is too large | ||
| # even on the very first call with empty trajectory | ||
| def mock_react(**kwargs): | ||
| raise litellm.ContextWindowExceededError("Context window exceeded", "dummy_model", "dummy_provider") | ||
|
|
||
| react.react = mock_react | ||
| react.extract = lambda **kwargs: dspy.Prediction(output_text="Fallback output") | ||
|
||
|
|
||
| # Call forward - should handle the error gracefully by logging and breaking the loop | ||
| # This should NOT raise AttributeError: 'NoneType' object has no attribute 'next_thought' | ||
| result = react(input_text="test input") | ||
|
|
||
| # The trajectory should be empty since the first call failed | ||
| assert result.trajectory == {} | ||
| # Extract should still be called and produce output | ||
| assert result.output_text == "Fallback output" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_context_window_exceeded_after_retries(): | ||
| """Test that context window errors are handled gracefully after exhausting retry attempts in async mode.""" | ||
|
|
||
| async def echo(text: str) -> str: | ||
| return f"Echoed: {text}" | ||
|
|
||
| react = dspy.ReAct("input_text -> output_text", tools=[echo]) | ||
|
|
||
| # Always raise context window exceeded | ||
| async def mock_react(**kwargs): | ||
| raise litellm.ContextWindowExceededError("Context window exceeded", "dummy_model", "dummy_provider") | ||
|
|
||
| async def mock_extract(**kwargs): | ||
| return dspy.Prediction(output_text="Fallback output") | ||
|
|
||
| react.react.acall = mock_react | ||
| react.extract.acall = mock_extract | ||
|
|
||
| # Call forward - should handle the error gracefully | ||
| # This should NOT raise AttributeError: 'NoneType' object has no attribute 'next_thought' | ||
| result = await react.acall(input_text="test input") | ||
|
|
||
| # The trajectory should be empty since the first call failed | ||
| assert result.trajectory == {} | ||
| # Extract should still be called and produce output | ||
| assert result.output_text == "Fallback output" | ||
|
|
||
|
|
||
| def test_error_retry(): | ||
| # --- a tiny tool that always fails ------------------------------------- | ||
| def foo(a, b): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot We don't need this try-catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the try-catch blocks in both sync and async methods. The
ValueErrorfromtruncate_trajectorynow naturally propagates up and is caught byforward(). Fixed in 2dd54fc.