litellm/tests/test_litellm/rust_bridge/test_failures.py
Yujong Lee a84f68b6e3 refactor(rust_bridge): give chat completions, messages and responses the ocr dispatch shape
Each route now has litellm/rust_bridge/<route>/{entrypoints,callbacks}.py and a
public dispatch module (litellm/chat_completions/dispatch.py,
litellm/responses/dispatch.py, litellm/messages/dispatch.py) that binds the
public call to the legacy Python signature, builds a frozen request, and asks
the runtime to pick Rust or Python from the catalog. The legacy implementations
stay in litellm/main.py, litellm/responses/main.py and the anthropic messages
handler, and litellm/__init__.py re-exports the dispatch names over them the
same way it already does for ocr

The per-handler shims in rust_bridge/chat_completions/native.py and
rust_bridge/messages/native.py are removed along with their call sites in the
anthropic and bedrock chat handlers and the http handler. The exception
mapping that every callbacks module repeated moves to rust_bridge/failures.py
and the signature binding helpers to rust_bridge/public_call.py
2026-09-16 15:02:12 -07:00

54 lines
1.9 KiB
Python

from types import MappingProxyType
from typing import Final
import pytest
import litellm
from litellm.rust_bridge import failures
class UpstreamRateLimited(Exception):
status_code = 429
message = "rate limited"
def test_upstream_status_maps_onto_the_public_exception_contract() -> None:
upstream: Final = UpstreamRateLimited("rate limited")
mapped: Final = failures.map_failure(upstream, "anthropic/claude-sonnet-4-5", "anthropic", MappingProxyType({}))
assert isinstance(mapped, litellm.RateLimitError)
assert mapped.llm_provider == "anthropic"
assert mapped.model == "claude-sonnet-4-5"
def test_mapper_failure_keeps_the_native_error_as_context(monkeypatch: pytest.MonkeyPatch) -> None:
def explode(**_kwargs: object) -> Exception:
raise ValueError("mapper broke")
monkeypatch.setattr(litellm, "exception_type", explode)
native_error: Final = RuntimeError("native")
mapped: Final = failures.map_failure(native_error, "mistral/mistral-ocr-latest", "mistral", MappingProxyType({}))
assert isinstance(mapped, ValueError)
assert mapped.__context__ is native_error
def test_kwargs_are_handed_to_the_mapper_as_owned_copies(monkeypatch: pytest.MonkeyPatch) -> None:
seen: Final[list[dict[str, object]]] = []
def record(**kwargs: object) -> Exception:
seen.append(dict(kwargs))
return RuntimeError("mapped")
monkeypatch.setattr(litellm, "exception_type", record)
request_kwargs: Final = MappingProxyType({"metadata": {"user_id": "u"}})
failures.map_failure(RuntimeError("native"), "gpt-4o", "openai", request_kwargs)
assert seen[0]["completion_kwargs"] == {"metadata": {"user_id": "u"}}
assert seen[0]["extra_kwargs"] == {"metadata": {"user_id": "u"}}
assert seen[0]["completion_kwargs"] is not request_kwargs
assert seen[0]["model"] == "gpt-4o"
assert seen[0]["custom_llm_provider"] == "openai"