This commit is contained in:
yujonglee 2026-10-26 21:42:10 +00:00 • committed by GitHub
commit 51d2be4f22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 362 additions and 146 deletions

View file

@ -8,22 +8,6 @@ impl<F: Fn(&str) -> Option<String>> Lookup for F {
}
}
pub fn resolve_non_empty(
value: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
names: &[&str],
) -> Option<String> {
value
.map(str::trim)
.filter(|value| !value.is_empty())
.map(str::to_string)
.or_else(|| {
names
.iter()
.find_map(|name| env_lookup(name).filter(|value| !value.trim().is_empty()))
})
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -1,33 +0,0 @@
use litellm_core_utils::settings::resolve_non_empty;
use rstest::rstest;
#[rstest]
#[case::explicit_wins(Some(" explicit "), &["FIRST", "SECOND"], Some("explicit"))]
#[case::absent_falls_back(None, &["FIRST", "SECOND"], Some(" first "))]
#[case::blank_falls_back(Some(" \t "), &["BLANK", "SECOND"], Some("second"))]
#[case::skips_missing_and_blank(None, &["MISSING", "BLANK", "SECOND"], Some("second"))]
#[case::environment_order(None, &["SECOND", "FIRST"], Some("second"))]
#[case::missing(None, &["MISSING", "BLANK"], None)]
#[case::no_environment(None, &[], None)]
fn resolves_explicit_value_then_first_nonblank_environment_value(
#[case] value: Option<&str>,
#[case] names: &[&str],
#[case] expected: Option<&str>,
) {
let env = |name: &str| match name {
"FIRST" => Some(" first ".to_string()),
"SECOND" => Some("second".to_string()),
"BLANK" => Some(" \t ".to_string()),
_ => None,
};
assert_eq!(resolve_non_empty(value, &env, names).as_deref(), expected);
}
#[rstest]
fn explicit_value_does_not_read_the_environment() {
let env = |_: &str| panic!("an explicit value must short-circuit environment lookup");
assert_eq!(
resolve_non_empty(Some("key"), &env, &["KEY"]).as_deref(),
Some("key")
);
}

View file

@ -21,6 +21,13 @@ pub fn merge<P: Partial>(highest_precedence_first: impl IntoIterator<Item = P>)
.unwrap_or_default()
}
pub fn non_blank(value: Option<&str>) -> Option<String> {
value
.map(str::trim)
.filter(|value| !value.is_empty())
.map(str::to_owned)
}
pub fn read_env<T>(
env: &(impl Lookup + ?Sized),
names: &'static [&'static str],

View file

