mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* 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>
123 lines
5.6 KiB
Python
123 lines
5.6 KiB
Python
from typing import Final
|
|
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm.router_utils.request_priority import (
|
|
InvalidPriority,
|
|
parse_default_priority,
|
|
priority_error,
|
|
request_drops_params,
|
|
resolve_request_priority,
|
|
unqueueable_priority_message,
|
|
)
|
|
from litellm.router_rust.core import _raised_before_provider
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("requested", "default_priority", "expected"),
|
|
[
|
|
(None, None, None),
|
|
(None, 3, 3),
|
|
(2, 3, 2),
|
|
(0, 3, 0),
|
|
(0, None, 0),
|
|
(255, None, 255),
|
|
("1", 3, 1),
|
|
(" 2 ", None, 2),
|
|
("-1", 3, -1),
|
|
(1.0, 3, 1),
|
|
(True, None, 1),
|
|
],
|
|
)
|
|
def test_an_absent_or_null_priority_takes_the_default_and_an_integer_like_value_wins(
|
|
requested: object, default_priority: int | None, expected: int | None
|
|
) -> None:
|
|
assert resolve_request_priority(requested, default_priority, drops_params=lambda: False) == expected
|
|
assert resolve_request_priority(requested, default_priority, drops_params=lambda: True) == expected
|
|
|
|
|
|
@pytest.mark.parametrize("requested", ["high", "", [1], 1.5, {"level": 1}])
|
|
def test_a_non_integer_priority_is_rejected_unless_params_are_dropped(requested: object) -> None:
|
|
rejected: Final = resolve_request_priority(requested, 3, drops_params=lambda: False)
|
|
assert isinstance(rejected, InvalidPriority)
|
|
assert rejected.value == requested
|
|
assert rejected.message.startswith("priority must be an integer, got ")
|
|
assert resolve_request_priority(requested, 3, drops_params=lambda: True) == 3
|
|
assert resolve_request_priority(requested, None, drops_params=lambda: True) is None
|
|
|
|
|
|
def test_the_drop_params_decision_is_consulted_only_for_a_non_integer_priority() -> None:
|
|
def _never() -> bool:
|
|
raise AssertionError("drop_params was consulted")
|
|
|
|
assert resolve_request_priority(None, 3, drops_params=_never) == 3
|
|
assert resolve_request_priority(2, 3, drops_params=_never) == 2
|
|
assert resolve_request_priority(0, None, drops_params=_never) == 0
|
|
assert resolve_request_priority("1", 3, drops_params=_never) == 1
|
|
|
|
|
|
def test_the_global_drop_params_wins_then_the_request_then_the_router_defaults_then_the_deployments(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
assert request_drops_params({}, {}, ()) is False
|
|
assert request_drops_params({"drop_params": True}, {}, ()) is True
|
|
assert request_drops_params({"drop_params": "true"}, {}, ()) is True
|
|
assert request_drops_params({}, {"drop_params": True}, ()) is True
|
|
assert request_drops_params({"drop_params": False}, {"drop_params": True}, ()) is False
|
|
assert request_drops_params({}, {}, ({"drop_params": True},)) is True
|
|
assert request_drops_params({}, {}, ({"drop_params": True}, {"model": "openai/gpt-5.4-nano"})) is True
|
|
assert request_drops_params({}, {}, ({"drop_params": False}, {"model": "openai/gpt-5.4-nano"})) is False
|
|
assert request_drops_params({}, {"drop_params": False}, ({"drop_params": True},)) is False
|
|
monkeypatch.setattr(litellm, "drop_params", True)
|
|
assert request_drops_params({}, {}, ()) is True
|
|
assert request_drops_params({}, {}, ({"model": "openai/gpt-5.4-nano"},)) is True
|
|
assert request_drops_params({"drop_params": False}, {}, ()) is True
|
|
assert request_drops_params({}, {"drop_params": False}, ()) is True
|
|
assert request_drops_params({}, {}, ({"drop_params": False},)) is True
|
|
assert request_drops_params({"drop_params": "not-a-flag"}, {}, ()) is True
|
|
|
|
|
|
def test_a_priority_named_in_additional_drop_params_at_any_level_is_dropped(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
by_name: Final = {"additional_drop_params": ["priority"]}
|
|
assert request_drops_params(by_name, {}, ()) is True
|
|
assert request_drops_params({**by_name, "drop_params": False}, {}, ()) is True
|
|
assert request_drops_params({}, by_name, ()) is True
|
|
assert request_drops_params({}, {"drop_params": False}, (by_name,)) is True
|
|
assert request_drops_params({"additional_drop_params": ["seed"]}, {}, ()) is False
|
|
assert request_drops_params({}, {}, (params for params in ({"drop_params": True},))) is True
|
|
|
|
|
|
def test_a_request_it_cannot_queue_is_told_why() -> None:
|
|
assert "priority is required" in unqueueable_priority_message(None)
|
|
assert unqueueable_priority_message("high").startswith("priority must be an integer, got str 'high'")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("configured", "expected"),
|
|
[(None, None), (3, 3), (0, 0), ("3", 3), (" 12 ", 12), ("0", 0), ("-1", -1), (2.0, 2)],
|
|
ids=["unset", "int", "zero", "digit-string", "padded-digit-string", "zero-string", "negative-string", "whole-float"],
|
|
)
|
|
def test_the_default_priority_is_taken_as_an_integer_or_an_integer_like_value(
|
|
configured: object, expected: int | None
|
|
) -> None:
|
|
assert parse_default_priority(configured) == expected
|
|
|
|
|
|
@pytest.mark.parametrize("configured", ["high", "", "1.5", 1.5, [3], {"level": 3}, True, False])
|
|
def test_any_other_default_priority_is_invalid(configured: object) -> None:
|
|
parsed: Final = parse_default_priority(configured)
|
|
assert isinstance(parsed, InvalidPriority)
|
|
assert parsed.value == configured
|
|
assert parsed.message.startswith("priority must be an integer, got ")
|
|
|
|
|
|
def test_only_litellm_own_priority_refusal_counts_as_raised_before_the_provider() -> None:
|
|
provider_rate_limit: Final = litellm.RateLimitError(
|
|
message="busy", llm_provider="openrouter", model="m", body={"message": "busy", "param": "priority"}
|
|
)
|
|
|
|
assert _raised_before_provider(priority_error("m", "priority must be an integer")) is True
|
|
assert _raised_before_provider(provider_rate_limit) is False
|