-
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 5 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,57 @@ def mock_react(**kwargs): | |
| assert result.output_text == "Final output" | ||
|
|
||
|
|
||
| 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]) | ||
|
|
||
| # 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(): | ||
| 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): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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 can we combine two tests by parameterizing?
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.
Combined the sync and async tests into a single test function. Fixed in e95c2dd.