litellm/litellm-rust/crates/callbacks-legacy-python/src/lib.rs
Yujong Lee 0c1e9d3131 feat(callbacks-legacy-python): run Messages body hooks on native calls
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
2026-10-11 19:14:46 -07:00

58 lines
1.9 KiB
Rust

//! The legacy `@client` wrapper as the native call sees it: litellm's `Logging` object, the
//! sync and async callback registries it fans out to, the deployment hooks and the deferred
//! proxy release. All of it sits behind one
//! [`PythonCallHooks`](litellm_host_python::PythonCallHooks), so the driver, the routes and
//! core never learn which Python object is on the other end.
//!
//! Legacy callbacks receive the caller's own objects and may mutate them. [`PublicCall`]
//! is where those objects live.
mod adapter;
mod body;
mod call;
mod callbacks;
mod deferred;
mod logger;
mod mapping;
mod python;
pub use adapter::LegacyLogging;
pub use body::HandlerBody;
pub use call::PublicCall;
pub(crate) use callbacks::{LegacyCallbacks, is_internal_call};
use litellm_host_python::HookChain;
pub(crate) use logger::{DeploymentHooks, PythonLogger, finalize, setup};
pub use mapping::{CallBoundary, CallbackMapping, Dispatch, Operations, callback_mappings};
use pyo3::Python;
use strum::IntoStaticStr;
#[derive(Clone, Copy, Debug, IntoStaticStr, PartialEq, Eq)]
pub enum LoggingOperation {
#[strum(serialize = "completion")]
Completion,
#[strum(serialize = "responses")]
Responses,
#[strum(serialize = "messages")]
Messages,
#[strum(serialize = "ocr")]
Ocr,
}
/// The legacy contract for one native call as layers: `Logging` for every route, and inside
/// it the body hooks the operation's async Python handler fires, so the body transforms the
/// response before the wrapper's own success hook sees it.
pub fn legacy_hooks(
py: Python<'_>,
operation: LoggingOperation,
call: PublicCall,
asynchronous: bool,
) -> HookChain {
let body = HandlerBody::new(py, operation, &call, asynchronous);
let chain = HookChain::new().with(LegacyLogging::new(py, operation, call, asynchronous));
match body {
Some(body) => chain.with(body),
None => chain,
}
}
#[cfg(test)]
mod test_support;