Skip to content

Conversation

@dinindunz
Copy link

Description

Fixes context overflow detection for OpenAI-compatible endpoints that wrap Bedrock models (e.g., Databricks Model Serving).

Problem:
The OpenAI provider only caught BadRequestError with code "context_length_exceeded". When using Databricks endpoints serving Bedrock models, context overflow errors come as APIError with Bedrock-style messages like "Input is too long for requested model". These were not converted to ContextWindowOverflowException, preventing SummarizingConversationManager and other conversation managers from triggering.

Solution:

  • Added constants for Bedrock-style overflow message patterns
  • Extended exception handling to catch openai.APIError and check for these patterns
  • Maintained proper exception order (BadRequestError → RateLimitError → APIError) to avoid intercepting rate limit errors
  • Applied fix to both stream() and structured_output() methods

Impact:
Agents using OpenAI-compatible endpoints that wrap Bedrock models can now properly handle context overflow with conversation managers.

Related Issues

Closes #1528

Documentation PR

N/A

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works (existing tests validate the fix)
  • I have updated the documentation accordingly (no documentation changes needed)
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed (no new docs are needed)
  • My changes generate no new warnings
  • Any dependent changes have been merged and published (no dependencies)

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

Adds logic to recognize and handle context window overflow
errors that are returned in a Bedrock-style format by
OpenAI-compatible endpoints that wrap Bedrock models.

Handles OpenAI rate limit errors consistently

Ensures rate limit errors from OpenAI are consistently
treated as throttling exceptions across different code paths.
This prevents potential misinterpretation of rate limits as
context window overflows.

Fixes Bedrock-style overflow detection when rate limited.
Updates the model to recognize a wider range of context overflow error messages.

This change expands error handling to include messages from various providers, ensuring more robust detection of context window overflows when using OpenAI-compatible endpoints.
  1. test_stream_alternative_context_overflow_messages (3 parametrized tests)

  - Tests that stream() method properly converts APIError with alternative overflow messages to ContextWindowOverflowException
  - Parametrized to test all 3 error message patterns

  2. test_structured_output_alternative_context_overflow_messages (3 parametrized tests)

  - Tests that structured_output() method properly converts APIError with alternative overflow messages
  - Parametrized to test all 3 error message patterns

  3. test_stream_api_error_passthrough (1 test)

  - Tests that APIError without overflow messages passes through unchanged
  - Ensures we don't accidentally catch unrelated API errors

  Test Coverage

  The new tests verify:
  - ✅ All 3 alternative overflow messages are detected
  - ✅ Both stream() and structured_output() methods handle the errors
  - ✅ Errors are properly converted to ContextWindowOverflowException
  - ✅ Original exception is preserved as __cause__
  - ✅ Non-overflow APIError exceptions pass through unchanged
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] Context overflow not detected for OpenAI-compatible endpoints wrapping Bedrock

2 participants