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]

refactor(rust): share settings lookup and layer merge through core-utils

Yujong Leecaf37c8b6f21
diff --git a/litellm-rust/Cargo.lock b/litellm-rust/Cargo.lock
index d4b32659ba1..83cdbc6a782 100644
--- a/litellm-rust/Cargo.lock
+++ b/litellm-rust/Cargo.lock
@@ -2137,6 +2137,7 @@ version = "0.1.0"
dependencies = [
"http 1.4.2",
"hyper-util",
+ "litellm-core-utils",
"reqwest 0.12.28",
"rstest",
"rustls 0.23.42",
@@ -2187,6 +2188,7 @@ dependencies = [
"litellm-auth-gcp",
"litellm-callbacks-legacy",
"litellm-core",
+ "litellm-core-utils",
"litellm-host-python",
"litellm-http",
"litellm-llms",
diff --git a/litellm-rust/crates/core-utils/src/lib.rs b/litellm-rust/crates/core-utils/src/lib.rs
index fcb232d8980..ceb0e9eb3f2 100644
--- a/litellm-rust/crates/core-utils/src/lib.rs
+++ b/litellm-rust/crates/core-utils/src/lib.rs
@@ -6,4 +6,5 @@ pub mod params;
pub mod prompt_templates;
pub mod secret_redaction;
pub mod serde_compat;
+pub mod settings;
pub mod url_utils;
diff --git a/litellm-rust/crates/core-utils/src/settings.rs b/litellm-rust/crates/core-utils/src/settings.rs
new file mode 100644
index 00000000000..59c76ce3015
--- /dev/null
+++ b/litellm-rust/crates/core-utils/src/settings.rs
@@ -0,0 +1,144 @@
+use std::str::FromStr;
+
+pub trait Lookup {
+ fn get(&self, name: &str) -> Option<String>;
+
+ fn truthy(&self, name: &str) -> Option<String> {
+ self.get(name).filter(|value| !value.is_empty())
+ }
+
+ fn enabled(&self, name: &str) -> Option<bool> {
+ self.get(name)
+ .is_some_and(|value| value.trim().eq_ignore_ascii_case("true"))
+ .then_some(true)
+ }
+
+ fn parsed<T: FromStr>(&self, name: &str) -> Option<T>
+ where
+ Self: Sized,
+ {
+ self.get(name).and_then(|value| value.trim().parse().ok())
+ }
+}
+
+impl<F: Fn(&str) -> Option<String>> Lookup for F {
+ fn get(&self, name: &str) -> Option<String> {
+ self(name)
+ }
+}
+
+pub struct ProcessEnvironment;
+
+impl Lookup for ProcessEnvironment {
+ fn get(&self, name: &str) -> Option<String> {
+ std::env::var(name).ok()
+ }
+}
+
+pub trait Layer: Default {
+ fn or(self, lower: Self) -> Self;
+}
+
+pub fn merge<L: Layer>(highest_precedence_first: impl IntoIterator<Item = L>) -> L {
+ highest_precedence_first
+ .into_iter()
+ .reduce(L::or)
+ .unwrap_or_default()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn env_of(values: &'static [(&'static str, &'static str)]) -> impl Fn(&str) -> Option<String> {
+ move |name| {
+ values
+ .iter()
+ .find(|(key, _)| *key == name)
+ .map(|(_, value)| value.to_string())
+ }
+ }
+
+ #[test]
+ fn get_keeps_a_present_empty_value_like_os_getenv_with_a_fallback() {
+ let env = env_of(&[("EMPTY", "")]);
+ assert_eq!(env.get("EMPTY"), Some(String::new()));
+ assert_eq!(env.get("ABSENT"), None);
+ }
+
+ #[test]
+ fn truthy_drops_an_empty_value_like_a_python_or_chain() {
+ let env = env_of(&[("EMPTY", ""), ("SET", "value")]);
+ assert_eq!(env.truthy("EMPTY"), None);
+ assert_eq!(env.truthy("SET").as_deref(), Some("value"));
+ }
+
+ #[test]
+ fn enabled_only_switches_on_for_true_and_never_forces_off() {
+ let env = env_of(&[
+ ("LOWER", "true"),
+ ("PADDED", " True "),
+ ("OFF", "false"),
+ ("ONE", "1"),
+ ]);
+ assert_eq!(env.enabled("LOWER"), Some(true));
+ assert_eq!(env.enabled("PADDED"), Some(true));
+ assert_eq!(env.enabled("OFF"), None);
+ assert_eq!(env.enabled("ONE"), None);
+ assert_eq!(env.enabled("ABSENT"), None);
+ }
+
+ #[test]
+ fn parsed_trims_and_skips_values_that_do_not_parse() {
+ let env = env_of(&[("PADDED", " 45 "), ("WORD", "soon"), ("FRACTION", "0.5")]);
+ assert_eq!(env.parsed::<u32>("PADDED"), Some(45));
+ assert_eq!(env.parsed::<u32>("WORD"), None);
+ assert_eq!(env.parsed::<f64>("FRACTION"), Some(0.5));
+ assert_eq!(env.parsed::<u32>("ABSENT"), None);
+ }
+
+ #[derive(Debug, Default, PartialEq)]
+ struct Pair {
+ first: Option<u8>,
+ second: Option<u8>,
+ }
+
+ impl Layer for Pair {
+ fn or(self, lower: Self) -> Self {
+ Self {
+ first: self.first.or(lower.first),
+ second: self.second.or(lower.second),
+ }
+ }
+ }
+
+ #[test]
+ fn merge_takes_each_field_from_the_highest_layer_that_sets_it() {
+ let merged = merge([
+ Pair {
+ first: Some(1),
+ second: None,
+ },
+ Pair {
+ first: Some(2),
+ second: Some(2),
+ },
+ Pair {
+ first: Some(3),
+ second: Some(3),
+ },
+ ]);
+ assert_eq!(
+ merged,
+ Pair {
+ first: Some(1),
+ second: Some(2),
+ }
+ );
+ }
+
+ #[test]
+ fn merging_no_layers_yields_the_empty_layer() {
+ assert_eq!(merge(Vec::<Pair>::new()), Pair::default());
+ }
+}
diff --git a/litellm-rust/crates/http/Cargo.toml b/litellm-rust/crates/http/Cargo.toml
index 0ac09a9d155..b0dc7693840 100644
--- a/litellm-rust/crates/http/Cargo.toml
+++ b/litellm-rust/crates/http/Cargo.toml
@@ -7,6 +7,7 @@ repository.workspace = true
[dependencies]
http.workspace = true
+litellm-core-utils.workspace = true
hyper-util.workspace = true
reqwest.workspace = true
rustls.workspace = true
diff --git a/litellm-rust/crates/http/src/settings.rs b/litellm-rust/crates/http/src/settings.rs
index 8ac7ef92568..43c7f6223d2 100644
--- a/litellm-rust/crates/http/src/settings.rs
+++ b/litellm-rust/crates/http/src/settings.rs
@@ -3,6 +3,8 @@ use std::{
time::Duration,
};
+use litellm_core_utils::settings::{Layer, Lookup, merge};
+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum SslVerify {
Enabled,
@@ -45,38 +47,35 @@ pub struct HttpSettingsLayer {
}
impl HttpSettingsLayer {
- pub fn from_environment(env: &(dyn Fn(&str) -> Option<String> + Sync)) -> Self {
- let enabled = |name: &str| {
- env(name)
- .is_some_and(|value| value.trim().eq_ignore_ascii_case("true"))
- .then_some(true)
- };
- let number = |name: &str| env(name).and_then(|value| value.trim().parse::<u32>().ok());
+ pub fn from_environment(env: &impl Lookup) -> Self {
let seconds = |name: &str, default: u32| {
- Duration::from_secs(u64::from(number(name).unwrap_or(default)))
+ Duration::from_secs(u64::from(env.parsed::<u32>(name).unwrap_or(default)))
};
Self {
- ssl_verify: env("SSL_VERIFY").map(|value| SslVerify::parse(&value)),
- ssl_cert_file: env("SSL_CERT_FILE").map(PathBuf::from),
- ssl_certificate: env("SSL_CERTIFICATE").map(PathBuf::from),
- ssl_security_level: env("SSL_SECURITY_LEVEL"),
- ssl_ecdh_curve: env("SSL_ECDH_CURVE"),
+ ssl_verify: env.get("SSL_VERIFY").map(|value| SslVerify::parse(&value)),
+ ssl_cert_file: env.truthy("SSL_CERT_FILE").map(PathBuf::from),
+ ssl_certificate: env.get("SSL_CERTIFICATE").map(PathBuf::from),
+ ssl_security_level: env.get("SSL_SECURITY_LEVEL"),
+ ssl_ecdh_curve: env.get("SSL_ECDH_CURVE"),
force_ipv4: None,
- http2: enabled("LITELLM_HTTP2"),
- aiohttp_trust_env: enabled("AIOHTTP_TRUST_ENV"),
- disable_aiohttp_trust_env: enabled("DISABLE_AIOHTTP_TRUST_ENV"),
- disable_aiohttp_transport: enabled("DISABLE_AIOHTTP_TRANSPORT"),
- user_agent: env("LITELLM_USER_AGENT"),
- tcp_keepalive: enabled("AIOHTTP_SO_KEEPALIVE").map(|_| TcpKeepalive {
+ http2: env.enabled("LITELLM_HTTP2"),
+ aiohttp_trust_env: env.enabled("AIOHTTP_TRUST_ENV"),
+ disable_aiohttp_trust_env: env.enabled("DISABLE_AIOHTTP_TRUST_ENV"),
+ disable_aiohttp_transport: env.enabled("DISABLE_AIOHTTP_TRANSPORT"),
+ user_agent: env.get("LITELLM_USER_AGENT"),
+ tcp_keepalive: env.enabled("AIOHTTP_SO_KEEPALIVE").map(|_| TcpKeepalive {
idle: seconds("AIOHTTP_TCP_KEEPIDLE", 60),
interval: seconds("AIOHTTP_TCP_KEEPINTVL", 30),
- retries: number("AIOHTTP_TCP_KEEPCNT").unwrap_or(5),
+ retries: env.parsed("AIOHTTP_TCP_KEEPCNT").unwrap_or(5),
}),
- pool_idle_timeout: number("AIOHTTP_KEEPALIVE_TIMEOUT")
+ pool_idle_timeout: env
+ .parsed::<u32>("AIOHTTP_KEEPALIVE_TIMEOUT")
.map(|timeout| Duration::from_secs(u64::from(timeout))),
}
}
+}
+impl Layer for HttpSettingsLayer {
fn or(self, lower: Self) -> Self {
Self {
ssl_verify: self.ssl_verify.or(lower.ssl_verify),
@@ -139,10 +138,7 @@ impl HttpSettings {
pub fn from_layers(
highest_precedence_first: impl IntoIterator<Item = HttpSettingsLayer>,
) -> Self {
- let merged = highest_precedence_first
- .into_iter()
- .reduce(HttpSettingsLayer::or)
- .unwrap_or_default();
+ let merged = merge(highest_precedence_first);
let defaults = Self::default();
let http2 = merged.http2.unwrap_or(defaults.http2);
Self {
@@ -190,9 +186,7 @@ mod tests {
None
}
- fn env_of(
- values: &'static [(&'static str, &'static str)],
- ) -> impl Fn(&str) -> Option<String> + Sync {
+ fn env_of(values: &'static [(&'static str, &'static str)]) -> impl Fn(&str) -> Option<String> {
move |name| {
values
.iter()
diff --git a/litellm-rust/crates/python-bridge/Cargo.toml b/litellm-rust/crates/python-bridge/Cargo.toml
index c66701548d1..8d31855f2fa 100644
--- a/litellm-rust/crates/python-bridge/Cargo.toml
+++ b/litellm-rust/crates/python-bridge/Cargo.toml
@@ -20,6 +20,7 @@ bytes.workspace = true
litellm-auth.workspace = true
litellm-callbacks-legacy.workspace = true
litellm-core.workspace = true
+litellm-core-utils.workspace = true
litellm-auth-gcp.workspace = true
litellm-http.workspace = true
litellm-llms.workspace = true
diff --git a/litellm-rust/crates/python-bridge/src/http.rs b/litellm-rust/crates/python-bridge/src/http.rs
index d174dccaa56..1fc3e4a60f1 100644
--- a/litellm-rust/crates/python-bridge/src/http.rs
+++ b/litellm-rust/crates/python-bridge/src/http.rs
@@ -4,6 +4,7 @@ use std::{
sync::{Arc, LazyLock, Mutex, PoisonError},
};
+use litellm_core_utils::settings::ProcessEnvironment;
use litellm_http::{
HttpClientConfig, HttpClientPool, HttpSettings, HttpSettingsLayer, Resolution, SslVerify,
Unsupported,
@@ -29,7 +30,7 @@ pub(crate) fn call_config(
) -> PyResult<HttpClientConfig> {
let settings = HttpSettings::from_layers([
for_call(call_ssl_verify(kwargs)?, asynchronous),
- HttpSettingsLayer::from_environment(&|name| std::env::var(name).ok()),
+ HttpSettingsLayer::from_environment(&ProcessEnvironment),
configured(&PythonSettings::Http.read(py)?)?,
])
.without_missing_files(&|path: &Path| path.exists());
@@ -232,7 +233,7 @@ user_agent='litellm/9.9.9',
Python::initialize();
Python::attach(|py| {
let settings = HttpSettings::from_layers([
- HttpSettingsLayer::from_environment(&|name| {
+ HttpSettingsLayer::from_environment(&|name: &str| {
(name == "LITELLM_USER_AGENT").then(|| "operator/1".to_string())
}),
configured(&python_settings(py, "")).unwrap(),