Add `ModelRequestRejectedError`, `ModelAuthenticationError` and `ModelNotFoundError` model error categories
Follow-up to #9354; no separate issue.
#9354 gave transient failures (rate limits, overloads, server and connection errors) and context window overflows a category of their own. Errors where the provider refused the request itself were left out: a bad API key, a missing permission, an unknown model, a malformed request. They still came as a plain ModelHTTPError with status 400, 401, 403, 404 or 422, so telling a permanent rejection apart from a transient failure meant reading status codes. This PR adds one category for them, with two subclasses, built the same way as #9354's.
[!WARNING]
Compatibility impact: Existing code that matches model errors by exact type or by class name may behave differently:
- A 400, 401, 403, 404 or 422 from a model or embedding adapter is now an instance of a
ModelHTTPError subclass. Exact-type checks like type(e) is ModelHTTPError no longer match it.
- Such an error now shows under its category's name (e.g.
ModelAuthenticationError: status_code: 401, ...) in tracebacks, repr(), OTel exception.type, and the ModelRequestAttempt.error strings in failed_attempts. Before, it showed as ModelHTTPError.
- A VoyageAI embedding error with one of these statuses used to be a plain
ModelAPIError. It is now a ModelHTTPError, as VoyageAI 429 and 5xx errors already were after #10804. So except ModelHTTPError now catches it, a Temporal retry policy sees it as ModelHTTPError, and its str(e) takes ModelHTTPError's form. The provider's text is still in body.
ModelContextWindowExceededError is now a subclass of the new ModelRequestRejectedError, so except ModelRequestRejectedError also catches context window overflows. Code written before this PR can't have used that class, so nothing that worked before changes.
- An OpenAI-compatible in-stream error with code
model_not_found used to be a plain ModelAPIError. It is now a ModelNotFoundError, still not a ModelHTTPError. Under Temporal, its ApplicationError type stays ModelAPIError.
Why this can ship in a minor release: Every error that matched an except or isinstance check before still matches it. Every error that had a status keeps its status code, body, headers, message and suggested_model_id. OTel attributes and repr() may change in a minor release, and the class name in an error string is not a documented format. #9354 shipped the same kind of change for its categories. Under Temporal, an error with a category still reports ModelHTTPError or ModelAPIError as its ApplicationError type, so non_retryable_error_types still matches what it matched before.
Migration: Match errors with except or isinstance (e.g. except ModelAuthenticationError) instead of exact-type checks or class-name strings in logs, alerts or failed_attempts. To keep VoyageAI 4xx errors out of an except ModelHTTPError, catch them first by category.
New public surface (all exported from pydantic_ai):
ModelRequestRejectedError(ModelAPIError): the provider rejected the request, so sending it unchanged won't succeed. Used for 400 and 422, and for gRPCINVALID_ARGUMENT, when no more specific category applies.ModelAuthenticationError(ModelRequestRejectedError): covers 401 and 403; gRPCUNAUTHENTICATEDandPERMISSION_DENIED; BedrockAccessDeniedException,UnrecognizedClientException,InvalidSignatureExceptionandExpiredToken(Exception); OpenAI-compatibleinvalid_api_key; and Gemini'sAPI_KEY_INVALIDreason, which Gemini sends as a 400.ModelNotFoundError(ModelRequestRejectedError): covers 404; gRPCNOT_FOUND; BedrockResourceNotFoundException; OpenAI-compatiblemodel_not_found, which the Responses API sends as a 400; and Bedrock'sThe provided model identifier is invalid., which Bedrock sends as a 400ValidationException. The adapter already matched that message to setsuggested_model_id.ModelContextWindowExceededError(#9354) is now a subclass ofModelRequestRejectedError, so a retry predicate that skipsModelRequestRejectedErrorskips overflows too. Its defaultFallbackModelbehavior doesn't change:FallbackModelfalls back on everyModelAPIError, and a model with a larger context window may accept the input.ModelHTTPError.for_categoryaccepts the three classes, and each has a private combined HTTP class like #9354's.
Precedence. A more specific category always wins over the generic rejection. Nothing #9354 categorized changes: a 400 context window overflow is still a ModelContextWindowExceededError (now a ModelRequestRejectedError subclass), and Anthropic's "credit balance is too low" 400 and Bedrock's ServiceQuotaExceededException 400 are still only a ModelQuotaExceededError. Other 4xx statuses (408, 409, 413, ā¦) stay a plain ModelHTTPError.
Coverage. http_status_category maps the statuses, so every adapter that uses it gets the categories. The provider-specific helpers map the provider codes: openai_compatible_category, google_status_category (plus a new google_error_reason), bedrock_code_category, and xAI's _GRPC_STATUS_CATEGORIES. That covers OpenAI Chat and Responses, OpenRouter, Groq, Anthropic (including stream error events, which already get their status from the error type), Google (HTTP and stream), Mistral, Cohere, Hugging Face, Bedrock (HTTP and stream exception events), xAI, the DecisionModel adapters, and the OpenAI, Google, Cohere, Bedrock and VoyageAI embedding models. suggested_model_id is still set wherever it was before.
Temporal. ModelRequestRejectedError is in _ERROR_CATEGORIES, which also covers ModelContextWindowExceededError now, so a categorized error without a status still reports ModelAPIError to a retry policy and is rebuilt as its own class in the workflow. non_retryable_model_errors=[ModelAuthenticationError] works through isinstance. Listing ModelRequestRejectedError covers both subclasses.
Docs. I updated the category table and precedence paragraph in docs/models/overview.md, and added a should_retry example that skips permanent rejections. The FallbackModel section, the Temporal non_retryable_model_errors section and its example, docs/retries.md, and the agent skill reference now mention the new classes.
Verification
tests/models/test_error_categories.py: 41 new cases across OpenAI (incl. in-stream), OpenRouter, Anthropic (incl. in-stream), Groq, Google, Mistral, Cohere, Hugging Face, Bedrock (incl. stream) and xAI. They send wire-shaped errors through each SDK and assert the exact set of classes. Precedence: the existing context window and quota 400 cases now also assert that they are not aModelRequestRejectedError. A 409 and Anthropic's 413request_too_largestay unclassified.tests/test_model_name_suggestions.py: the recorded model-name errors from OpenAI Chat (404), OpenAI Responses (400), Anthropic, Google, Bedrock (400) and Groq are all aModelNotFoundErrorand keep theirsuggested_model_id.tests/test_embeddings_error_categories.py: new cases for OpenAI, Google, Cohere, Bedrock and VoyageAI. I ran the VoyageAI cases on Python 3.13; VoyageAI isn't installable on 3.14.- Temporal: categorized round-trips and
_retry_policy_typefor the new classes. A workflow test checks that a listedModelAuthenticationErrorfails on its first attempt, while an unlisted siblingModelNotFoundErroris still retried. DroppingModelRequestRejectedErrorfrom_ERROR_CATEGORIESmakes the non-HTTP case fail. check_api_compatibility.py --against v2.59.0passes.
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