Skip to content

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]

feat(cache): add SemanticCacheContext

Yujong Lee8dc960c92861
diff --git a/litellm-rust/crates/cache/src/base_cache.rs b/litellm-rust/crates/cache/src/base_cache.rs
index 8bd69ba5ad6..6f961798795 100644
--- a/litellm-rust/crates/cache/src/base_cache.rs
+++ b/litellm-rust/crates/cache/src/base_cache.rs
@@ -32,6 +32,31 @@ impl CacheContext for ExactCacheContext {
}
}
+#[derive(Clone, Debug, Default, PartialEq)]
+pub struct SemanticCacheContext {
+ pub input: Option<serde_json::Value>,
+ pub messages: Vec<serde_json::Value>,
+ pub metadata: serde_json::Map<String, serde_json::Value>,
+ pub scope: Option<String>,
+ pub ttl: Option<Duration>,
+}
+
+impl CacheContext for SemanticCacheContext {
+ fn ttl(&self) -> Option<Duration> {
+ self.ttl
+ }
+
+ fn with_ttl(&self, ttl: Option<Duration>) -> Self {
+ Self {
+ input: self.input.clone(),
+ messages: self.messages.clone(),
+ metadata: self.metadata.clone(),
+ scope: self.scope.clone(),
+ ttl,
+ }
+ }
+}
+
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CacheConnectionStatus {
diff --git a/litellm-rust/crates/cache/src/lib.rs b/litellm-rust/crates/cache/src/lib.rs
index ce9f93b6dc4..8364c635e3a 100644
--- a/litellm-rust/crates/cache/src/lib.rs
+++ b/litellm-rust/crates/cache/src/lib.rs
@@ -8,7 +8,7 @@ mod error;
pub use base_cache::{
BaseCache, BatchEntry, CacheConnectionResult, CacheConnectionStatus, CacheContext,
- ExactCacheContext,
+ ExactCacheContext, SemanticCacheContext,
};
pub use cache_type::CacheType;
pub use caching::{Cache, CacheBackend, get_cache, set_cache};
diff --git a/litellm-rust/crates/cache/tests/caching.rs b/litellm-rust/crates/cache/tests/caching.rs
index 9180ee9d0dc..2e65b4eeae5 100644
--- a/litellm-rust/crates/cache/tests/caching.rs
+++ b/litellm-rust/crates/cache/tests/caching.rs
@@ -1,7 +1,8 @@
use std::{sync::Mutex, time::Duration};
use litellm_cache::{
- BaseCache, CacheConnectionResult, CacheContext, Error, ExactCacheContext, get_cache,
+ BaseCache, CacheConnectionResult, CacheContext, Error, ExactCacheContext,
+ SemanticCacheContext, get_cache,
};
struct TestCache {
@@ -126,6 +127,27 @@ fn associated_context_preserves_backend_specific_lookup_inputs() {
);
}
+#[test]
+fn semantic_context_with_ttl_preserves_lookup_inputs() {
+ let context = SemanticCacheContext {
+ input: Some(serde_json::json!("text")),
+ messages: vec![serde_json::json!({"role": "user", "content": "hi"})],
+ metadata: serde_json::Map::from_iter([(
+ "key".into(),
+ serde_json::json!("value"),
+ )]),
+ scope: Some("scope".into()),
+ ttl: None,
+ };
+ let updated = context.with_ttl(Some(Duration::from_secs(30)));
+ assert_eq!(updated.ttl(), Some(Duration::from_secs(30)));
+ assert_eq!(updated.input, context.input);
+ assert_eq!(updated.messages, context.messages);
+ assert_eq!(updated.metadata, context.metadata);
+ assert_eq!(updated.scope, context.scope);
+ assert_eq!(context.with_ttl(None).ttl(), None);
+}
+
#[tokio::test]
async fn default_batch_operations_use_async_writes_and_stop_on_failure() {
let cache = TestCache {