mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-06 02:09:04 +00:00
The auth flags read through python-compat's IsTrue adapter and public_routes through RouteList on both hosts, and python-bridge reads those keys through GeneralSettings' own serde rules. A flag is on only for a boolean true, and a public_routes value that is not a list of strings adds no public route, so the Rust gateway and the Python proxy read the same settings the same way.
7.1 KiB
7.1 KiB
Ownership
- This package owns
- choosing Rust or Python for each call, and falling back to Python when Rust is unavailable
- Python public API compatibility at the native boundary
- projecting Python settings into the values Rust reads
- the Python bindings handed to the Rust bridge
- It does not own
- provider execution, which is Rust core
- CPython runtime mechanics, which is
litellm-host-python - legacy callback sharing and dispatch policy, which is
callbacks-legacy-python
host/holds the modules Rust imports by name to reach Python-owned state and behavior. Only Rust calls them, and Rust reaches Python nowhere else.host/AGENTS.mdstates that contract
Routing
catalog.pydecides, the detectors describe the request,dispatch.pyapplies the decision- Shared detectors live in
detectors.py; route-only detectors live in<route>/detectors.py - The first matching rule wins. A context no rule matches stays on Python
- Every Python-only exclusion for a route (a
targetsorfeaturesrule) sits above any Rust rule for that route - A route whose Python side is
NO_PYTHONresolves toRUST_REQUIREDfor every context it can producetests/test_litellm_rust/test_public_entries.pymarks such a routepython=None, so dropping a route's Python changes its entries, its rules and that field together
- A detector reads the same inputs the legacy Python path reads, so the catalog and the fallback agree on which requests need Python
- A route's context, its target as well as its features, reads
NativeCall.resolved, so an argument counts the same whether it came by position, by keyword or as a signature default - A route's sync and async dispatches read different detector maps when the legacy handlers differ, as the sync Responses handler runs the deployment hook itself. Messages reads one map for both, since Rust serves its async-only pre-request hooks, web search short-circuit and interceptors through host calls
- A route's context, its target as well as its features, reads
- A detector that raises keeps the call on Python.
PublicDispatchlogs it and never fails the call over routing - Messages for providers
anthropicandazure_aiisRUST_REQUIREDand has no Python path- their Python request code (
AnthropicMessagesConfig,AzureAnthropicMessagesConfig) stays as the live reference the parity suite runs the Python leg through, as Vertex and Bedrock keep theirs - a call the rules would send to Python (a
MESSAGES_PYTHON_FEATURESfeature or a raising detector) raisesUnsupportedParamsErrorfrom the Python fallback inlitellm/messages/dispatch.py, which names the detected features - other Messages providers keep the Python fallback
- their Python request code (
- A Messages call targets
MESSAGESonly when its provider's own Messages config serves it. A deployment with no such config that lists/v1/messagesinmodel_info.supported_endpoints, like a non-Claudeazure_aimodel, targetsMESSAGES_PASSTHROUGH, which Python's OpenAI-compatible config forwards untranslated and which stays on Python. Anazure_aicall bridged to Chat Completions or Responses stays on Python the same way catalog.RULESis rollout policy with no tests of its own. Tests pass the rules they need, so check these invariants by review wheneverRULESchanges
Public entries
- Every route serves
a<entry>(...),<entry>(..., a<entry>=True)anda<entry>(..., a<entry>=True)the same way, handing native one identicalNativeCallwith no async flag on the async binding tests/test_litellm_rust/test_public_entries.pyholds this for every route in its table, so a new public route gets a row there
Execution
execution.pyowns generic inline execution, stream iteration and the step classes Rust constructs- Native entrypoints return an unstarted
Execution.execution.synchronousorexecution.asynchronoushands it to the caller throughrunordrive, so Rust never names a Python driver or stream class - Keep one driver, implementing generator-style
send(send(None)starts),throwand idempotentclose - Generic steps and suspension handling never depend on LiteLLM response metadata. Public stream construction,
_hidden_paramsand header compatibility stay in the product wrappers, and generic stream heads stay opaque - Await each selected suspension in the caller's task, so hooks keep their thread, loop and context
- Return a final awaitable as a value instead of awaiting it implicitly
- Closing, cancellation and
GeneratorExitrelease the execution without replaying provider work or starting more callbacks
Fallback
- Native admission may pick Python before execution starts
- A caller or deployment
api_basethe native URL parser reads otherwise than httpx and aiohttp (no host, credentials in the URL, a port Python cannot read, a missing http(s) scheme, whitespace, control or non-ASCII characters) goes to Python's client through_unsendable_api_baseinhost/routes/messages.py. The native parser can read a host out of such a base where Python refuses it, sending the operator's key to a host the configuration did not name or mean - A failure after execution starts is terminal and never triggers fallback
- Creating an unstarted native coroutine acquires no clients or credentials and captures no execution context
The LITELLM_RUST switch
decisiontreats a blank or invalidLITELLM_RUSTas unset and caches the parse by raw value. It runs inside SDK calls, so it never fails one over a switch and never validates per call- It does not put the Rust gateway in front of Python. That is
LITELLM_RUST_FIRST, whichlitellm/proxy/rust_forward/mode.pyreads strictly at startup with this sameRustSwitchtype, stopping the CLI with one line naming the variable
Native extension
_nativeships in every wheel and the sdist builds it, so it is always importable- Import it with
from litellm.rust_bridge import _nativeand use the attributes_native.pyideclares - Never add a
Nonebranch,getattrdefault or stand-in class for a missing extension
Verification
- Check cross-language behavior against a freshly installed extension, covering unstarted-call release, setup ordering, exception identity, sync and async streams, cancellation and GC
- Keep
_native.pyiaccurate about which APIs return a Future and which return a coroutine _native.pyiis hand-written, so declare what PyO3 builds at runtime, not what reads nicer (classes, typing)- A fieldless
#[pyclass]enum is a plain class, never anenum.Enum. Stub each variant asVariant: ClassVar[Cls], and match it with value patterns, which pyright cannot check for exhaustiveness - An enum with fields exposes each variant as a nested subclass of the enum class, with getters for its fields,
__match_args__, and a__new__that takes the fields in order eqwithouthashmakes instances unhashable, so stub__hash__: ClassVar[None] # type: ignore[assignment], the way typeshed does.make test-rust-extensionruns mypy stubtest over the stub, and it stops at the first mypy error
- A fieldless
- Unit tests of this package live in
tests/unit/rust_bridge/, end-to-end Rust path tests intests/test_litellm_rust/