Raise `ModelAPIError` for transport errors that escaped Google, Mistral, Cohere, Hugging Face, Anthropic, Groq and Bedrock
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:
| Adapter | Before |
|---|---|
GoogleModel, GoogleEmbeddingModel | raw httpx2 connect, timeout and mid-stream errors |
MistralModel | raw httpx2 connect, timeout and mid-stream errors |
CohereModel, CohereEmbeddingModel | raw httpx connect and timeout errors |
HuggingFaceModel | raw httpx errors, and InferenceTimeoutError |
AnthropicModel, GroqModel | raw httpx2 / httpx error when the stream breaks off (the SDKs wrap only until the response starts) |
BedrockConverseModel | raw 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_errorintests/models/test_mistral.py,test_cohere.py,test_huggingface.pytest_stream_transport_error_mid_streamintests/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_suppressedcovers the cancel guard;test_anthropic_read_error_is_raised_when_not_cancellednow expectsModelAPIError- Each new test fails with the adapter change reverted.
Not changed here
GoogleModel.count_tokensisn't mapped at all today; #9308 routes it through_map_api_error, which then covers transport errors too.- A legacy
httpx.AsyncClientpassed to the Google or Mistral provider (deprecated, warns) still leakshttpxerrors; the defaulthttpx2client is covered. BedrockEmbeddingModelwraps errors in anExceptionGroupfrom 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.pyandhuggingface.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