Skip to content

fix(models): add ContextWindowOverflowException to Ollama, Mistral, LlamaAPI, Writer providers#2056

Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/context-window-overflow-providers
Open

fix(models): add ContextWindowOverflowException to Ollama, Mistral, LlamaAPI, Writer providers#2056
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Zelys-DFKH:fix/context-window-overflow-providers

Conversation

@Zelys-DFKH
Copy link
Copy Markdown

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 ContextWindowOverflowException and calls ConversationManager.reduce_context() to trim the conversation history. Without this exception, the error propagates as a provider-specific error.

Changes

For each provider, this fix:

  • Imports ContextWindowOverflowException
  • Adds OVERFLOW_MESSAGES constant with context-related error strings
  • Wraps the stream call in try/except to detect overflow
  • Raises ContextWindowOverflowException on match

Ollama 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

  • Python syntax validated on all 4 files
  • Exception handling follows the existing pattern in Anthropic/OpenAI/Bedrock
  • Manual testing: long conversations with these providers should now call reduce_context() instead of crashing

Checklist

  • I have read the CONTRIBUTING document
  • My changes follow the coding style and patterns of this project
  • Exception handling matches the working provider pattern

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Ollama, Mistral, LlamaAPI, and Writer providers missing ContextWindowOverflowException

1 participant