Raise `ModelAPIError` for transport errors that escaped Google, Mistral, Cohere, Hugging Face, Anthropic, Groq and Bedrock

Merged
DO
2mo ago
+583-58

Related: #8722

A connection that can't be opened, times out, or breaks off mid-stream escaped as a raw httpx, httpx2 or urllib3 exception from several adapters, so except ModelAPIError and FallbackModel's default fallback_on=(ModelAPIError,) missed it. The same failure on OpenAI, or before the stream opens on Anthropic, Groq and Bedrock, already raises ModelAPIError. #8131 did the same for botocore's pre-stream transport errors, which is how the ModelAPIError contract has moved on since #2899.

Each reproduced on main with a mock transport, then mapped in the adapter's existing error mapper to ModelAPIError, chained from the original:

AdapterBefore
GoogleModel, GoogleEmbeddingModelraw httpx2 connect, timeout and mid-stream errors
MistralModelraw httpx2 connect, timeout and mid-stream errors
CohereModel, CohereEmbeddingModelraw httpx connect and timeout errors
HuggingFaceModelraw httpx errors, and InferenceTimeoutError
AnthropicModel, GroqModelraw httpx2 / httpx error when the stream breaks off (the SDKs wrap only until the response starts)
BedrockConverseModelraw urllib3 ReadTimeoutError / ProtocolError when the event stream breaks off

OpenAIChatModel and OpenAIResponsesModel already wrap all of these, so they're unchanged.

StreamedResponse's cancel guard suppressed only the raw transport errors that cancel() causes when it tears a stream down. Now that the adapters wrap those errors, the guard also suppresses a ModelAPIError whose __cause__ is one of them, so cancel() still ends a stream cleanly.

New public surface: none.

What changes for existing users: code that caught the raw httpx/httpx2/urllib3 exception from these adapters now gets ModelAPIError, with the original as __cause__. FallbackModel now falls back on these failures when they happen before the stream is returned.

Verification

  • test_google_connect_error_is_wrapped, test_google_stream_read_error_mid_stream_is_wrapped (tests/models/test_google.py)
  • test_model_transport_error in tests/models/test_mistral.py, test_cohere.py, test_huggingface.py
  • test_stream_transport_error_mid_stream in tests/models/test_anthropic.py, test_groq.py; test_bedrock_stream_transport_error_mid_stream (tests/models/test_bedrock.py)
  • TestCohere.test_embed_connect_error, TestGoogle.test_embed_connect_error (tests/test_embeddings.py)
  • test_anthropic_cancelled_read_error_is_suppressed covers the cancel guard; test_anthropic_read_error_is_raised_when_not_cancelled now expects ModelAPIError
  • Each new test fails with the adapter change reverted.
Not changed here
  • GoogleModel.count_tokens isn't mapped at all today; #9308 routes it through _map_api_error, which then covers transport errors too.
  • A legacy httpx.AsyncClient passed to the Google or Mistral provider (deprecated, warns) still leaks httpx errors; the default httpx2 client is covered.
  • BedrockEmbeddingModel wraps errors in an ExceptionGroup from its task group, a separate defect.
  • JSON decode errors from a malformed 200 body (#8843 / #8846) and in-stream error objects (#8722) are separate PRs.
  • The #8722 fix (in-stream error objects) landed first; this keeps its clauses in groq.py and huggingface.py.

Checklist

  • 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.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code