Wrap botocore transport errors in `ModelAPIError` in `BedrockConverseModel`

Merged
KI
3mo ago
+75-9

BedrockConverseModel only mapped ClientError, so botocore's own transport failures (ReadTimeoutError, EndpointConnectionError, ConnectionClosedError, ...), which derive from BotoCoreError, escaped agent.run() unwrapped and bypassed FallbackModel (default fallback_on=(ModelAPIError,)) and except ModelAPIError handlers. Repro and details in #8130.

The fix wraps BotoCoreError in _call_bedrock, the helper that opens converse, converse_stream and count_tokens, raising ModelAPIError(model_name, str(e)) from e like the existing ClientError-without-status branch does. It deliberately doesn't go into _map_api_errors: that context manager also guards the chunk reads in BedrockStreamedResponse._get_event_iterator, and the cancel guard (get_stream_cancel_errors -> (BotoCoreError, ClientError)) needs botocore's raw types there after cancel(). This mirrors anthropic.py / openai.py, which wrap APIConnectionError on the request and let bare httpx errors propagate from chunk reads (see the base get_stream_cancel_errors docstring).

BeforeAfter
ReadTimeoutError / EndpointConnectionError while opening converse, converse_stream or count_tokensraw botocore exception out of agent.run()ModelAPIError, botocore error kept as __cause__
FallbackModel(bedrock, other) on a Bedrock timeout while opening the requestrun fails with ReadTimeoutErrorfalls back to other
ClientError with HTTP status (throttling, validation, 5xx)ModelHTTPErrorunchanged
ClientError without status, EventStreamError mid-streamModelAPIError / ModelHTTPErrorunchanged
botocore errors raised from chunk reads mid-stream, including after cancel()rawunchanged

Test: test_bedrock_botocore_error drives request, request_stream and count_tokens through the existing _StubBedrockClient with a ReadTimeoutError (the stub's type hint is widened to ClientError | BotoCoreError). Not a VCR test since a cassette can't make botocore time out. With the source change reverted it fails as:

>       raise self._error
E       botocore.exceptions.ReadTimeoutError: Read timeout on endpoint URL: "https://bedrock.stub"

Locally: tests/models/test_bedrock.py, tests/providers/test_bedrock.py and tests/models/test_fallback.py pass (332 tests); ruff format --check, ruff check and pyright are clean on both files.

Code that caught the raw botocore exception around agent.run() will now see ModelAPIError instead, with the original as __cause__, the same as it already does for Anthropic/OpenAI connection errors.

Out of scope: embeddings/bedrock.py has the same except ClientError-only handling around invoke_model; left as is to keep this to the model.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver — n/a.
  • PR title is fit for the release changelog.