@ -1,9 +1,9 @@
use crate::base_llm::messages::context::MessagesModelCapabilities;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_http::request::{
has_header, header_value, header_values, with_header, without_headers,
};
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::{
formats::messages::{
ContentBlock, ContentBlockType, EffortLevel, Message, MessageContent, MessagesTool,
@ -85,16 +85,45 @@ impl<'a> OauthToken<'a> {
}
}
/// Python's `AnthropicModelInfo.get_api_key`: the param, else `ANTHROPIC_API_KEY`.
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct AnthropicKeys {
#[layer(
skip_blank,
litellm_param = "api_key",
env = "ANTHROPIC_API_KEY",
secret,
label = "API Key"
)]
api_key: String,
#[layer(skip_blank, env = "ANTHROPIC_AUTH_TOKEN", secret)]
auth_token: String,
#[layer(skip_blank,
litellm_param = "api_base",
env = ["ANTHROPIC_API_BASE", "ANTHROPIC_BASE_URL"],
label = "API Base"
)]
api_base: String,
}
impl AnthropicKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
/// Python's `AnthropicModelInfo.get_api_key`: the param, else `ANTHROPIC_API_KEY`, stripped.
pub fn get_api_key(
api_key: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Option<String> {
resolve_non_empty(api_key, env_lookup, &[ANTHROPIC_API_KEY_ENV])
non_blank(api_key)
.or_else(|| AnthropicKeysPartial::env(env_lookup).api_key)
.map(|key| key.trim().to_owned())
}
pub fn get_auth_token(env_lookup: &dyn Fn(&str) -> Option<String>) -> Option<String> {
resolve_non_empty(None, env_lookup, &[ANTHROPIC_AUTH_TOKEN_ENV])
AnthropicKeysPartial::env(env_lookup).auth_token
}
/// Python's `AnthropicModelInfo.get_auth_header`, naming the credential instead of building
@ -125,7 +154,7 @@ pub fn resolve_anthropic_api_key(
) -> Result<String, litellm_auth::Error> {
get_api_key(api_key, env_lookup).ok_or(litellm_auth::Error::MissingApiKey {
provider: "Anthropic",
environment_variable: ANTHROPIC_API_KEY_ENV,
environment_variable: AnthropicKeys::API_KEY.env[0],
})
}
@ -138,12 +167,9 @@ pub fn resolve_anthropic_api_base(
api_base: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> String {
resolve_non_empty(
api_base,
env_lookup,
&[ANTHROPIC_API_BASE_ENV, ANTHROPIC_BASE_URL_ENV],
)
.unwrap_or_else(|| API_BASE.to_string())
non_blank(api_base)
.or_else(|| AnthropicKeysPartial::env(env_lookup).api_base)
.unwrap_or_else(|| API_BASE.to_string())
}
pub fn complete_anthropic_url(
@ -1652,6 +1678,15 @@ mod tests {
);
}
#[rstest]
fn an_explicit_key_resolves_without_reading_the_environment() {
let unread = |name: &str| -> Option<String> { panic!("read {name}") };
assert_eq!(
resolve_anthropic_api_key(Some("sk-param"), &unread).ok(),
Some("sk-param".to_owned())
);
}
#[rstest]
#[case::regex_tool(Some(json!([{"type": "tool_search_tool_regex_20251119", "name": "tool_search_tool_regex"}])), true)]
#[case::bm25_tool(Some(json!([{"type": "tool_search_tool_bm25_20251119", "name": "tool_search_tool_bm25"}])), true)]

View file

@ -1,25 +1,54 @@
use crate::Error;
use litellm_core_utils::settings::resolve_non_empty;
use litellm_layered::{Layered, non_blank};
pub const AZURE_API_KEY_ENV: &str = "AZURE_API_KEY";
pub const AZURE_API_BASE_ENV: &str = "AZURE_API_BASE";
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct AzureAiKeys {
#[layer(
skip_blank,
litellm_param = "api_key",
env = "AZURE_API_KEY",
secret,
label = "API Key"
)]
api_key: String,
#[layer(
skip_blank,
litellm_param = "api_base",
env = "AZURE_API_BASE",
label = "API Base"
)]
api_base: String,
}
impl AzureAiKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
pub fn resolve_azure_api_key(
api_key: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
resolve_non_empty(api_key, env_lookup, &[AZURE_API_KEY_ENV]).ok_or_else(|| {
Error::from(litellm_auth::Error::MissingApiKey {
provider: "Azure",
environment_variable: AZURE_API_KEY_ENV,
non_blank(api_key)
.or_else(|| AzureAiKeysPartial::env(env_lookup).api_key)
.ok_or_else(|| {
Error::from(litellm_auth::Error::MissingApiKey {
provider: "Azure",
environment_variable: AzureAiKeys::API_KEY.env[0],
})
})
})
}
pub fn resolve_azure_api_base(
api_base: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
resolve_non_empty(api_base, env_lookup, &[AZURE_API_BASE_ENV])
non_blank(api_base)
.or_else(|| AzureAiKeysPartial::env(env_lookup).api_base)
.ok_or_else(|| Error::from(litellm_auth::Error::MissingApiBase { provider: "Azure", guidance: "Set `api_base` or the AZURE_API_BASE environment variable. Expected format: https://<resource-name>.services.ai.azure.com/anthropic" }))
}

View file

@ -2,9 +2,8 @@ use std::sync::LazyLock;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_auth_aws::{AwsCredentialSource, resolve_aws_region};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_http::request::{header_value, with_header, without_headers};
use litellm_layered::Layered;
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::{BetaProvider, DEFAULT_HEADERS, MESSAGES_PATH},
@ -26,16 +25,15 @@ use crate::{
};
const SERVICE: &str = "aws-external-anthropic";
const BASE_ENVS: &[&str] = &["ANTHROPIC_AWS_BASE_URL", "ANTHROPIC_AWS_API_BASE"];
const WORKSPACE_ENVS: &[&str] = WorkspaceKeys::ENV_NAMES;
const WORKSPACE_FIELDS: &[&str] = WorkspaceKeys::LITELLM_PARAMS;
const BASE_ENVS: &[&str] = ClaudePlatformKeys::API_BASE.env;
const WORKSPACE_ENVS: &[&str] = ClaudePlatformKeys::WORKSPACE_ID.env;
const WORKSPACE_FIELDS: &[&str] = ClaudePlatformKeys::WORKSPACE_ID.litellm_params;
const WORKSPACE_HEADER: &str = "anthropic-workspace-id";
#[derive(Layered)]
#[expect(dead_code, reason = "only WorkspaceKeysPartial is built")]
struct WorkspaceKeys {
#[layer(
skip_blank,
#[expect(dead_code, reason = "only the derived partial is read")]
struct ClaudePlatformKeys {
#[layer(skip_blank,
litellm_param = [
"workspace_id",
"aws_workspace_id",
@ -46,6 +44,26 @@ struct WorkspaceKeys {
label = "workspace_id"
)]
workspace_id: String,
#[layer(
skip_blank,
litellm_param = "api_key",
env = "ANTHROPIC_AWS_API_KEY",
secret
)]
api_key: String,
#[layer(skip_blank,
litellm_param = "api_base",
env = ["ANTHROPIC_AWS_BASE_URL", "ANTHROPIC_AWS_API_BASE"],
)]
api_base: String,
#[layer(skip_blank, env = "AWS_DEFAULT_REGION")]
default_region: String,
}
impl ClaudePlatformKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
const UNSUPPORTED_OVERRIDE: &str = "claude_platform_unsupported_params";
@ -79,14 +97,16 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
.or_else(|| workspace_value(params.extra.get(*field)))
})
});
let workspace = resolve_non_empty(workspace, env, WORKSPACE_ENVS).ok_or_else(|| {
Error::Auth(litellm_auth::Error::missing(
"Claude Platform on AWS",
WorkspaceKeys::WORKSPACE_ID,
))
})?;
let workspace = non_blank(workspace)
.or_else(|| ClaudePlatformKeysPartial::env(env).workspace_id)
.ok_or_else(|| {
Error::Auth(litellm_auth::Error::missing(
"Claude Platform on AWS",
ClaudePlatformKeys::WORKSPACE_ID,
))
})?;
let headers = with_header(headers, WORKSPACE_HEADER, workspace);
let key = resolve_non_empty(api_key, env, &["ANTHROPIC_AWS_API_KEY"]);
let key = non_blank(api_key).or_else(|| ClaudePlatformKeysPartial::env(env).api_key);
let headers = without_headers(headers, &["authorization"]);
if header_value(&headers, "x-api-key").is_some_and(|value| !value.trim().is_empty()) {
return Ok(ValidatedEnvironment {
@ -103,7 +123,8 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
},
});
}
let base = resolve_non_empty(params.api_base.as_deref(), env, BASE_ENVS);
let base = non_blank(params.api_base.as_deref())
.or_else(|| ClaudePlatformKeysPartial::env(env).api_base);
Ok(ValidatedEnvironment {
headers: without_headers(headers, &["x-api-key"]),
auth: AuthScheme::AwsSigV4 {
@ -122,13 +143,12 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
_stream: bool,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
let base = resolve_non_empty(
let base = non_blank(
api_base
.filter(|base| !base.trim().is_empty())
.or(params.api_base.as_deref()),
env,
BASE_ENVS,
);
)
.or_else(|| ClaudePlatformKeysPartial::env(env).api_base);
let base = match base {
Some(base) => base,
None => format!(
@ -209,7 +229,7 @@ fn platform_region(
});
let region = host_region
.or_else(|| resolve_aws_region(None, &params.aws, env))
.or_else(|| resolve_non_empty(None, env, &["AWS_DEFAULT_REGION"]))
.or_else(|| ClaudePlatformKeysPartial::env(env).default_region)
.ok_or_else(|| {
Error::Auth(litellm_auth::Error::missing(
"Claude Platform on AWS",
@ -286,7 +306,7 @@ mod tests {
.unwrap_err(),
Error::Auth(litellm_auth::Error::missing(
"Claude Platform on AWS",
WorkspaceKeys::WORKSPACE_ID,
ClaudePlatformKeys::WORKSPACE_ID,
))
);
}

View file

@ -3,8 +3,8 @@ use std::sync::LazyLock;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_auth_aws::AwsCredentialSource;
use litellm_auth_aws::bedrock_model_id_and_region;
use litellm_core_utils::settings::resolve_non_empty;
use litellm_http::request::{with_header, without_headers};
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::{AnthropicBeta, BetaProvider, BetaSet, DEFAULT_HEADERS},
@ -31,14 +31,37 @@ use crate::{
bedrock::common_utils::validated_region,
};
const API_BASE_ENV: &str = "BEDROCK_MANTLE_API_BASE";
const REGION_ENVS: &[&str] = &[
"BEDROCK_MANTLE_REGION",
"AWS_REGION_NAME",
"AWS_REGION",
"AWS_DEFAULT_REGION",
];
const KEY_ENVS: &[&str] = &["BEDROCK_MANTLE_API_KEY", "AWS_BEARER_TOKEN_BEDROCK"];
const API_BASE_ENV: &str = MantleKeys::API_BASE.env[0];
const REGION_ENVS: &[&str] = MantleKeys::REGION.env;
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct MantleKeys {
#[layer(skip_blank,
litellm_param = "api_key",
env = ["BEDROCK_MANTLE_API_KEY", "AWS_BEARER_TOKEN_BEDROCK"],
secret,
label = "API Key"
)]
api_key: String,
#[layer(
skip_blank,
litellm_param = "api_base",
env = "BEDROCK_MANTLE_API_BASE",
label = "API Base"
)]
api_base: String,
#[layer(skip_blank,
env = ["BEDROCK_MANTLE_REGION", "AWS_REGION_NAME", "AWS_REGION", "AWS_DEFAULT_REGION"],
)]
region: String,
}
impl MantleKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
const MESSAGES_PATH: &str = "/anthropic/v1/messages";
const BASE_SUFFIXES: &[&str] = &[
MESSAGES_PATH,
@ -78,7 +101,7 @@ impl BaseMessagesConfig for BedrockMantleAnthropicMessagesConfig {
Some(project) => with_header(headers, "anthropic-workspace-id", project.to_string()),
None => headers,
};
let auth = match resolve_non_empty(api_key, env, KEY_ENVS) {
let auth = match non_blank(api_key).or_else(|| MantleKeysPartial::env(env).api_key) {
Some(token) => AuthScheme::Credential {
placement: CredentialPlacement::Bearer,
secret: SecretValue::new(token),
@ -242,7 +265,7 @@ fn mantle_base(
.into_iter()
.flatten()
.find(|base| !base.trim().is_empty());
resolve_non_empty(explicit, env, &[API_BASE_ENV])
non_blank(explicit).or_else(|| MantleKeysPartial::env(env).api_base)
}
fn mantle_region(
@ -253,9 +276,9 @@ fn mantle_region(
) -> Result<String, Error> {
let region = mantle_base(base, params, env)
.and_then(|base| mantle_host_region(&base))
.or_else(|| resolve_non_empty(params.aws.aws_region_name.as_deref(), &|_| None, &[]))
.or_else(|| non_blank(params.aws.aws_region_name.as_deref()))
.or_else(|| mantle_model(model).1)
.or_else(|| resolve_non_empty(None, env, REGION_ENVS))
.or_else(|| MantleKeysPartial::env(env).region)
.unwrap_or_else(|| "us-east-1".into());
validated_region(region)
}

View file

@ -1,5 +1,5 @@
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest, MessagesTool},
providers::anthropic::{DEFAULT_HEADERS, MESSAGES_PATH},
@ -138,20 +138,42 @@ impl BaseMessagesConfig for DeepSeekAnthropicMessagesConfig {
}
}
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct DeepSeekKeys {
#[layer(
skip_blank,
litellm_param = "api_key",
env = "DEEPSEEK_API_KEY",
secret,
label = "API Key"
)]
api_key: String,
#[layer(skip_blank,
litellm_param = "api_base",
env = ["DEEPSEEK_ANTHROPIC_API_BASE", "DEEPSEEK_API_BASE"],
label = "API Base"
)]
api_base: String,
}
impl DeepSeekKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
pub fn get_api_key(
api_key: Option<&str>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Option<String> {
resolve_non_empty(api_key, env_lookup, &[DEEPSEEK_API_KEY_ENV])
non_blank(api_key).or_else(|| DeepSeekKeysPartial::env(env_lookup).api_key)
}
pub fn get_api_base(api_base: Option<&str>, env_lookup: &dyn Fn(&str) -> Option<String>) -> String {
resolve_non_empty(
api_base,
env_lookup,
&[DEEPSEEK_ANTHROPIC_API_BASE_ENV, DEEPSEEK_API_BASE_ENV],
)
.unwrap_or_else(|| DEFAULT_DEEPSEEK_ANTHROPIC_API_BASE.to_string())
non_blank(api_base)
.or_else(|| DeepSeekKeysPartial::env(env_lookup).api_base)
.unwrap_or_else(|| DEFAULT_DEEPSEEK_ANTHROPIC_API_BASE.to_string())
}
/// Python's `get_complete_url`: a base already ending in the Anthropic messages path is kept,

View file

@ -1,5 +1,5 @@
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::formats::messages::{
MessagesOptionalParams, MessagesRequest, MessagesResponse,
};
@ -27,6 +27,32 @@ const API_KEY_ENV: &str = "EDENAI_API_KEY";
const API_BASE_ENV: &str = "EDENAI_API_BASE";
const DEFAULT_BASE: &str = "https://api.edenai.run/v3";
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct EdenAiKeys {
#[layer(
skip_blank,
litellm_param = "api_key",
env = "EDENAI_API_KEY",
secret,
label = "API Key"
)]
api_key: String,
#[layer(
skip_blank,
litellm_param = "api_base",
env = "EDENAI_API_BASE",
label = "API Base"
)]
api_base: String,
}
impl EdenAiKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
pub struct EdenAIAnthropicMessagesConfig;
pub const EDENAI_MESSAGES_CONFIG: EdenAIAnthropicMessagesConfig = EdenAIAnthropicMessagesConfig;
@ -40,12 +66,12 @@ impl BaseMessagesConfig for EdenAIAnthropicMessagesConfig {
_params: &LitellmParams,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
litellm_auth::Error::MissingApiKey {
let key = non_blank(api_key)
.or_else(|| EdenAiKeysPartial::env(env).api_key)
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
provider: "Eden AI",
environment_variable: API_KEY_ENV,
},
))?;
environment_variable: EdenAiKeys::API_KEY.env[0],
}))?;
if has_anthropic_credential(&headers) {
return Ok(ValidatedEnvironment {
headers,
@ -69,7 +95,8 @@ impl BaseMessagesConfig for EdenAIAnthropicMessagesConfig {
_stream: bool,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
let base = resolve_non_empty(api_base, env, &[API_BASE_ENV])
let base = non_blank(api_base)
.or_else(|| EdenAiKeysPartial::env(env).api_base)
.unwrap_or_else(|| DEFAULT_BASE.into());
Ok(complete_messages_url(&base))
}

View file

@ -1,5 +1,5 @@
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::{DEFAULT_HEADERS, MESSAGES_PATH},
@ -25,6 +25,32 @@ const API_KEY_ENV: &str = "MINIMAX_API_KEY";
const API_BASE_ENV: &str = "MINIMAX_API_BASE";
const DEFAULT_BASE: &str = "https://api.minimax.io/anthropic";
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct MinimaxKeys {
#[layer(
skip_blank,
litellm_param = "api_key",
env = "MINIMAX_API_KEY",
secret,
label = "API Key"
)]
api_key: String,
#[layer(
skip_blank,
litellm_param = "api_base",
env = "MINIMAX_API_BASE",
label = "API Base"
)]
api_base: String,
}
impl MinimaxKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
pub struct MinimaxMessagesConfig;
pub const MINIMAX_MESSAGES_CONFIG: MinimaxMessagesConfig = MinimaxMessagesConfig;
@ -52,12 +78,12 @@ impl BaseMessagesConfig for MinimaxMessagesConfig {
auth: AuthScheme::Forwarded,
});
}
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
litellm_auth::Error::MissingApiKey {
let key = non_blank(api_key)
.or_else(|| MinimaxKeysPartial::env(env).api_key)
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
provider: "MiniMax",
environment_variable: API_KEY_ENV,
},
))?;
environment_variable: MinimaxKeys::API_KEY.env[0],
}))?;
Ok(ValidatedEnvironment {
headers,
auth: AuthScheme::Credential {
@ -75,7 +101,8 @@ impl BaseMessagesConfig for MinimaxMessagesConfig {
_stream: bool,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
let base = resolve_non_empty(api_base, env, &[API_BASE_ENV])
let base = non_blank(api_base)
.or_else(|| MinimaxKeysPartial::env(env).api_base)
.unwrap_or_else(|| DEFAULT_BASE.into());
let base = base.trim_end_matches('/');
if base.ends_with(MESSAGES_PATH) {

View file

@ -1,6 +1,6 @@
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_http::request::{has_bearer_auth, without_headers};
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::DEFAULT_HEADERS,
@ -22,6 +22,32 @@ use crate::{
const API_KEY_ENV: &str = "OPENROUTER_API_KEY";
const API_BASE_ENV: &str = "OPENROUTER_API_BASE";
const DEFAULT_BASE: &str = "https://openrouter.ai/api/v1";
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct OpenRouterKeys {
#[layer(
skip_blank,
litellm_param = "api_key",
env = "OPENROUTER_API_KEY",
secret,
label = "API Key"
)]
api_key: String,
#[layer(
skip_blank,
litellm_param = "api_base",
env = "OPENROUTER_API_BASE",
label = "API Base"
)]
api_base: String,
}
impl OpenRouterKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
const MESSAGES_PATH: &str = "/v1/messages";
pub struct OpenRouterAnthropicMessagesConfig;
@ -45,12 +71,12 @@ impl BaseMessagesConfig for OpenRouterAnthropicMessagesConfig {
auth: AuthScheme::Forwarded,
});
}
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
litellm_auth::Error::MissingApiKey {
let key = non_blank(api_key)
.or_else(|| OpenRouterKeysPartial::env(env).api_key)
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
provider: "OpenRouter",
environment_variable: API_KEY_ENV,
},
))?;
environment_variable: OpenRouterKeys::API_KEY.env[0],
}))?;
Ok(ValidatedEnvironment {
headers: without_headers(headers, &["authorization"]),
auth: AuthScheme::Credential {
@ -71,7 +97,8 @@ impl BaseMessagesConfig for OpenRouterAnthropicMessagesConfig {
let explicit = api_base
.filter(|base| !base.trim().is_empty())
.or(params.api_base.as_deref());
let base = resolve_non_empty(explicit, env, &[API_BASE_ENV])
let base = non_blank(explicit)
.or_else(|| OpenRouterKeysPartial::env(env).api_base)
.unwrap_or_else(|| DEFAULT_BASE.into());
let trimmed = base.trim_end_matches('/');
if trimmed.ends_with(MESSAGES_PATH) {

View file

@ -1,5 +1,5 @@
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::formats::responses::ResponsesApiResponse;
use serde_json::{Map, Value};
@ -15,6 +15,31 @@ use crate::{
const API_KEY_ENVS: [&str; 2] = ["OPENROUTER_API_KEY", "OR_API_KEY"];
const API_BASE_ENV: &str = "OPENROUTER_API_BASE";
const DEFAULT_BASE: &str = "https://openrouter.ai/api/v1";
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct OpenRouterKeys {
#[layer(skip_blank,
litellm_param = "api_key",
env = ["OPENROUTER_API_KEY", "OR_API_KEY"],
secret,
label = "API Key"
)]
api_key: String,
#[layer(
skip_blank,
litellm_param = "api_base",
env = "OPENROUTER_API_BASE",
label = "API Base"
)]
api_base: String,
}
impl OpenRouterKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
const RESPONSES_PATH: &str = "/responses";
pub struct OpenRouterResponsesApiConfig;
@ -43,12 +68,12 @@ impl BaseResponsesApiConfig for OpenRouterResponsesApiConfig {
api_key: Option<&str>,
lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
let key = resolve_non_empty(api_key, lookup, &API_KEY_ENVS).ok_or(
litellm_auth::Error::MissingApiKey {
let key = non_blank(api_key)
.or_else(|| OpenRouterKeysPartial::env(lookup).api_key)
.ok_or(litellm_auth::Error::MissingApiKey {
provider: "OpenRouter",
environment_variable: API_KEY_ENVS[0],
},
)?;
environment_variable: OpenRouterKeys::API_KEY.env[0],
})?;
Ok(ValidatedEnvironment {
headers: litellm_http::request::with_default_headers(
headers,
@ -66,7 +91,8 @@ impl BaseResponsesApiConfig for OpenRouterResponsesApiConfig {
api_base: Option<&str>,
lookup: &dyn Fn(&str) -> Option<String>,
) -> String {
let base = resolve_non_empty(api_base, lookup, &[API_BASE_ENV])
let base = non_blank(api_base)
.or_else(|| OpenRouterKeysPartial::env(lookup).api_base)
.unwrap_or_else(|| DEFAULT_BASE.into());
format!("{}{RESPONSES_PATH}", base.trim_end_matches('/'))
}

View file

@ -1,5 +1,5 @@
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_layered::{Layered, non_blank};
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::{DEFAULT_HEADERS, MESSAGES_PATH},
@ -26,6 +26,27 @@ const API_BASE_ENV: &str = "TENCENT_API_BASE";
const ANTHROPIC_API_BASE_ENV: &str = "TENCENT_ANTHROPIC_API_BASE";
const DEFAULT_BASE: &str = "https://tokenhub-intl.tencentcloudmaas.com";
#[derive(Layered)]
#[expect(dead_code, reason = "only the derived partial is read")]
struct TencentKeys {
#[layer(
skip_blank,
litellm_param = "api_key",
env = "TENCENT_API_KEY",
secret,
label = "API Key"
)]
api_key: String,
#[layer(skip_blank, litellm_param = "api_base", env = ["TENCENT_ANTHROPIC_API_BASE", "TENCENT_API_BASE"], label = "API Base")]
api_base: String,
}
impl TencentKeysPartial {
fn env(env: &dyn Fn(&str) -> Option<String>) -> Self {
Self::from_env(&env).unwrap_or_default()
}
}
pub struct TencentAnthropicMessagesConfig;
pub const TENCENT_MESSAGES_CONFIG: TencentAnthropicMessagesConfig = TencentAnthropicMessagesConfig;
@ -53,12 +74,12 @@ impl BaseMessagesConfig for TencentAnthropicMessagesConfig {
auth: AuthScheme::Forwarded,
});
}
let key = resolve_non_empty(api_key, env, &[API_KEY_ENV]).ok_or(Error::Auth(
litellm_auth::Error::MissingApiKey {
let key = non_blank(api_key)
.or_else(|| TencentKeysPartial::env(env).api_key)
.ok_or(Error::Auth(litellm_auth::Error::MissingApiKey {
provider: "Tencent",
environment_variable: API_KEY_ENV,
},
))?;
environment_variable: TencentKeys::API_KEY.env[0],
}))?;
Ok(ValidatedEnvironment {
headers,
auth: AuthScheme::Credential {
@ -76,7 +97,8 @@ impl BaseMessagesConfig for TencentAnthropicMessagesConfig {
_stream: bool,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<String, Error> {
let base = resolve_non_empty(api_base, env, &[ANTHROPIC_API_BASE_ENV, API_BASE_ENV])
let base = non_blank(api_base)
.or_else(|| TencentKeysPartial::env(env).api_base)
.unwrap_or_else(|| DEFAULT_BASE.into());
let base = base.trim_end_matches('/');
if base.ends_with(MESSAGES_PATH) {