The Python driver was a hand-written continuation machine: every hook and host call returned a step, the driver parked a resume function in a Pending slot, and the hook chain kept per-phase state to pick up where it stopped. This replaces that with one async body per call, polled only from the caller's Python task through a new PythonTask primitive. Hooks and host calls are plain async fns that await Python awaitables through PythonTask::settle, so the caller's task and contextvars context still run every hook of a call, and the core coroutine stays as it is. HookStep, HostStep, HookResume, HostResume and the Pending enum are gone. The hook chain is a loop over boxed async hooks. The legacy callback adapter awaits its deployment hooks and failure handlers in place, and the cache binding awaits the Python cache the same way. The handle shares the binding, hooks and keyword view with the body through a lock released around native steps and stream boundaries, so GC traversal still reaches retained Python objects while a call waits on the runtime or on its consumer. A new driver test pins that an abandoned stream retained by a hook is collected. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
7.5 KiB
- Target invariants. Implementation and runtime validation may lag these rules
Boundary with Python consumers
This crate owns CPython execution mechanics for generic litellm-host machines and hooks. Consumers supply domain bindings, host operations, public result construction and callback policy. Neither Rust dependencies nor Python imports may require LiteLLM route modules or legacy Logging
src/native.rs belongs here: it runs a generic machine through the Python runtime and owns its pending execution and abort handle. Keep provider selection, request projection and public exception policy out of it. A rename to machine_runner.rs is optional and must not change behavior
The execution handle receives its Python lifecycle binding from its consumer through PythonLifecycle rather than import a fixed litellm.rust_bridge module. Generic suspension and execution state validation belong here. Public stream wrappers and _hidden_params conventions belong to the consumer
Creating a resolved asyncio Future from an already constructed Python value belongs here, alongside runtime waiting, interpreter detachment and panic containment. Choosing which callable exceptions become a public RuntimeError belongs to the consumer. python-bridge::callable::wrap_failure owns that policy
The driver owns ordering: start, argument preparation, prepared-argument hooks, binding decode and machine start. Fallible per-call resource setup supplied by the consumer runs after all argument hooks, using the prepared argument view, and before provider work. Setup failure follows the existing terminal failure path. Creating or discarding an unstarted coroutine must not initialize clients, acquire credentials or capture execution context
Boundary tests exercise behavior with a supplied lifecycle binding without importing the LiteLLM Python package. Pin inline awaiting, awaitable final values, exception identity, cancellation, re-entry and release of retained objects, rather than module names or source layout
HookChain composes Python runtime hooks in order. Each argument, wire-request and response transformation feeds its result to the next hook. After all argument transformations, the driver calls arguments_prepared on every hook in order. Retained callback views must adopt that dictionary before later policy hooks can mutate or reject it. SDK policy is supplied by bridge composition as a hook, never a separate driver phase or parameter. Hooks implement only the stages they need. Default stages preserve the supplied values
Terminal notifications share the selected response or exception. An ordinary notification error is reported as unraisable and does not skip the next hook or replace the selected outcome. Preparation, interception and transformation errors stop the chain. Cancellation stops all further hook dispatch
Hooks and host calls are async fns that receive the PythonTask. A Python call's result goes through PythonTask::settle, which awaits an awaitable on the caller's task when the call is asynchronous and returns a cancellation as the outer error. No hook returns a continuation or keeps pending state between steps: whatever a suspended hook holds lives on its own future, and dropping the future releases it
Existing runtime invariants
- Keep this crate the CPython runtime adapter and nothing more: Serde marshalling, interpreter detachment, tokio/asyncio glue, the
Executionhandle, thePythonTask, the call driver and thePythonBinding,PythonHostCallsandPythonOwnedtraits, and thePythonRuntimespecialization ofhost::hooks::CallHooks- No LiteLLM domain dependencies beyond
litellm-host: no route types, noLoggingpolicy, no public API registration, no cdylib build features PythonCallHooksonly constrains the shared call-stage interface toPythonRuntimeand Python ownership. It must not redeclare the stagesPythonCallEventis a specialization of the sharedCallEvent, never a separately defined lifecycle. The driver emitsSucceededorFailedexactly once and never dispatches callbacks after a cancellation. Which Python objects consume those events is the legacy adapter's businessCallOptionscan publish snapshots independently of callback delivery. Terminal snapshots follow completed hook dispatch, and cancellation never calls a Python callback. For Python-driven calls, leave the machine's observation publisher unset so the driver is the sole publisher of intercepted provider-response snapshotsPythonBinding::decode_requestreceives the keyword view returned byprepare_argumentsand updated byarguments_prepared, not the caller's dict. A binding that decodes from it inherits all composed argument rewrites- A native failure, including one a host op returns as
InvokeError::Native, is classified exactly once through the binding'smap_error. A Python exception raised inside the call, and a failure inprepare_argumentsortransform_response, is raised as is - A failing
map_erroris raised with the native error's text as its__context__, never swallowed
- No LiteLLM domain dependencies beyond
- Use standard PyO3 ownership and conversion APIs
- Prefer
Bound<'py, T>for attached operations/results,Py<T>for retention. Binding/unbinding does not copy payloads - Use
pythonizefor selected Serde data, never a JSON-text round trip. Share conversion withPythonized<T> - Preserve
PythonizeError's standard conversion intoPyErr. Do not stringify original Python exceptions into newValueErrors - Keep serializer-panic containment in
Pythonized<T>: async output conversion can run in an unjoined blocking task and otherwise strand delivery
- Prefer
- Use
Python::detachfor Rust-only work. Python operations require attachment- Keep diagnostic counters in the consumer. Wrapper invocations do not measure every interpreter release
- Release exclusive class borrows/locks before Python calls or decrements that can invoke finalizers. Expose retained Python edges to GC without calling Python during traversal
- Keep coroutine driving in the shared Python driver and the native handle
- Shared driver implementation:
litellm/rust_bridge/lifecycle.py, handle:src/handle.rs, task:src/task.rs, call driver:src/driver.rs, native-backed behavior tests:tests/lifecycle.py. The consumer supplies the lifecycle binding - The call is one async body polled only from
Execution.resume_*throughPythonTask::resume, with a noop waker. Nothing in that body may await tokio I/O directly: the native machine is polled inline and otherwise becomes a Python awaitable, and every other await goes throughPythonTask::suspend - Every lifecycle suspension is awaited inline in the caller's task.
into_futurecreates a separate task and cannot satisfy this contract - The body shares the binding, hooks and keyword view with the handle through a lock it releases around every native step and stream boundary, so GC traversal sees them while the call waits on the runtime or its consumer. Traversal is blind only while a hook's own awaitable is in flight
- Shared driver implementation:
- References: ownership, conversions, pythonize errors