mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
Python's async Messages handler runs async_pre_request_hook over the resolved call before the provider transform and the agentic-loop hooks over the complete response. The native route ran neither, so the T7 body rows of CustomLogger were lost on the Rust path Add a prepare_request stage to the host CallHooks contract, driven after every prepare_arguments and before arguments_prepared, and walk the response transformation outbound so the innermost layer transforms first. MessagesBody in the legacy crate hands the resolved request to the handler's pre-request fan-out through the caller's task, writes only the keys the hooks changed back into the keyword layer, and runs the handler's agentic orchestration on the complete response. legacy_hooks composes it inside LegacyLogging for async Messages calls The callback table gains the PrepareRequest boundary and per-operation qualifiers and is published as custom_logger_contract.json. A Rust test pins the file to the table and a Python test pins it to every public CustomLogger method, so a new hook fails until the table says where the native call runs it. async_agentic_loop_cleanup_hook moves to the Python-owned row since only the Responses surface fires it
4 KiB
4 KiB
- Target invariants, not completion claims
- This crate owns compatibility for all existing Python callbacks and loggers, including
CustomLogger.mapping.rsowns the executable call bindings and the inventory of Python-owned hooks. A Python-owned entry records an existing path, never permission to invoke it a second time. The native call adapter preserves theLoggingcontract (function_setup, the deployment hooks,pre_call/post_call, the sync and async success and failure fan-out, the deferred proxy release, the argument sharing those callbacks rely on)- Smell test: if a future callback host (
callbacks-v1-python, WASM, in-process Rust) could share a piece of this crate, it does not belong here - SDK request policy (credential inheritance, the budget and retry-count limits) is a separate hook supplied by
python-bridge. Compose it after this adapter so logging adopts the final keyword view before policy mutates or rejects it HandlerBodyis the Python handler body an async route runs around the provider call, keyed byLoggingOperation: for Messages theasync_pre_request_hookfan-out at the request stage and the agentic-loop orchestration at the response stage.legacy_hookscomposes it insideLegacyLoggingso the body's response replaces the one the wrapper's success hook andLoggingsee, as on the Python path. Both fan-outs stay incallbacks_legacy_python.py, dispatch on the operation name and borrow the handler's own functions; the body hook only hands them the resolved request and lays their changed keys over the keyword layer. Adding a route's body means a new operation arm in those two functions and its rows inmapping.rs, never a second hook type- The driver in
litellm-host-python, the routes and core see onePythonCallHooksusing the sharedCallEvent. They never learn which Python objects consume a call
- Smell test: if a future callback host (
- Every litellm Python internal Rust still borrows is a variant of
LegacyPython, grouped by subsystem, with its signature pinned inpython_contract.json- The enum only shrinks: when Rust owns a subsystem, delete its group rather than adding a Rust path beside it
- Calling a user's own callback directly is permanent Python surface and gets its own type outside
LegacyPython
PublicCallis the caller's call asLoggingsees it: the positional arguments, the keyword view as the call rewrites it (setup, deployment hook, preflight) and the bound request object backing omitted keywords. Shared bridge composition hands it toLegacyLogging. Routes use the neutral call boundaryLoggingOperationselects legacy logging entrypoints and response handling. It belongs here rather than in shared inference data contractssetupreuses aLoggingpassed aslitellm_logging_obj(the proxy and Router) and otherwise builds one throughfunction_setup. Which callbacks run isLogging's decision, never this crate's- Callbacks receive the caller's own objects and may mutate them. This crate alone carries that obligation
- Retain complete boundary arguments, opaque values, aliases, omitted/default distinctions and deliberate copies. Preserve the deployment-hook kwargs view
- Before
pre_call, re-alias every body key whose value equals the caller's argument to the caller's own object, resolved throughlitellm_host_python::lookup - Retain body/header roots from
pre_calltopost_call. In-place mutation reaches the wire, envelope field replacement is visible to later callbacks only
- Success and failure handlers receive the exact selected public response or exception
- A failure-handler error cannot suppress the other eligible family or replace the mapped provider error. A cancellation ends the call with no further dispatch
- Dispatch errors never replay provider work or trigger the opposite outcome. The proxy releases deferred success at most once
- Delivery follows the registry, not the callable's type: direct, awaited, executor-submitted, logging-worker and deferred paths stay distinct
- Traverse every retained Python edge.
closeis idempotent and restores the correlation context once