diff --git a/litellm-rust/crates/python-bridge/src/errors.rs b/litellm-rust/crates/python-bridge/src/errors.rs index ce8b2239534..fae46fdd261 100644 --- a/litellm-rust/crates/python-bridge/src/errors.rs +++ b/litellm-rust/crates/python-bridge/src/errors.rs @@ -1,6 +1,7 @@ use litellm_core::error::Error; use pyo3::exceptions::{PyRuntimeError, PyValueError}; use pyo3::prelude::*; +use std::fmt::{Display, Formatter}; pyo3::create_exception!( _native, @@ -28,6 +29,21 @@ pub(crate) enum BridgeError { Host(PyErr), } +impl Display for BridgeError { + fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Self::InvalidArgument(message) | Self::Declined(message) | Self::Internal(message) => { + formatter.write_str(message) + } + Self::Upstream { status, message } => match status { + Some(status) => write!(formatter, "{status}: {message}"), + None => formatter.write_str(message), + }, + Self::Host(error) => Display::fmt(error, formatter), + } + } +} + impl From for PyErr { fn from(error: BridgeError) -> Self { match error { diff --git a/litellm-rust/crates/python-bridge/src/routes/ocr.rs b/litellm-rust/crates/python-bridge/src/routes/ocr.rs index ea146b2e67a..61818d99179 100644 --- a/litellm-rust/crates/python-bridge/src/routes/ocr.rs +++ b/litellm-rust/crates/python-bridge/src/routes/ocr.rs @@ -134,6 +134,10 @@ fn prepare_ocr( }) } +fn identity_bridge_error(error: BridgeError) -> BridgeError { + error +} + bridge_route! { sync = ocr, asynchronous = aocr, @@ -159,7 +163,7 @@ bridge_route! { call_completion: Option>, }, prepare = prepare_ocr, - errors = std::convert::identity, + errors = identity_bridge_error, } #[cfg(test)]