diff --git a/litellm-rust/crates/cache-disk/src/cache.rs b/litellm-rust/crates/cache-disk/src/cache.rs index 98182d93968..adff078a89b 100644 --- a/litellm-rust/crates/cache-disk/src/cache.rs +++ b/litellm-rust/crates/cache-disk/src/cache.rs @@ -18,22 +18,20 @@ pub struct DiskCache { } impl DiskCache { - #[allow(clippy::default_constructed_unit_structs)] pub fn open(directory: impl AsRef, codec: S) -> Result { Ok(Self { store: Arc::new(DiskcacheSqliteStore::open(directory)?), - adapter: Arc::new(PythonDiskCacheAdapter::default()), + adapter: Arc::new(PythonDiskCacheAdapter), codec, }) } } impl DiskCache { - #[allow(clippy::default_constructed_unit_structs)] pub fn with_store(store: D, codec: S) -> Self { Self { store: Arc::new(store), - adapter: Arc::new(PythonDiskCacheAdapter::default()), + adapter: Arc::new(PythonDiskCacheAdapter), codec, } } diff --git a/litellm-rust/crates/cache-disk/src/python/mod.rs b/litellm-rust/crates/cache-disk/src/python/mod.rs index 5c9b57de000..7a370db357c 100644 --- a/litellm-rust/crates/cache-disk/src/python/mod.rs +++ b/litellm-rust/crates/cache-disk/src/python/mod.rs @@ -13,9 +13,7 @@ impl PythonDiskCacheAdapter { let value = match value { StoredValue::Bytes(value) => Value::Bytes(value), StoredValue::Text(value) => Value::String(value), - StoredValue::Integer(value) => { - value::from_json(serde_json::Value::Number(value.into())) - } + StoredValue::Integer(value) => Value::Integer(value.into()), StoredValue::Float(value) => Value::Float(value), StoredValue::Pickle(value) => value::from_pickle(&value)?, }; @@ -39,18 +37,16 @@ impl PythonDiskCacheAdapter { impl ValueAdapter for PythonDiskCacheAdapter { fn read(&self, value: StoredValue) -> Result>, Error> { - let raw = match &value { - StoredValue::Text(value) => Some(value.as_bytes().to_vec()), - StoredValue::Bytes(value) => Some(value.clone()), - StoredValue::Integer(_) | StoredValue::Float(_) | StoredValue::Pickle(_) => None, - }; - let Some(value) = Self::python_get_cache(value)? else { - return Ok(None); - }; - if let Some(raw) = raw { - return Ok(Some(raw)); + match value { + StoredValue::Text(value) => Ok((!value.is_empty()).then(|| value.into_bytes())), + StoredValue::Bytes(value) => Ok((!value.is_empty()).then_some(value)), + value => { + let Some(value) = Self::python_get_cache(value)? else { + return Ok(None); + }; + value::to_json(&value).map(Some) + } } - value::to_json(&value).map(Some) } fn write(&self, payload: Vec) -> StoredValue { diff --git a/litellm-rust/crates/cache-disk/tests/python_compat.rs b/litellm-rust/crates/cache-disk/tests/python_compat.rs index f9e12af6424..9cbef8573bd 100644 --- a/litellm-rust/crates/cache-disk/tests/python_compat.rs +++ b/litellm-rust/crates/cache-disk/tests/python_compat.rs @@ -43,6 +43,14 @@ enum ReadExpectation { )] #[case::integer_seven(StoredValue::Integer(7), ReadExpectation::Bytes(b"7"))] #[case::float_one_point_five(StoredValue::Float(1.5), ReadExpectation::Bytes(b"1.5"))] +#[case::pickled_true( + StoredValue::Pickle(vec![0x80, 0x05, 0x88, 0x2e]), + ReadExpectation::Bytes(b"true") +)] +#[case::pickled_negative_integer( + StoredValue::Pickle(vec![0x80, 0x05, 0x95, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0xfd, 0xff, 0xff, 0xff, 0x2e]), + ReadExpectation::Bytes(b"-3") +)] #[case::pickled_bytes( StoredValue::Pickle(vec![0x80, 0x05, 0x95, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x05, 0x62, 0x79, 0x74, 0x65, 0x73, 0x94, 0x2e]), ReadExpectation::Invalid