Raise an error status a retrying transport gave up on as `ModelHTTPError`, so fallback moves on
No issue was filed for this. It came up while writing the retries docs for #9330, part of the model request reliability work in #9335.
The retrying transport in the retries guide has a validate_response that raises httpx2.HTTPStatusError for statuses it retries, with reraise=True, so once it runs out of attempts it re-raises that exception. The provider SDK never sees a response, so it's up to the model class to map the exception, and none did:
- OpenAI (and every model class built on it), Google, Mistral, Cohere and TypeSafe let the raw
HTTPStatusErrorescape. That isn't aModelAPIError, soModelFallbackandFallbackModeldidn't fall back on it, even though the guide recommends exactly this pairing. - Anthropic and Groq wrap it in their
APIConnectionError, so it was raised asModelConnectionError. Fallback worked, but the error claimed the connection failed when the provider had responded, and its category was wrong: a 429 the transport gave up on wasn't aModelRateLimitError.
[!WARNING]
Compatibility impact: On Anthropic and Groq, an error status that a retrying transport's validate_response gave up on used to be raised as ModelConnectionError. It's now raised as ModelHTTPError, with the status and its error category (for example ModelUnavailableError for a 503, or ModelRateLimitError for a 429). Code that catches ModelConnectionError for those errors no longer catches them. It was a misclassification: the provider had responded.
Migration: Catch ModelHTTPError or the error category, or ModelAPIError for both.
The fix adds one helper, _model_errors.transport_status_error(). It builds a ModelHTTPError from the response: its status, its category from http_status_category(), its headers (so retry_after is read), and its body when one was read. A transport that closes a rejected response without reading it leaves the body None. Each affected model class calls it from its existing error mapping:
- an
except httpx2.HTTPStatusError/httpx.HTTPStatusErrorclause for the SDKs that let it through; - a check of
APIConnectionError.__cause__for Anthropic and Groq.
Hugging Face, Bedrock and xAI don't take an httpx2 client, so they can't get this error.
I reproduced each provider before the fix by raising HTTPStatusError from a mock transport. In tests/models/test_error_categories.py, these cases cover every affected provider, streamed and not (Cohere doesn't stream), plus body and header variants:
- an unread body;
- a text body that says "overloaded";
- a JSON body;
- a 429 with
retry-after.
test_model_fallback_after_retrying_transport_gives_up runs the guide's actual AsyncHTTPX2TenacityTransport with 3 attempts on OpenAI, Anthropic, Google and Mistral. For each, it checks that ModelFallback falls back after 3 wire requests and records a ModelUnavailableError with status 503. TypeSafe gets the same check with FallbackModel in test_typesafe.py. All 22 new tests fail on main.
The retries guide's "Error handling" paragraph now says what the transport's last exception is raised as.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code