mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(proxy): answer only a 4xx or 5xx carried status and fall back to the route default otherwise
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Has been cancelled
LiteLLM Rust DB / db-queries (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Has been cancelled
LiteLLM Rust DB / db-queries (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
A numeric-string status outside 400 to 599 made /v1/messages, images, rerank and the OpenAI pass-through answer HTTP 200 with an error body or drop the connection, and non-ASCII digits or a 5000-digit string slipped through isdecimal. error_status_code now accepts an int or a three-digit ASCII string in 400 to 599, the range the chat route already enforces
This commit is contained in:
parent
6d678cf098
commit
c7ea544bfa
2 changed files with 29 additions and 12 deletions
|
|
@ -26,6 +26,8 @@ _OPENAI_ERROR_TYPE_BY_STATUS: Final[Mapping[int, str]] = MappingProxyType(
|
|||
|
||||
_AUTH_FAILURE_STATUS_CODES: Final = frozenset({status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN})
|
||||
|
||||
_HTTP_ERROR_STATUSES: Final = range(status.HTTP_400_BAD_REQUEST, 600)
|
||||
|
||||
_FAILOVER_SQLSTATES: Final = frozenset({"25006", "57P01", "57P02", "57P03"})
|
||||
|
||||
_FAILOVER_REFUSALS: Final = (
|
||||
|
|
@ -43,15 +45,26 @@ def attribute_of(value: object, name: str, default: object = None) -> object:
|
|||
|
||||
|
||||
def error_status_code(exc: object, default: int) -> int:
|
||||
"""The HTTP status an exception carries as ``status_code`` (an int or a decimal string) or, the
|
||||
way ``ProxyException`` stores it, as a stringified ``code``; ``default`` when it carries neither."""
|
||||
carried: Final = attribute_of(exc, "status_code")
|
||||
if isinstance(carried, int) and not isinstance(carried, bool):
|
||||
"""The 4xx or 5xx HTTP status an exception carries as ``status_code`` (an int or a decimal string) or,
|
||||
the way ``ProxyException`` stores it, as a stringified ``code``; ``default`` when it carries neither."""
|
||||
carried: Final = _http_error_status(attribute_of(exc, "status_code"))
|
||||
if carried is not None:
|
||||
return carried
|
||||
if isinstance(carried, str) and carried.isdecimal():
|
||||
return int(carried)
|
||||
stringified: Final = attribute_of(exc, "code")
|
||||
return int(stringified) if isinstance(stringified, str) and stringified.isdecimal() else default
|
||||
from_code: Final = _http_error_status(stringified) if isinstance(stringified, str) else None
|
||||
return default if from_code is None else from_code
|
||||
|
||||
|
||||
def _http_error_status(value: object) -> int | None:
|
||||
match value:
|
||||
case bool():
|
||||
return None
|
||||
case int() if value in _HTTP_ERROR_STATUSES:
|
||||
return value
|
||||
case str() if len(value) == 3 and value.isascii() and value.isdigit():
|
||||
return _http_error_status(int(value))
|
||||
case _:
|
||||
return None
|
||||
|
||||
|
||||
def openai_error_type(exc: object, status_code: int) -> str:
|
||||
|
|
|
|||
|
|
@ -111,9 +111,13 @@ def test_the_default_status_stands_when_the_exception_carries_none(default: int)
|
|||
assert error_status_code(ValueError("boom"), default) == default
|
||||
|
||||
|
||||
@pytest.mark.parametrize("carried_status", [True, False, "slow down", "", None, 429.0])
|
||||
def test_a_carried_status_that_is_not_a_status_falls_back_to_the_default(carried_status: object):
|
||||
"""True is an int in Python but not an HTTP status."""
|
||||
@pytest.mark.parametrize(
|
||||
"carried_status",
|
||||
[True, False, "slow down", "", None, 429.0, 200, 1301, 0, "200", "1301", "0", "0429", " 429", "\u0664\u0662\u0669", "4" * 5000],
|
||||
)
|
||||
def test_a_carried_status_that_is_not_an_error_status_falls_back_to_the_default(carried_status: object):
|
||||
"""True is an int in Python but not an HTTP status, and answering a 200 or a 1301 either reads
|
||||
as success or makes uvicorn drop the connection."""
|
||||
|
||||
class _Carrier(Exception):
|
||||
status_code = carried_status
|
||||
|
|
@ -145,8 +149,8 @@ def test_a_proxy_exception_keeps_the_status_it_was_raised_with():
|
|||
assert error_status_code(rejection, 500) == 400
|
||||
|
||||
|
||||
@pytest.mark.parametrize("carried_code", [None, "None", "", "rate_limited", "4xx", 404])
|
||||
def test_a_code_that_is_not_a_decimal_string_falls_back_to_the_default(carried_code: object):
|
||||
@pytest.mark.parametrize("carried_code", [None, "None", "", "rate_limited", "4xx", 404, "200", "1301"])
|
||||
def test_a_code_that_is_not_a_stringified_error_status_falls_back_to_the_default(carried_code: object):
|
||||
"""Only ProxyException's stringified status is a status; ``code`` on anything else
|
||||
(OpenAI's ``invalid_api_key``, a stray int) says nothing about the HTTP answer."""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue