litellm/tests/unit/anthropic_interface/exceptions
Jc X cbed72f102
fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809)
* fix(openai): preserve error body, param, and message from OpenAI-compatible backends

* fix(proxy): redact secrets and internal details from relayed error type and param

* test: use a fake key the master key guard allows in the redaction tests

* fix: log the HTTP status as error_code, retry rejected reasoning by its error code, and relay only Responses param names from the chat bridge

* fix: relay the backend's extra error fields such as content_filters as provider_specific_fields, and keep unrelated test formatting unchanged

* fix: relay only OpenAI-style error types, name bridged params as the Responses field, and log HTTP statuses as error_code

A backend type outside OpenAI's snake_case vocabulary, such as vLLM's BadRequestError, now answers with the type its status stands for. The Responses bridge maps a chat param back to the field the caller sent (max_tokens to max_output_tokens, reasoning_effort to reasoning.effort, response_format to text.format, a tool path to tools) and keeps a param naming one of the caller's own options. A non-string param relays as null. error_code logs a carried code only when it is an HTTP error status, so a numeric provider code such as 1211 logs the HTTP status. Extra error fields relay only from an OpenAI-shaped error object, never with NaN or Infinity, and their keys are redacted like their values

* fix: relay a structured upstream error message as JSON and keep the provider's own error code

A backend error body whose message is an object, like Mistral's 422 validation detail, was rendered as a Python dict repr once the body reached the exception. get_error_message now returns only string messages, so those errors fall back to the backend's JSON text again

The provider's own error code, such as previous_response_not_found or content_filter, now rides in error.provider_specific_fields.code when it is not the HTTP status, since error.code keeps the status

* fix(logging): log the provider's own error code as error_provider_code next to the HTTP status

* fix(logging): read the provider's error code from the error body when the exception carries the HTTP status

A 429 RateLimitError pins code to 429, so insufficient_quota from openai/ and azure/ never reached error_provider_code. Azure's 429 mapping now keeps the error body like openai/'s

* fix(logging): redact secrets in the logged provider error code

* refactor(openai): build on the merged JSON error decode and keep the Responses body

The OpenAI exception mapping now decodes a JSON-text error into its body and
plain message, and text completion raises carry the SDK body. Drop this
branch's copies of that work and reuse core_helpers.http_error_status.

Azure's mapping does not decode the text, so the Responses error class still
passes the parsed body. It moves from the LiteLLM Proxy config to the OpenAI
Responses config, so Azure Responses errors relay param and a plain message

* fix(responses): name a rejected verbosity as text.verbosity on the chat bridge

* fix(proxy): relay provider error fields on mid-stream error frames and name a bridged message path as input

A streamed request that fails after its first chunk now carries error.provider_specific_fields like the non-streamed answer does. On the /v1/responses chat bridge, a backend param under messages now answers input, the Responses field the messages came from, instead of null

* fix(proxy): keep logged error codes as before, relay Azure chat and OpenRouter error fields, and name a bridged instructions message

Logging goes back to main's rule, so error_code keeps whatever code the exception carries and error_provider_code is gone. Responses failures on the shared error class now log the provider code, as chat failures have since #28405

The proxy relays provider fields from the error body and from fields the exception already carries in one merge, so an Azure chat content filter error keeps its code next to content_filter_result, and a body without a message, like xAI's, still relays its code

Azure's content policy mapping keeps the provider's param. The OpenRouter mapper unwraps the error message and passes the body, so its metadata is relayed

On the chat bridge, messages[0] answers instructions when the caller sent instructions, and a messages path answers null after previous_response_id, since stored turns come first

* fix(proxy): keep OpenRouter's upstream error reason in the message and redact carried provider fields

OpenRouter wraps an upstream provider's rejection as "Provider returned error" with the real reason in metadata.raw, so the message now carries that reason again. Provider fields the exception already carries go through the same redaction as the ones read from the error body, mid-stream frames included

Tests cover the OpenRouter upstream reason, the Azure 429 provider code and the redaction of carried fields and body keys

get_error_message takes a typed object and reads the body through as_str_mapping, which removes unknown-type diagnostics and leaves room under the type gate's ceiling for a basedpyright result that flips between runs

* fix(proxy): state OpenRouter's upstream error reason once in the message

OpenRouter's metadata.raw holds the upstream provider's whole error body, and on /v1/responses OpenRouter's own message already is that provider's message, so the relayed message repeated the reason and then the raw JSON. The message now appends only the upstream message decoded from metadata.raw, skips it when it matches OpenRouter's message, and keeps plain text raw as is. The full raw value stays in provider_specific_fields.metadata

The JSON error decoding that _relayed_openai_error did moves to _decoded_openai_error so both read an OpenAI-shaped error body the same way

* fix(proxy): keep OpenRouter responses cooldowns, send object error messages as JSON, and relay provider fields in stream frames

The OpenAI Responses config raises its error class again, as the base config does. Returning it made the handler mark the 500 as made up by LiteLLM, and OpenRouter maps that to APIConnectionError, which the router never cools down, so an unreachable OpenRouter deployment kept taking traffic

An error body whose message is an object, like Mistral's 422 validation detail, now relays the whole error object as JSON. OpenAI SDK providers sent the SDK's "Error code: 422" dict repr before, while the others already sent the raw JSON

Chat and responses stream error frames now carry provider_specific_fields. The field the proxy streaming generator set only reached the Anthropic and A2A serializers, which never send it, so that line is gone

* fix(proxy): redact credentials from relayed provider errors and stream frames

Structured values under credential keys, sibling fields of a structured message, OpenRouter messages, and the mid-stream error frame could carry an upstream credential to the client

* fix(openai): relay provider error fields without moving logged error codes

Mapped errors now carry the provider body, so code, param, and extra fields reach clients on every status, including /v1/messages before a stream starts. Chat mid-stream frames carry them too, and /v1/responses mid-stream frames carry the code and extra fields

Logged error_code stays what it was at the base. OpenRouter and Azure Responses failures relay the provider code to the client and keep logging their HTTP status, and litellm_proxy keeps logging the provider code

Relayed fields and param redact credentials and stop at the recursion cap, the Responses bridge only names a field the caller sent, and a provider 429 naming priority still cools the deployment down

* fix(proxy): redact credential-named error fields and keep provider fields on mid-stream errors

* fix(anthropic): relay provider fields on /v1/messages stream error frames

* fix(proxy): redact credentials inside JSON text in relayed provider error fields

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(responses): stop reloading proxy_server in the streaming iterator fixture

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor(logging): parse embedded JSON through the existing client-field adapters

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(responses): refuse the connection in-process in the router cooldown test

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(responses): inject a refusing transport through the handler's client factory

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: xinglan163 <250816997+xinglan163@users.noreply.github.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-24 02:23:00 +00:00
..
__init__.py test: add __init__.py to migrated tests/unit packages 2026-09-20 11:53:49 +00:00
test_exception_mapping_utils.py fix(openai): relay provider error codes, types, and Responses params from OpenAI-compatible backends (#51809) 2026-11-24 02:23:00 +00:00