The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor(cache-disk): tidy python adapter
Yujong Lee4a8826e03b08
diff --git a/litellm-rust/crates/cache-disk/src/cache.rs b/litellm-rust/crates/cache-disk/src/cache.rsindex 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<S, D = DiskcacheSqliteStore, A = PythonDiskCacheAdapter> {}impl<S: CacheCodec> DiskCache<S> {- #[allow(clippy::default_constructed_unit_structs)]pub fn open(directory: impl AsRef<Path>, codec: S) -> Result<Self, Error> {Ok(Self {store: Arc::new(DiskcacheSqliteStore::open(directory)?),- adapter: Arc::new(PythonDiskCacheAdapter::default()),+ adapter: Arc::new(PythonDiskCacheAdapter),codec,})}}impl<S: CacheCodec, D: DiskStore> DiskCache<S, D, PythonDiskCacheAdapter> {- #[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.rsindex 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<Option<Vec<u8>>, 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<u8>) -> StoredValue {diff --git a/litellm-rust/crates/cache-disk/tests/python_compat.rs b/litellm-rust/crates/cache-disk/tests/python_compat.rsindex 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