diff --git a/litellm-rust/crates/host-python/src/lib.rs b/litellm-rust/crates/host-python/src/lib.rs index 382c01d9525..37f8a2c9e4b 100644 --- a/litellm-rust/crates/host-python/src/lib.rs +++ b/litellm-rust/crates/host-python/src/lib.rs @@ -28,7 +28,7 @@ pub use fork_gate::RuntimeAlreadyStarted; pub use gil::{release_count, release_gil}; pub use handle::{Execution, ExecutionBody, ExecutionStep}; pub use marshal::{ - Pythonized, from_py, from_py_argument, json_object_field, panic_to_pyerr, to_py, + Pythonized, from_py, from_py_argument, json_loads, json_object_field, panic_to_pyerr, to_py, }; /// Starts the interpreter and imports the standard modules the tests share, once, so diff --git a/litellm-rust/crates/host-python/src/marshal.rs b/litellm-rust/crates/host-python/src/marshal.rs index 669ae37cc68..53f11d8c40a 100644 --- a/litellm-rust/crates/host-python/src/marshal.rs +++ b/litellm-rust/crates/host-python/src/marshal.rs @@ -4,6 +4,7 @@ use std::panic::{AssertUnwindSafe, catch_unwind}; use pyo3::exceptions::PyValueError; use pyo3::panic::PanicException; use pyo3::prelude::*; +use pyo3::types::PyBytes; use serde::Serialize; use serde::de::DeserializeOwned; @@ -39,6 +40,12 @@ pub fn json_object_field(py: Python<'_>, document: &str, name: &str) -> PyResult .map(Bound::unbind) } +pub fn json_loads(py: Python<'_>, document: &[u8]) -> PyResult> { + py.import("json")? + .call_method1("loads", (PyBytes::new(py, document),)) + .map(Bound::unbind) +} + pub struct Pythonized(pub T); impl<'py, T> IntoPyObject<'py> for Pythonized diff --git a/litellm-rust/crates/python-bridge/src/secrets/mutation.rs b/litellm-rust/crates/python-bridge/src/secrets/mutation.rs index 0ae8c2c5256..eaab06a5b12 100644 --- a/litellm-rust/crates/python-bridge/src/secrets/mutation.rs +++ b/litellm-rust/crates/python-bridge/src/secrets/mutation.rs @@ -1,5 +1,5 @@ use super::operations::{PythonMutationError, PythonMutationResponse}; -use litellm_host_python::to_py; +use litellm_host_python::{json_loads, to_py}; use litellm_secrets::cyberark; use pyo3::{exceptions::PyValueError, prelude::*, types::PyDict}; @@ -85,9 +85,7 @@ fn os_error_code(error: &(dyn std::error::Error + 'static)) -> Option { } pub(super) fn json_value(py: Python<'_>, body: &[u8]) -> PyResult> { - py.import("json")? - .call_method1("loads", (pyo3::types::PyBytes::new(py, body),)) - .map(Bound::unbind) + json_loads(py, body) } pub(super) fn error_value(py: Python<'_>, message: String) -> PyResult> { diff --git a/litellm-rust/crates/python-bridge/src/secrets/vault/operation.rs b/litellm-rust/crates/python-bridge/src/secrets/vault/operation.rs index e332f6cb581..cf56beb5efe 100644 --- a/litellm-rust/crates/python-bridge/src/secrets/vault/operation.rs +++ b/litellm-rust/crates/python-bridge/src/secrets/vault/operation.rs @@ -114,7 +114,7 @@ pub(crate) async fn rotate( let status = raw_object_field(parsed, "status") .ok() .flatten() - .and_then(|value| serde_json::from_str::(value.get()).ok()); + .and_then(|value| serde_json::from_slice::(value.get().as_bytes()).ok()); if status.as_deref() == Some("error") { return Ok(response); } @@ -134,7 +134,7 @@ pub(crate) async fn rotate( let actual = verification_value(parsed, data_key).map_err(|failure| { Failure::from(failure).during(FailureStage::Replacement(new_name.to_owned())) })?; - let actual_string = serde_json::from_str::(actual.get()).ok(); + let actual_string = serde_json::from_slice::(actual.get().as_bytes()).ok(); if actual_string.as_deref() != Some(value.expose()) { return Err(FailureKind::ValueMismatch { expected: value.clone(), @@ -162,7 +162,7 @@ fn raw_object_field<'a>( document: &'a RawValue, key: &str, ) -> Result, FailureKind> { - let object: HashMap = serde_json::from_str(document.get()) + let object: HashMap = serde_json::from_slice(document.get().as_bytes()) .map_err(|_| FailureKind::MissingGet(document.get().as_bytes().to_vec()))?; Ok(object.get(key).copied()) }