mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
refactor(rust_bridge): decode native upstream failures in one place
runtime._raise_upstream now reads RustUpstreamError through failures.decode_upstream instead of hand-parsing its args, so the attempt path and map_native_failure agree on what a native failure is. decode_upstream treats a missing headers attribute as no headers, which route_error_to_pyerr errors never set Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ada3614eb4
commit
bbfe9ee7bf
4 changed files with 73 additions and 12 deletions
|
|
@ -3,6 +3,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from typing import Final, Protocol, cast # noqa: TID251 # adapts the public exception mapper
|
||||
|
||||
import httpx
|
||||
|
|
@ -12,7 +13,23 @@ from pydantic import TypeAdapter, ValidationError
|
|||
import litellm
|
||||
|
||||
_UPSTREAM_ARGS: Final = TypeAdapter(tuple[int, str])
|
||||
_UPSTREAM_HEADERS: Final = TypeAdapter(list[tuple[str, str]])
|
||||
_UPSTREAM_HEADERS: Final = TypeAdapter(tuple[tuple[str, str], ...])
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class UpstreamReply:
|
||||
status: int
|
||||
body: str
|
||||
headers: tuple[tuple[str, str], ...]
|
||||
|
||||
|
||||
def decode_upstream(error: BaseException) -> UpstreamReply | None:
|
||||
try:
|
||||
status, body = _UPSTREAM_ARGS.validate_python(error.args)
|
||||
headers: Final = _UPSTREAM_HEADERS.validate_python(getattr(error, "headers", ()))
|
||||
except ValidationError:
|
||||
return None
|
||||
return UpstreamReply(status=status, body=body, headers=headers)
|
||||
|
||||
|
||||
class UpstreamFailure(Exception):
|
||||
|
|
@ -25,14 +42,12 @@ class UpstreamFailure(Exception):
|
|||
|
||||
|
||||
def _upstream_failure(error: Exception, api_base: str | None) -> Exception:
|
||||
try:
|
||||
status, body = _UPSTREAM_ARGS.validate_python(error.args)
|
||||
headers: Final = _UPSTREAM_HEADERS.validate_python(getattr(error, "headers", None))
|
||||
except ValidationError:
|
||||
reply: Final = decode_upstream(error)
|
||||
if reply is None:
|
||||
return error
|
||||
http_request: Final = httpx.Request("POST", api_base or "https://docs.litellm.ai/docs")
|
||||
return UpstreamFailure(
|
||||
httpx.Response(status, content=body.encode(), headers=headers, request=http_request),
|
||||
httpx.Response(reply.status, content=reply.body.encode(), headers=reply.headers, request=http_request),
|
||||
error,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ from litellm.exceptions import APIError
|
|||
from litellm.rust_bridge.bindings import NativeBinding, native_exception_types
|
||||
from litellm.rust_bridge.catalog import RouteContext, Rules, decision
|
||||
from litellm.rust_bridge.configuration import Decision
|
||||
from litellm.rust_bridge.failures import decode_upstream
|
||||
from litellm.rust_bridge.response_metadata import mark_rust_response
|
||||
|
||||
NativeT = TypeVar("NativeT")
|
||||
|
|
@ -208,13 +209,11 @@ def _required_reason(result: RustDeclined | RustUnavailable) -> str:
|
|||
|
||||
|
||||
def _raise_upstream(error: BaseException, context: BridgeErrorContext) -> NoReturn:
|
||||
args: Final[tuple[object, ...]] = error.args
|
||||
status_value: Final = args[0] if args else 0
|
||||
message_value: Final = args[1] if len(args) > 1 else str(error)
|
||||
status: Final = status_value if isinstance(status_value, int) else 0
|
||||
message: Final = message_value if isinstance(message_value, str) else str(message_value)
|
||||
reply: Final = decode_upstream(error)
|
||||
status: Final = reply.status if reply is not None and reply.status else 500
|
||||
message: Final = reply.body if reply is not None else str(error)
|
||||
raise APIError(
|
||||
status_code=status or 500,
|
||||
status_code=status,
|
||||
message=f"litellm rust {context.route}: {message}",
|
||||
llm_provider=context.provider,
|
||||
model=context.model,
|
||||
|
|
|
|||
|
|
@ -22,6 +22,22 @@ def test_upstream_status_maps_onto_the_public_exception_contract() -> None:
|
|||
assert mapped.model == "claude-sonnet-4-5"
|
||||
|
||||
|
||||
class RustUpstreamError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def test_native_failure_without_headers_maps_from_its_status() -> None:
|
||||
native_error: Final = RustUpstreamError(429, "rate limited")
|
||||
|
||||
mapped: Final = failures.map_native_failure(
|
||||
native_error, "anthropic/claude-sonnet-4-5", "anthropic", MappingProxyType({})
|
||||
)
|
||||
|
||||
assert isinstance(mapped, litellm.RateLimitError)
|
||||
assert mapped.status_code == 429
|
||||
assert mapped.__context__ is native_error
|
||||
|
||||
|
||||
def test_mapper_failure_keeps_the_native_error_as_context(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
def explode(**_kwargs: object) -> Exception:
|
||||
raise ValueError("mapper broke")
|
||||
|
|
|
|||
|
|
@ -23,6 +23,12 @@ class RustUpstreamError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class RustUpstreamErrorWithHeaders(RustUpstreamError):
|
||||
def __init__(self, status: object, body: object, headers: object) -> None:
|
||||
super().__init__(status, body)
|
||||
self.headers: Final = headers
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def native_exceptions(monkeypatch: pytest.MonkeyPatch) -> Generator[None]:
|
||||
native: Final = SimpleNamespace(
|
||||
|
|
@ -335,6 +341,31 @@ def test_upstream_error_maps_to_api_error_without_fallback() -> None:
|
|||
assert calls.calls == (RUST,)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("failure", "status", "message"),
|
||||
(
|
||||
(RustUpstreamErrorWithHeaders(429, "rate limited", [("retry-after", "7")]), 429, "rate limited"),
|
||||
(RustUpstreamErrorWithHeaders(0, "connection reset", []), 500, "connection reset"),
|
||||
(RustUpstreamErrorWithHeaders(429, "rate limited", "not headers"), 500, "(429, 'rate limited')"),
|
||||
(RustUpstreamError("no status"), 500, "no status"),
|
||||
),
|
||||
)
|
||||
def test_upstream_error_agrees_with_the_native_failure_decoder(
|
||||
failure: RustUpstreamError, status: int, message: str
|
||||
) -> None:
|
||||
calls: Final = recorder(failure)
|
||||
|
||||
with pytest.raises(APIError) as caught:
|
||||
run(Rollout.RUST_OPT_OUT, calls)
|
||||
|
||||
assert caught.value.status_code == status
|
||||
assert caught.value.message == f"litellm.APIError: litellm rust messages: {message}"
|
||||
assert caught.value.llm_provider == "anthropic"
|
||||
assert caught.value.model == "model"
|
||||
assert caught.value.__cause__ is failure
|
||||
assert calls.calls == (RUST,)
|
||||
|
||||
|
||||
def test_other_native_errors_propagate_without_fallback() -> None:
|
||||
failure: Final = ValueError("admitted")
|
||||
calls: Final = recorder(failure)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue