fix(models): add ContextWindowOverflowException to Ollama, Mistral, LlamaAPI, Writer providers#2056
Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…lamaAPI, Writer providers All four providers now detect when context window is exceeded and raise ContextWindowOverflowException, allowing the event loop to trigger automatic context reduction via ConversationManager.reduce_context(). Previously: - Ollama: No exception handling at all - Mistral: Only caught rate limits - LlamaAPI: Only caught RateLimitError - Writer: Only caught RateLimitError Now all providers: - Define OVERFLOW_MESSAGES constant with common context-related error strings - Catch provider-specific/general exceptions in stream() method - Check error message against OVERFLOW_MESSAGES - Raise ContextWindowOverflowException on match for automatic recovery Fixes strands-agents#2052
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds exception handling for context window overflow across four model providers: Ollama, Mistral, LlamaAPI, and Writer. These providers currently lack the detection, so agents crash with unhandled errors when the context window fills up instead of triggering automatic recovery.
When context window is exceeded, the SDK's event loop catches
ContextWindowOverflowExceptionand callsConversationManager.reduce_context()to trim the conversation history. Without this exception, the error propagates as a provider-specific error.Changes
For each provider, this fix:
ContextWindowOverflowExceptionOVERFLOW_MESSAGESconstant with context-related error stringsContextWindowOverflowExceptionon matchOllama had no error handling at all. Mistral, LlamaAPI, and Writer already catch rate limits, so the new overflow check is added to those handlers.
All four providers now follow the pattern from Anthropic, OpenAI, and Bedrock.
Related Issues
Fixes #2052
Documentation PR
N/A
Type of Change
Bug fix
Testing
reduce_context()instead of crashingChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.