mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-24 00:52:24 +00:00
* docs(rust): plan Python interop foundation * fix(rust): preserve Python settings coercion at the native boundary * chore(rust): drop interop planning note Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat(rust): resolve OCR provider secrets through an async SecretSource before transformation Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * feat(rust): project the Python secret manager into the bridge and resolve OCR secrets through it Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(rust): drop premium_user from the secret manager snapshot Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(rust-bridge): read the private key management globals once in the settings snapshot Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(rust): bound the bridge secret manager state cache to the active snapshot Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(rust): inline coercion unit tests * fix(rust): preserve Python secret manager bindings * refactor(rust-bridge): let settings projectors own their contract specs Each settings group now declares its SettingSpec rows next to the projector that reads them, and the manifest test derives python_settings.json from those tables instead of a hand-copied duplicate. Field carries (group, name) instead of a dotted path, and coercion gains the dict-item reader plus the Redis Boolean, certificate-requirement, non-empty string, and numeric adapters that the cache configuration projection adopts next. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * refactor(rust-bridge): capture the secret manager binding in one settings read The secret_manager accessor now carries the live client and settings objects, so the bridge classifies the binding from a single snapshot instead of re-reading litellm globals. The unreachable native arm and the service alias go away, the binding-to-state mapping moves next to the snapshot, and the Python callback precomputes its key_manager name. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * refactor(rust-bridge): execute typed settings field declarations * refactor(rust-bridge): compare cache backends by identity behind one exact trait cache-response gains an object-safe ExactResponseCache so every exact-match backend sits behind one pointer; WriteBuffer flushes through it. The bridge's NativeResponseCache shrinks from nine variants and fifteen per-backend accessors to an exact service plus the three semantic backends, and facade mismatch detection compares BackendIdentity values instead of matching on each backend type. Request projections move next to NativeRequest. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * refactor(rust-bridge): drive both Python-embedded semantic caches through one execution Redis-semantic and Valkey-semantic operations now share one SemanticExecution body: await the Python embedder, seed the task-local vector, run the native backend, repeat per batch entry. Valkey drops its with_embedder path in favor of the same seeded embedder, and each backend keeps its own embedding-failure policy. PythonEmbedder exposes one call shape. Redis-semantic thresholds are compared at the backend's f32 width, which un-breaks the redis-stack parity tests that a 0.8 facade threshold failed before this branch. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * wip * feat(rust-bridge): complete response cache runtime surface * fix(rust-bridge): preserve secret manager callback exceptions * refactor(rust-bridge): unify route cache and secret rollout catalog --------- Co-authored-by: Yujong Lee <yujong@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
235 lines
8.9 KiB
Python
235 lines
8.9 KiB
Python
from collections.abc import AsyncGenerator, Awaitable, Callable, Iterator, Mapping
|
|
from dataclasses import dataclass
|
|
from typing import Final
|
|
|
|
import pytest
|
|
|
|
from litellm.rust_bridge import configuration
|
|
from litellm.rust_bridge.bindings import NativeBinding
|
|
from litellm.rust_bridge.catalog import CacheRule, Delivery, Route, RouteContext, RouteRule, Rules, SecretManagerRule
|
|
from litellm.rust_bridge.configuration import Rollout
|
|
from litellm.rust_bridge.dispatch import PublicDispatch
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Request:
|
|
model: str
|
|
|
|
|
|
def binding() -> NativeBinding[object]:
|
|
bound: Final[NativeBinding[object]] = NativeBinding("unused", validate=lambda value: value)
|
|
bound.override(None)
|
|
return bound
|
|
|
|
|
|
@pytest.mark.parametrize("rules", ((), (CacheRule(Rollout.RUST_REQUIRED), SecretManagerRule(Rollout.RUST_REQUIRED))))
|
|
def test_route_without_rules_forwards_before_request_projection(rules: Rules) -> None:
|
|
stream: Final[Iterator[int]] = iter((1, 2))
|
|
|
|
def reject_request(args: tuple[object, ...], kwargs: Mapping[str, object]) -> Request:
|
|
pytest.fail("Python-only routes must not project the request")
|
|
|
|
dispatch: Final = PublicDispatch(
|
|
route=Route.CHAT_COMPLETIONS, request=reject_request, context=lambda _: RouteContext(Route.CHAT_COMPLETIONS)
|
|
)
|
|
result: Final = dispatch.run(
|
|
("model",),
|
|
{"stream": True},
|
|
python=lambda *args, **kwargs: stream,
|
|
binding=binding(),
|
|
native=lambda hook, request, args, kwargs: pytest.fail("Python-only routes must not call native"),
|
|
rules=rules,
|
|
)
|
|
assert result is stream
|
|
|
|
|
|
def test_unconditional_python_rule_prevents_later_rust_rule_projection() -> None:
|
|
rules: Final[Rules] = (
|
|
RouteRule(Route.CHAT_COMPLETIONS, Rollout.PYTHON_ONLY),
|
|
RouteRule(Route.CHAT_COMPLETIONS, Rollout.RUST_REQUIRED),
|
|
)
|
|
|
|
def reject_request(args: tuple[object, ...], kwargs: Mapping[str, object]) -> Request:
|
|
pytest.fail("First-match Python rule must prevent request projection")
|
|
|
|
dispatch: Final = PublicDispatch(
|
|
route=Route.CHAT_COMPLETIONS,
|
|
request=reject_request,
|
|
context=lambda _: RouteContext(Route.CHAT_COMPLETIONS),
|
|
)
|
|
expected: Final = object()
|
|
result: Final = dispatch.run(
|
|
("model",),
|
|
{},
|
|
python=lambda *args, **kwargs: expected,
|
|
binding=binding(),
|
|
native=lambda hook, request, args, kwargs: pytest.fail("First-match Python rule must prevent native"),
|
|
rules=rules,
|
|
)
|
|
assert result is expected
|
|
|
|
|
|
def test_disabled_optional_rust_rule_forwards_before_projection() -> None:
|
|
rules: Final[Rules] = (RouteRule(Route.OCR, Rollout.RUST_OPT_OUT),)
|
|
|
|
def reject_request(args: tuple[object, ...], kwargs: Mapping[str, object]) -> Request:
|
|
pytest.fail("Disabled optional Rust must not project the request")
|
|
|
|
dispatch: Final = PublicDispatch(route=Route.OCR, request=reject_request, context=lambda _: RouteContext(Route.OCR))
|
|
expected: Final = object()
|
|
configuration.rust(False)
|
|
try:
|
|
result: Final = dispatch.run(
|
|
("model",),
|
|
{},
|
|
python=lambda *args, **kwargs: expected,
|
|
binding=binding(),
|
|
native=lambda hook, request, args, kwargs: pytest.fail("Disabled optional Rust must not call native"),
|
|
rules=rules,
|
|
)
|
|
finally:
|
|
configuration.rust(None)
|
|
assert result is expected
|
|
|
|
|
|
def test_native_stream_result_is_not_consumed_or_wrapped() -> None:
|
|
request: Final = Request(model="streaming-model")
|
|
stream: Final[Iterator[int]] = iter((1, 2))
|
|
rules: Final[Rules] = (
|
|
CacheRule(Rollout.PYTHON_ONLY),
|
|
SecretManagerRule(Rollout.PYTHON_ONLY),
|
|
RouteRule(Route.CHAT_COMPLETIONS, Rollout.RUST_REQUIRED, deliveries=frozenset({Delivery.STREAMING})),
|
|
)
|
|
dispatch: Final = PublicDispatch(
|
|
route=Route.CHAT_COMPLETIONS,
|
|
request=lambda args, kwargs: request,
|
|
context=lambda value: RouteContext(Route.CHAT_COMPLETIONS, model=value.model, delivery=Delivery.STREAMING),
|
|
)
|
|
|
|
def native(request: Request, args: tuple[object, ...], kwargs: Mapping[str, object]) -> Iterator[int]:
|
|
return stream
|
|
|
|
native_binding: Final[
|
|
NativeBinding[Callable[[Request, tuple[object, ...], Mapping[str, object]], Iterator[int]]]
|
|
] = NativeBinding("stream", validate=lambda _: None)
|
|
native_binding.override(native)
|
|
result: Final = dispatch.run(
|
|
("streaming-model",),
|
|
{"stream": True},
|
|
python=lambda *args, **kwargs: pytest.fail("Required native stream dispatch must not call Python"),
|
|
binding=native_binding,
|
|
native=lambda hook, value, args, kwargs: hook(value, args, kwargs),
|
|
rules=rules,
|
|
)
|
|
assert result is stream
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("rules", ((), (CacheRule(Rollout.RUST_REQUIRED), SecretManagerRule(Rollout.RUST_REQUIRED))))
|
|
async def test_async_route_without_rules_preserves_async_iterator_result(rules: Rules) -> None:
|
|
async def chunks() -> AsyncGenerator[int, None]:
|
|
yield 1
|
|
|
|
stream: Final = chunks()
|
|
|
|
def reject_request(args: tuple[object, ...], kwargs: Mapping[str, object]) -> Request:
|
|
pytest.fail("Python-only routes must not project the request")
|
|
|
|
async def python(*args: object, **kwargs: object) -> AsyncGenerator[int, None]: # kwargs-ok: pass-through shape
|
|
return stream
|
|
|
|
dispatch: Final = PublicDispatch(
|
|
route=Route.RESPONSES, request=reject_request, context=lambda _: RouteContext(Route.RESPONSES)
|
|
)
|
|
result: Final = await dispatch.arun(
|
|
("model",),
|
|
{"stream": True},
|
|
python=python,
|
|
binding=binding(),
|
|
native=lambda hook, request, args, kwargs: pytest.fail("Python-only routes must not call native"),
|
|
rules=rules,
|
|
)
|
|
assert result is stream
|
|
await stream.aclose()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_dispatch_accepts_websocket_style_none_result() -> None:
|
|
request: Final = Request(model="realtime-model")
|
|
rules: Final[Rules] = (
|
|
RouteRule(Route.RESPONSES, Rollout.RUST_REQUIRED, deliveries=frozenset({Delivery.WEBSOCKET})),
|
|
)
|
|
dispatch: Final = PublicDispatch(
|
|
route=Route.RESPONSES,
|
|
request=lambda args, kwargs: request,
|
|
context=lambda value: RouteContext(Route.RESPONSES, model=value.model, delivery=Delivery.WEBSOCKET),
|
|
)
|
|
|
|
async def python(*args: object, **kwargs: object) -> None: # kwargs-ok: public pass-through shape
|
|
pytest.fail("Required native WebSocket dispatch must not call Python")
|
|
|
|
async def native(request: Request, args: tuple[object, ...], kwargs: Mapping[str, object]) -> None:
|
|
return None
|
|
|
|
native_binding: Final[
|
|
NativeBinding[Callable[[Request, tuple[object, ...], Mapping[str, object]], Awaitable[None]]]
|
|
] = NativeBinding("websocket", validate=lambda _: None)
|
|
native_binding.override(native)
|
|
|
|
result: Final = await dispatch.arun(
|
|
("realtime-model",),
|
|
{},
|
|
python=python,
|
|
binding=native_binding,
|
|
native=lambda hook, value, args, kwargs: hook(value, args, kwargs),
|
|
rules=rules,
|
|
)
|
|
assert result is None
|
|
|
|
|
|
def test_rules_for_other_routes_and_constrained_python_rules_skip_projection() -> None:
|
|
rules: Final[Rules] = (
|
|
RouteRule(Route.MESSAGES, Rollout.RUST_REQUIRED),
|
|
RouteRule(Route.OCR, Rollout.PYTHON_ONLY, providers=frozenset({"mistral"})),
|
|
)
|
|
|
|
def reject_request(args: tuple[object, ...], kwargs: Mapping[str, object]) -> Request:
|
|
pytest.fail("Rules that cannot select Rust must not project the request")
|
|
|
|
dispatch: Final = PublicDispatch(route=Route.OCR, request=reject_request, context=lambda _: RouteContext(Route.OCR))
|
|
expected: Final = object()
|
|
result: Final = dispatch.run(
|
|
("model",),
|
|
{},
|
|
python=lambda *args, **kwargs: expected,
|
|
binding=binding(),
|
|
native=lambda hook, request, args, kwargs: pytest.fail("Rules that cannot select Rust must not call native"),
|
|
rules=rules,
|
|
)
|
|
assert result is expected
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_bypass_forwards_to_python_without_native() -> None:
|
|
request: Final = Request(model="bypassed-model")
|
|
rules: Final[Rules] = (RouteRule(Route.RESPONSES, Rollout.RUST_REQUIRED),)
|
|
dispatch: Final = PublicDispatch(
|
|
route=Route.RESPONSES,
|
|
request=lambda args, kwargs: request,
|
|
context=lambda value: RouteContext(Route.RESPONSES, model=value.model),
|
|
bypass=lambda value: value.model == "bypassed-model",
|
|
)
|
|
expected: Final = object()
|
|
|
|
async def python(*args: object, **kwargs: object) -> object: # kwargs-ok: public pass-through shape
|
|
return expected
|
|
|
|
result: Final = await dispatch.arun(
|
|
("bypassed-model",),
|
|
{},
|
|
python=python,
|
|
binding=binding(),
|
|
native=lambda hook, value, args, kwargs: pytest.fail("Bypassed requests must not call native"),
|
|
rules=rules,
|
|
)
|
|
assert result is expected
|