This commit is contained in:
yujonglee 2026-10-26 12:12:03 -04:00 • committed by GitHub
commit fd41b626b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
89 changed files with 1091 additions and 926 deletions

View file

@ -3356,6 +3356,7 @@ version = "0.0.1"
name = "litellm-auth"
version = "0.1.0"
dependencies = [
"http 1.4.2",
"litellm-auth-aws",
"litellm-auth-azure",
"litellm-auth-gcp",
@ -3429,6 +3430,7 @@ dependencies = [
name = "litellm-auth-types"
version = "0.1.0"
dependencies = [
"http 1.4.2",
"litellm-error",
"rstest",
"serde",
@ -3648,6 +3650,7 @@ dependencies = [
name = "litellm-callbacks-legacy-python"
version = "0.1.0"
dependencies = [
"http 1.4.2",
"litellm-auth",
"litellm-error",
"litellm-host",
@ -3929,6 +3932,7 @@ name = "litellm-host"
version = "0.1.0"
dependencies = [
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-coroutine",
"rstest",
@ -3958,6 +3962,7 @@ name = "litellm-host-native"
version = "0.1.0"
dependencies = [
"futures-util",
"http 1.4.2",
"litellm-host",
"rstest",
"serde_json",
@ -3970,6 +3975,7 @@ version = "0.1.0"
dependencies = [
"bytes",
"futures-util",
"http 1.4.2",
"litellm-coroutine",
"litellm-error",
"litellm-host",
@ -4017,6 +4023,7 @@ version = "0.1.0"
dependencies = [
"bytes",
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-cache",
"litellm-cache-memory",
@ -4046,6 +4053,7 @@ name = "litellm-inference-chat"
version = "0.1.0"
dependencies = [
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-cache-memory",
"litellm-cache-response",
@ -4073,6 +4081,7 @@ version = "0.1.0"
dependencies = [
"bytes",
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-auth-aws",
"litellm-cache-memory",
@ -4106,6 +4115,7 @@ dependencies = [
"base64 0.22.1",
"bytes",
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-auth-aws",
"litellm-auth-gcp",
@ -4139,6 +4149,7 @@ dependencies = [
"base64 0.22.1",
"bytes",
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-cache",
"litellm-cache-memory",
@ -4180,6 +4191,7 @@ name = "litellm-inference-transcription"
version = "0.1.0"
dependencies = [
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-core-utils",
"litellm-error",
@ -4206,6 +4218,7 @@ dependencies = [
"bytes",
"data-url",
"futures-util",
"http 1.4.2",
"litellm-auth",
"litellm-auth-aws",
"litellm-auth-azure",

View file

@ -89,7 +89,10 @@ mod tests {
use serde_json::{Value, json as json_value};
use std::time::{Duration, UNIX_EPOCH};
use litellm_http::outbound::{json, signed};
use litellm_http::{
outbound::{json, signed},
request::header_map,
};
use super::*;
@ -108,17 +111,14 @@ mod tests {
.with_clock(fixed_clock)
}
fn target() -> (String, String) {
("X-Amz-Target".into(), "Textract.DetectDocumentText".into())
fn target() -> HeaderMap {
header_map(&[("X-Amz-Target", "Textract.DetectDocumentText")])
}
fn authorization(body: &Value, service: &'static str) -> String {
signed(
json(URL, &[target()], body, None).unwrap(),
&signer(service),
)
.unwrap()
.headers()["authorization"]
signed(json(URL, target(), body, None).unwrap(), &signer(service))
.unwrap()
.headers()["authorization"]
.to_str()
.unwrap()
.to_string()
@ -129,7 +129,7 @@ mod tests {
let sent = signed(
json(
URL,
&[target()],
target(),
&json_value!({"Document": {"Bytes": "aGk="}}),
None,
)
@ -180,7 +180,7 @@ mod tests {
let error = signed(
json(
URL,
&[("authorization".into(), "Bearer caller".into())],
header_map(&[("authorization", "Bearer caller")]),
&json_value!({}),
None,
)
@ -198,7 +198,7 @@ mod tests {
let error = signed(
json(
URL,
&[("x-amz-meta-note".into(), "café".into())],
header_map(&[("x-amz-meta-note", "café")]),
&json_value!({}),
None,
)

View file

@ -6,7 +6,7 @@ license.workspace = true
repository.workspace = true
[features]
google-sdk = ["dep:google-cloud-auth", "dep:http"]
google-sdk = ["dep:google-cloud-auth"]
[dependencies]
litellm-auth-types.workspace = true
@ -21,7 +21,7 @@ veil.workspace = true
gcp_auth = "0.12.7"
google-cloud-auth = { workspace = true, optional = true }
http = { workspace = true, optional = true }
http.workspace = true
[dev-dependencies]
litellm-error.workspace = true

View file

@ -1,6 +1,7 @@
use std::{future::Future, pin::Pin, sync::Arc};
use gcp_auth::{CustomServiceAccount, TokenProvider};
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth_types::{CredentialPlacement, Error, ErrorDetail, http::apply_credential};
use moka::future::Cache;
use serde_json::Value;
@ -18,7 +19,7 @@ use crate::{
};
pub struct VertexEnvironment {
pub headers: Vec<(String, String)>,
pub headers: HeaderMap,
pub project_id: String,
}
@ -57,14 +58,12 @@ impl VertexAuth {
pub async fn validate_environment(
&self,
headers: Vec<(String, String)>,
headers: HeaderMap,
api_key: Option<&str>,
config: &VertexConfig,
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
) -> Result<VertexEnvironment, Error> {
let has_authorization = headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case("Authorization"));
let has_authorization = headers.contains_key(AUTHORIZATION);
let static_token = api_key
.map(str::trim)
.filter(|value| !value.is_empty())

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use google_cloud_auth::credentials::{CacheableResource, CredentialsProvider, EntityTag};
use google_cloud_auth::errors::CredentialsError;
use http::{Extensions, HeaderMap, HeaderName, HeaderValue};
use http::{Extensions, HeaderMap};
use litellm_auth_types::Error;
use crate::{VertexAuth, VertexConfig};
@ -25,22 +25,13 @@ impl GoogleCredentials {
}
pub async fn request_headers(&self) -> Result<HeaderMap, Error> {
let response = self
Ok(self
.auth
.validate_environment(Vec::new(), None, &self.config, &|name| {
.validate_environment(HeaderMap::new(), None, &self.config, &|name| {
(self.environment)(name)
})
.await?;
response
.headers
.into_iter()
.map(|(key, value)| {
let name =
HeaderName::from_bytes(key.as_bytes()).map_err(|_| Error::InvalidHeader)?;
let value = HeaderValue::from_str(&value).map_err(|_| Error::InvalidHeader)?;
Ok((name, value))
})
.collect()
.await?
.headers)
}
}

View file

@ -65,7 +65,7 @@ async fn secret_names_cover_environment_reads() {
None
};
let auth = auth(Arc::new(AtomicUsize::new(0)), Arc::new(AtomicUsize::new(0)));
auth.validate_environment(Vec::new(), None, &VertexConfig::default(), &env)
auth.validate_environment(http::HeaderMap::new(), None, &VertexConfig::default(), &env)
.await
.unwrap();
get_vertex_ai_location(&VertexConfig::default(), &env);
@ -84,20 +84,28 @@ async fn explicit_token_and_header_do_not_acquire_adc() {
let auth = auth(Arc::new(AtomicUsize::new(0)), loads.clone());
let configured = config(json!({"vertex_project":"project-1"}));
let explicit = auth
.validate_environment(Vec::new(), Some("access-token"), &configured, &|_| None)
.validate_environment(
http::HeaderMap::new(),
Some("access-token"),
&configured,
&|_| None,
)
.await
.unwrap();
assert_eq!(explicit.headers[0].1, "Bearer access-token");
assert_eq!(explicit.headers["authorization"], "Bearer access-token");
let existing = auth
.validate_environment(
vec![("authorization".into(), "Bearer existing".into())],
http::HeaderMap::from_iter([(
http::header::AUTHORIZATION,
http::HeaderValue::from_static("Bearer existing"),
)]),
None,
&configured,
&|_| None,
)
.await
.unwrap();
assert_eq!(existing.headers[0].1, "Bearer existing");
assert_eq!(existing.headers["authorization"], "Bearer existing");
assert_eq!(loads.load(Ordering::SeqCst), 0);
}
@ -109,11 +117,16 @@ async fn provider_is_reused_across_authentication_calls() {
let auth = auth(calls.clone(), loads.clone());
for _ in 0..2 {
let environment = auth
.validate_environment(Vec::new(), None, &VertexConfig::default(), &|_| None)
.validate_environment(
http::HeaderMap::new(),
None,
&VertexConfig::default(),
&|_| None,
)
.await
.unwrap();
assert_eq!(environment.project_id, "adc-project");
assert_eq!(environment.headers[0].1, "Bearer adc-token");
assert_eq!(environment.headers["authorization"], "Bearer adc-token");
}
assert_eq!(loads.load(Ordering::SeqCst), 1);
assert_eq!(calls.load(Ordering::SeqCst), 4);

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
litellm-error.workspace = true
serde.workspace = true
serde_json.workspace = true

View file

@ -1,3 +1,5 @@
use http::{HeaderMap, HeaderName, HeaderValue};
use crate::Error;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@ -9,63 +11,66 @@ pub enum CredentialPlacement {
impl CredentialPlacement {
pub const fn header_name(self) -> &'static str {
match self {
Self::Bearer => "Authorization",
Self::Bearer => "authorization",
Self::Header(name) => name,
}
}
pub fn header(self, credential: &str) -> Result<(HeaderName, HeaderValue), Error> {
let name = HeaderName::try_from(self.header_name()).map_err(|_| Error::InvalidHeader)?;
let value = match self {
Self::Bearer => HeaderValue::try_from(format!("Bearer {credential}")),
Self::Header(_) => HeaderValue::try_from(credential),
};
let mut value = value.map_err(|_| Error::InvalidHeader)?;
value.set_sensitive(true);
Ok((name, value))
}
}
pub fn apply_credential(
headers: Vec<(String, String)>,
mut headers: HeaderMap,
credential: &str,
placement: CredentialPlacement,
) -> Result<Vec<(String, String)>, Error> {
) -> Result<HeaderMap, Error> {
if credential.trim().is_empty() {
return Err(Error::InvalidConfiguration(crate::ErrorDetail::Empty {
subject: "credential",
}));
}
if headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case(placement.header_name()))
{
let (name, value) = placement.header(credential)?;
if headers.contains_key(&name) {
return Err(Error::InvalidConfiguration(
crate::ErrorDetail::DuplicateHeader(placement.header_name()),
));
}
let value = match placement {
CredentialPlacement::Bearer => format!("Bearer {credential}"),
CredentialPlacement::Header(_) => credential.to_string(),
};
Ok(
std::iter::once((placement.header_name().to_string(), value))
.chain(headers)
.collect(),
)
headers.insert(name, value);
Ok(headers)
}
#[cfg(test)]
mod tests {
use super::{CredentialPlacement, apply_credential};
use rstest::rstest;
use super::*;
#[test]
fn bearer_uses_authorization_header() {
let headers = apply_credential(Vec::new(), "key", CredentialPlacement::Bearer)
let headers = apply_credential(HeaderMap::new(), "key", CredentialPlacement::Bearer)
.expect("credential applies");
assert_eq!(
headers,
vec![("Authorization".to_string(), "Bearer key".to_string())]
);
assert_eq!(headers.len(), 1);
assert_eq!(headers["Authorization"], "Bearer key");
assert!(headers["Authorization"].is_sensitive());
}
#[test]
fn named_header_rejects_existing_value() {
fn named_header_rejects_existing_value_in_any_case() {
let error = apply_credential(
vec![(
"ocp-apim-subscription-key".to_string(),
"caller-key".to_string(),
)],
HeaderMap::from_iter([(
HeaderName::from_static("ocp-apim-subscription-key"),
HeaderValue::from_static("caller-key"),
)]),
"configured-key",
CredentialPlacement::Header("Ocp-Apim-Subscription-Key"),
)
@ -77,4 +82,17 @@ mod tests {
.contains("already exists")
);
}
#[rstest]
#[case::newline_in_credential(CredentialPlacement::Bearer, "key\r\nx-injected: 1")]
#[case::invalid_header_name(CredentialPlacement::Header("x api key"), "key")]
fn a_credential_that_cannot_be_a_header_is_rejected(
#[case] placement: CredentialPlacement,
#[case] credential: &str,
) {
assert_eq!(
apply_credential(HeaderMap::new(), credential, placement),
Err(Error::InvalidHeader)
);
}
}

View file

@ -1,3 +1,5 @@
use http::HeaderMap;
use crate::http::apply_credential;
use crate::{CredentialPlacement, Error, ErrorDetail, ResolvedCredential};
@ -30,27 +32,23 @@ pub struct ProviderAuthPolicy {
}
impl ProviderAuthPolicy {
pub fn has_existing_credential(&self, headers: &[(String, String)]) -> bool {
pub fn has_existing_credential(&self, headers: &HeaderMap) -> bool {
self.existing_credential_header(headers).is_some()
}
fn existing_credential_header(&self, headers: &[(String, String)]) -> Option<&'static str> {
fn existing_credential_header(&self, headers: &HeaderMap) -> Option<&'static str> {
self.accepted_existing_headers
.iter()
.copied()
.find(|accepted| {
headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case(accepted))
})
.find(|accepted| headers.contains_key(*accepted))
}
pub fn apply(
&self,
headers: Vec<(String, String)>,
headers: HeaderMap,
kind: CredentialPlanKind,
credential: &ResolvedCredential,
) -> Result<Vec<(String, String)>, Error> {
) -> Result<HeaderMap, Error> {
if let Some(existing) = self.existing_credential_header(&headers) {
return match self.existing_header_behavior {
ExistingHeaderBehavior::Preserve => Ok(headers),
@ -89,23 +87,21 @@ mod tests {
fn rules_define_allowed_plans_and_credential_placement() {
let headers = POLICY
.apply(
Vec::new(),
http::HeaderMap::new(),
CredentialPlanKind::Static,
&ResolvedCredential::Static(SecretValue::new("secret")),
)
.unwrap();
assert_eq!(
headers,
vec![("x-api-key".to_string(), "secret".to_string())]
);
assert_eq!(headers.len(), 1);
assert_eq!(headers["x-api-key"], "secret");
}
#[test]
fn unsupported_plan_is_rejected() {
let error = POLICY
.apply(
Vec::new(),
http::HeaderMap::new(),
CredentialPlanKind::Entra,
&ResolvedCredential::Static(SecretValue::new("secret")),
)

View file

@ -16,3 +16,6 @@ litellm-auth-types.workspace = true
litellm-auth-aws = { workspace = true, optional = true }
litellm-auth-azure = { workspace = true, optional = true }
litellm-auth-gcp = { workspace = true, optional = true }
[dev-dependencies]
http.workspace = true

View file

@ -20,14 +20,12 @@ fn facade_applies_shared_auth_policy() {
let headers = policy
.apply(
Vec::new(),
http::HeaderMap::new(),
CredentialPlanKind::Static,
&ResolvedCredential::Static(SecretValue::new("secret")),
)
.expect("facade policy applies");
assert_eq!(
headers,
vec![("x-api-key".to_string(), "secret".to_string())]
);
assert_eq!(headers.len(), 1);
assert_eq!(headers["x-api-key"], "secret");
}

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
litellm-error.workspace = true
litellm-host.workspace = true
litellm-host-python.workspace = true

View file

@ -5,6 +5,7 @@
use crate::LoggingOperation;
use litellm_host_python::PythonOwned;
use http::{HeaderMap, HeaderName, HeaderValue};
use litellm_host::{
interceptors::{RawResponse, RequestContext, WireRequest},
lifecycle::{FailureOrigin, Timing, epoch_seconds},
@ -21,6 +22,7 @@ use serde_json::Value;
use crate::{
DeploymentHooks, LegacyCallbacks, PublicCall, PythonLogger,
deferred::{PendingLogging, PendingSuccess},
error::Error,
finalize, is_internal_call,
python::Streaming,
setup,
@ -67,6 +69,31 @@ fn is_cancellation(py: Python<'_>, error: &PyErr) -> bool {
!error.is_instance_of::<PyException>(py)
}
fn header_dict<'py>(py: Python<'py>, headers: &HeaderMap) -> PyResult<Bound<'py, PyDict>> {
let dict = PyDict::new(py);
for (name, value) in headers {
dict.set_item(name.as_str(), String::from_utf8_lossy(value.as_bytes()))?;
}
Ok(dict)
}
fn dict_headers(headers: &Bound<'_, PyDict>) -> PyResult<HeaderMap> {
headers
.iter()
.map(|(name, value)| {
let name = name.extract::<String>()?;
let name = match HeaderName::try_from(name.as_str()) {
Ok(parsed) => parsed,
Err(source) => return Err(Error::CallbackHeaderName { name, source }.into()),
};
match HeaderValue::try_from(value.extract::<String>()?) {
Ok(value) => Ok((name, value)),
Err(source) => Err(Error::CallbackHeaderValue { name, source }.into()),
}
})
.collect()
}
impl LegacyLogging {
pub fn new(
py: Python<'_>,
@ -388,10 +415,7 @@ impl LegacyLogging {
body.set_item(name, value)?;
}
}
let headers = PyDict::new(py);
for (name, value) in &wire.headers {
headers.set_item(name, value)?;
}
let headers = header_dict(py, &wire.headers)?;
self.request = Some(LoggedRequest {
body: body.clone().unbind(),
headers: headers.clone().unbind(),
@ -405,13 +429,9 @@ impl LegacyLogging {
&headers,
&wire.url,
)?;
let headers = headers
.iter()
.map(|(name, value)| Ok((name.extract::<String>()?, value.extract::<String>()?)))
.collect::<PyResult<Vec<_>>>()?;
Ok(HookStep::Ready(Box::new(WireRequest {
body: from_py(&body)?,
headers,
headers: dict_headers(&headers)?,
..*wire
})))
}
@ -1091,7 +1111,7 @@ assert logger.update['custom_llm_provider'] == 'edenai', logger.update
};
let wire = WireRequest {
url: "https://provider.invalid/ocr".into(),
headers: vec![("x-route".into(), "route".into())],
headers: route_headers(),
body,
};
let (_, step) = send_and_receive(py, &mut logging, wire, &context);
@ -1147,11 +1167,27 @@ assert logger.update['custom_llm_provider'] == 'edenai', logger.update
fn route_wire() -> WireRequest {
WireRequest {
url: "https://provider.invalid/ocr".into(),
headers: vec![("x-route".into(), "route".into())],
headers: route_headers(),
body: json!({}),
}
}
fn headers(pairs: &[(&str, &str)]) -> http::HeaderMap {
pairs
.iter()
.map(|&(name, value)| {
(
http::HeaderName::try_from(name).unwrap(),
http::HeaderValue::try_from(value).unwrap(),
)
})
.collect()
}
fn route_headers() -> http::HeaderMap {
headers(&[("x-route", "route")])
}
/// A Python object owning one `LegacyLogging`, so the interpreter's collector sees the
/// edges the adapter reports and clears them the way the driver's `Execution` does.
#[pyclass(weakref)]
@ -1394,7 +1430,7 @@ def on_pre_call(args):
let body = json!({"document": document(DOCUMENT)});
let wire = before_provider_request(script, body.clone());
assert_eq!(wire.body, body);
assert_eq!(wire.headers, [("x-route".to_string(), "route".to_string())]);
assert_eq!(wire.headers, route_headers());
}
#[rstest::rstest]
@ -1408,13 +1444,42 @@ def on_pre_call(args):
);
assert_eq!(
wire.headers,
[
("x-route".to_string(), "route".to_string()),
("x-callback".to_string(), "edited".to_string()),
]
headers(&[("x-route", "route"), ("x-callback", "edited")])
);
}
#[rstest]
#[case::name(
c"
def on_pre_call(args):
args['headers']['x bad'] = 'value'
"
)]
#[case::value(
c"
def on_pre_call(args):
args['headers']['x-callback'] = 'a\\r\\nx-injected: 1'
"
)]
fn a_pre_call_header_edit_that_cannot_go_on_the_wire_is_a_value_error(#[case] script: &CStr) {
Python::initialize();
Python::attach(|py| {
let locals = namespace(py, PAYLOAD_LOGGER);
run(py, &locals, script);
let mut logging = LegacyLogging {
logger: Some(PythonLogger::new(local(&locals, "logger").unbind())),
..legacy_call(py, &locals, false)
};
let Err(error) =
logging.before_provider_request(py, Box::new(route_wire()), &route_context())
else {
panic!("an invalid header edit reached the wire");
};
assert!(error.is_instance_of::<pyo3::exceptions::PyValueError>(py));
assert!(!error.to_string().contains("x-injected"), "{error}");
});
}
#[rstest::rstest]
fn pre_call_receives_the_wire_request_and_the_logger_its_redacted_request() {
let body = json!({"model": "model", "document": document(DOCUMENT)});
@ -1504,10 +1569,7 @@ def on_pre_call(args):
);
assert_eq!(
wire.headers,
[
("x-route".to_string(), "route".to_string()),
("x-retained".to_string(), "sent".to_string()),
]
headers(&[("x-route", "route"), ("x-retained", "sent")])
);
}
@ -1699,7 +1761,7 @@ def check():
);
prop_assert_eq!(wire.body, edit.sent(&body));
prop_assert_eq!(wire.headers, [("x-route".to_string(), "route".to_string())]);
prop_assert_eq!(wire.headers, route_headers());
}
}
}

View file

@ -1,10 +1,28 @@
use http::header::{HeaderName, InvalidHeaderName, InvalidHeaderValue};
use litellm_error::Report;
use pyo3::{PyErr, exceptions::PyRuntimeError};
use pyo3::{
PyErr,
exceptions::{PyRuntimeError, PyValueError},
};
#[derive(Debug, thiserror::Error)]
pub(crate) enum Error {
#[error("call logging is not initialized")]
LoggingNotInitialized,
#[error("a pre-call callback set the header name {name:?}, which is not a valid header name")]
CallbackHeaderName {
name: String,
#[source]
source: InvalidHeaderName,
},
#[error(
"a pre-call callback set a value for the header {name} that is not a valid header value"
)]
CallbackHeaderValue {
name: HeaderName,
#[source]
source: InvalidHeaderValue,
},
}
impl From<Error> for PyErr {
@ -12,6 +30,9 @@ impl From<Error> for PyErr {
let message = Report::new(&error).to_string();
match error {
Error::LoggingNotInitialized => PyRuntimeError::new_err(message),
Error::CallbackHeaderName { .. } | Error::CallbackHeaderValue { .. } => {
PyValueError::new_err(message)
}
}
}
}

View file

@ -202,7 +202,7 @@ async fn projection_custom_operations_and_hooks_feed_the_http_response(intercept
.before_provider_request(
WireRequest {
url: request.into(),
headers: Vec::new(),
headers: http::HeaderMap::new(),
body: json!({}),
},
RequestContext {
@ -510,7 +510,7 @@ async fn hook_rejection_stops_execution_and_is_reported_once(
.before_provider_request(
WireRequest {
url: "url".into(),
headers: Vec::new(),
headers: http::HeaderMap::new(),
body: json!({}),
},
RequestContext {
@ -624,7 +624,7 @@ async fn unary_calls_use_into_response_after_hooks_and_before_success(intercepto
.before_provider_request(
WireRequest {
url: request.into(),
headers: Vec::new(),
headers: http::HeaderMap::new(),
body: json!({}),
},
RequestContext {

View file

@ -10,6 +10,7 @@ litellm-host.workspace = true
[dev-dependencies]
futures-util.workspace = true
http.workspace = true
rstest.workspace = true
serde_json.workspace = true
tokio.workspace = true

View file

@ -157,7 +157,7 @@ fn dispatching_call() -> TestMachine {
.before_provider_request(
WireRequest {
url: request.into(),
headers: Vec::new(),
headers: http::HeaderMap::new(),
body: json!({}),
},
RequestContext {

View file

@ -22,5 +22,6 @@ pyo3-async-runtimes.workspace = true
pythonize = "0.29.0"
[dev-dependencies]
http.workspace = true
rstest.workspace = true
serde_json.workspace = true

View file

@ -287,7 +287,7 @@ fn transformations_feed_each_other_and_notifications_share_final_values(
};
let wire = Box::new(WireRequest {
url: "url".into(),
headers: vec![],
headers: http::HeaderMap::new(),
body: serde_json::Value::Null,
});
let step = hooks.before_provider_request(py, wire, &context).unwrap();

View file

@ -114,7 +114,7 @@ impl Protocol for Synthetic {
pub fn wire() -> WireRequest {
WireRequest {
url: "https://example.invalid".into(),
headers: Vec::new(),
headers: http::HeaderMap::new(),
body: serde_json::json!({}),
}
}

View file

@ -7,6 +7,7 @@ repository.workspace = true
[dependencies]
futures-util.workspace = true
http.workspace = true
litellm-auth.workspace = true
litellm-coroutine.workspace = true
serde_json.workspace = true

View file

@ -1,12 +1,13 @@
use std::future::Future;
use http::HeaderMap;
use serde_json::Value;
/// The provider request as it is about to leave, offered to the host for rewriting.
#[derive(Clone, Debug, PartialEq)]
pub struct WireRequest {
pub url: String,
pub headers: Vec<(String, String)>,
pub headers: HeaderMap,
pub body: Value,
}
@ -158,7 +159,7 @@ mod tests {
fn wire(url: &str) -> WireRequest {
WireRequest {
url: url.into(),
headers: Vec::new(),
headers: HeaderMap::new(),
body: json!({}),
}
}

View file

@ -10,7 +10,7 @@ use std::time::Duration;
use reqwest::{
Request,
header::{CONTENT_TYPE, HeaderMap},
header::{CONTENT_TYPE, HeaderMap, HeaderValue},
};
use serde::Serialize;
@ -23,25 +23,17 @@ pub trait RequestSigner: Send + Sync {
pub fn json(
url: &str,
headers: &[(String, String)],
headers: HeaderMap,
body: &impl Serialize,
timeout: Option<Duration>,
) -> Result<Request, Error> {
let body = serde_json::to_vec(body).map_err(Repr::RequestBody)?;
let with_headers = headers
.iter()
.fold(http::Request::post(url), |builder, (name, value)| {
builder.header(name, value)
});
let has_content_type = with_headers
.headers_ref()
.is_some_and(|headers| headers.contains_key(CONTENT_TYPE));
let builder = if has_content_type {
with_headers
} else {
with_headers.header(CONTENT_TYPE, "application/json")
};
let mut request = from_http(builder.body(body).map_err(Repr::Request)?)?;
let mut request = from_http(http::Request::post(url).body(body).map_err(Repr::Request)?)?;
*request.headers_mut() = headers;
request
.headers_mut()
.entry(CONTENT_TYPE)
.or_insert(HeaderValue::from_static("application/json"));
*request.timeout_mut() = timeout;
Ok(request)
}
@ -60,7 +52,6 @@ pub fn signed(mut request: Request, signer: &dyn RequestSigner) -> Result<Reques
mod tests {
use std::sync::Mutex;
use reqwest::header::HeaderValue;
use serde_json::json;
use super::*;
@ -84,7 +75,7 @@ mod tests {
let request = signed(
json(
"https://provider.test/",
&[("x-caller".into(), "kept".into())],
crate::request::header_map(&[("x-caller", "kept")]),
&json!({"b": 1, "a": [true, null]}),
Some(Duration::from_secs(7)),
)
@ -116,7 +107,7 @@ mod tests {
}
let defaulted = signed(
json("https://u.test/", &[], &json!({}), None).unwrap(),
json("https://u.test/", HeaderMap::new(), &json!({}), None).unwrap(),
&RequiresContentType,
)
.unwrap();
@ -125,7 +116,7 @@ mod tests {
let provider = signed(
json(
"https://u.test/",
&[("Content-Type".into(), "application/x-amz-json-1.1".into())],
crate::request::header_map(&[("Content-Type", "application/x-amz-json-1.1")]),
&json!({}),
None,
)
@ -150,7 +141,7 @@ mod tests {
}
let error = signed(
json("https://u.test/", &[], &json!({}), None).unwrap(),
json("https://u.test/", HeaderMap::new(), &json!({}), None).unwrap(),
&Refuses,
)
.unwrap_err();
@ -160,16 +151,7 @@ mod tests {
#[test]
fn a_request_that_cannot_be_sent_fails_when_it_is_built() {
assert!(matches!(
json("not a url", &[], &json!({}), None),
Err(error) if error.is_request()
));
assert!(matches!(
json(
"https://u.test/",
&[("x-bad".into(), "\u{7f}".into())],
&json!({}),
None
),
json("not a url", HeaderMap::new(), &json!({}), None),
Err(error) if error.is_request()
));
}

View file

@ -1,16 +1,34 @@
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("invalid request: {context} extra_headers.{name} must be a string, got {actual}")]
pub struct HeaderError {
pub context: &'static str,
pub name: String,
pub actual: &'static str,
}
use http::header::{AUTHORIZATION, HeaderMap, HeaderName, HeaderValue};
use litellm_core_utils::core_helpers::json_type_name;
use litellm_error::Source;
use serde_json::{Map, Value};
use crate::Error;
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum HeaderError {
#[error("invalid request: {context} extra_headers.{name} must be a string, got {actual}")]
NotAString {
context: &'static str,
name: String,
actual: &'static str,
},
#[error("invalid request: {context} extra_headers.{name:?} is not a valid header name")]
InvalidName {
context: &'static str,
name: String,
#[source]
source: Source,
},
#[error("invalid request: {context} extra_headers.{name} is not a valid header value")]
InvalidValue {
context: &'static str,
name: HeaderName,
#[source]
source: Source,
},
}
pub enum HeaderPolicy<'a> {
All,
Only(&'a [&'a str]),
@ -19,7 +37,7 @@ pub enum HeaderPolicy<'a> {
pub fn with_headers(
builder: reqwest::RequestBuilder,
headers: &[(String, String)],
headers: &HeaderMap,
policy: HeaderPolicy<'_>,
) -> reqwest::RequestBuilder {
headers
@ -28,10 +46,10 @@ pub fn with_headers(
HeaderPolicy::All => true,
HeaderPolicy::Only(names) => names
.iter()
.any(|allowed| name.eq_ignore_ascii_case(allowed)),
.any(|allowed| name.as_str().eq_ignore_ascii_case(allowed)),
HeaderPolicy::Except(names) => !names
.iter()
.any(|excluded| name.eq_ignore_ascii_case(excluded)),
.any(|excluded| name.as_str().eq_ignore_ascii_case(excluded)),
})
.fold(builder, |builder, (name, value)| {
builder.header(name, value)
@ -49,93 +67,68 @@ pub async fn execute_http_request(
Ok(client.execute(request).await?)
}
pub fn header_pairs(headers: &reqwest::header::HeaderMap) -> Vec<(String, String)> {
headers
.iter()
.filter_map(|(name, value)| {
value
.to_str()
.ok()
.map(|value| (name.to_string(), value.to_string()))
})
.collect()
}
pub fn string_headers(
context: &'static str,
extra_headers: Option<Map<String, Value>>,
) -> Result<Vec<(String, String)>, HeaderError> {
) -> Result<HeaderMap, HeaderError> {
extra_headers
.unwrap_or_default()
.into_iter()
.map(|(key, value)| {
value
.as_str()
.map(|value| (key.clone(), value.to_string()))
.ok_or_else(|| HeaderError {
context,
name: key,
actual: json_type_name(&value),
})
})
.map(|(name, value)| string_header(context, name, value))
.collect()
}
pub fn has_header(headers: &[(String, String)], name: &str) -> bool {
headers
.iter()
.any(|(key, _)| key.eq_ignore_ascii_case(name))
}
pub fn header_values<'a>(
headers: &'a [(String, String)],
name: &str,
) -> impl Iterator<Item = &'a str> {
headers
.iter()
.filter(move |(key, _)| key.eq_ignore_ascii_case(name))
.map(|(_, value)| value.as_str())
}
pub fn header_value<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> {
header_values(headers, name).next()
}
pub fn without_headers(headers: Vec<(String, String)>, names: &[&str]) -> Vec<(String, String)> {
headers
.into_iter()
.filter(|(key, _)| !names.iter().any(|name| key.eq_ignore_ascii_case(name)))
.collect()
}
pub fn with_header(
headers: Vec<(String, String)>,
name: &str,
value: String,
) -> Vec<(String, String)> {
without_headers(headers, &[name])
.into_iter()
.chain([(name.to_ascii_lowercase(), value)])
.collect()
fn string_header(
context: &'static str,
name: String,
value: Value,
) -> Result<(HeaderName, HeaderValue), HeaderError> {
let Value::String(value) = value else {
return Err(HeaderError::NotAString {
context,
actual: json_type_name(&value),
name,
});
};
let name = match HeaderName::try_from(name.as_str()) {
Ok(parsed) => parsed,
Err(source) => {
return Err(HeaderError::InvalidName {
context,
name,
source: Source::new(source),
});
}
};
match HeaderValue::try_from(value) {
Ok(value) => Ok((name, value)),
Err(source) => Err(HeaderError::InvalidValue {
context,
name,
source: Source::new(source),
}),
}
}
pub fn with_default_headers(
headers: Vec<(String, String)>,
defaults: &[(&str, &str)],
) -> Vec<(String, String)> {
let missing: Vec<(String, String)> = defaults
headers: HeaderMap,
defaults: &[(&'static str, &'static str)],
) -> HeaderMap {
defaults
.iter()
.filter(|(name, _)| !has_header(&headers, name))
.map(|(name, value)| ((*name).to_string(), (*value).to_string()))
.collect();
headers.into_iter().chain(missing).collect()
.fold(headers, |mut headers, &(name, value)| {
headers
.entry(name)
.or_insert(HeaderValue::from_static(value));
headers
})
}
pub fn has_bearer_auth(headers: &[(String, String)]) -> bool {
headers.iter().any(|(name, value)| {
if !name.eq_ignore_ascii_case("authorization") {
pub fn has_bearer_auth(headers: &HeaderMap) -> bool {
headers.get_all(AUTHORIZATION).iter().any(|value| {
let Ok(value) = value.to_str() else {
return false;
}
};
let value = value.trim();
value.len() > 7
&& value
@ -145,13 +138,27 @@ pub fn has_bearer_auth(headers: &[(String, String)]) -> bool {
})
}
#[cfg(any(test, feature = "test-support"))]
pub fn header_map(pairs: &[(&str, &str)]) -> HeaderMap {
pairs
.iter()
.map(|&(name, value)| {
(
HeaderName::try_from(name).expect("test header name"),
HeaderValue::try_from(value).expect("test header value"),
)
})
.collect()
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use serde_json::json;
use super::*;
#[rstest::rstest]
#[rstest]
#[case(HeaderPolicy::All, true, true)]
#[case(HeaderPolicy::Only(&["authorization"]), true, false)]
#[case(HeaderPolicy::Except(&["authorization"]), false, true)]
@ -162,11 +169,11 @@ mod tests {
) {
let request = with_headers(
reqwest::Client::new().get("https://example.com"),
&[
("AuThOrIzAtIoN".into(), "Bearer token".into()),
("X-Trace".into(), "first".into()),
("x-trace".into(), "second".into()),
],
&header_map(&[
("AuThOrIzAtIoN", "Bearer token"),
("X-Trace", "first"),
("x-trace", "second"),
]),
policy,
)
.build()
@ -186,10 +193,10 @@ mod tests {
reqwest::Client::new()
.post("https://example.com")
.multipart(reqwest::multipart::Form::new().text("file", "abc")),
&[
("Content-Type".into(), "application/json".into()),
("CONTENT-LENGTH".into(), "0".into()),
],
&header_map(&[
("Content-Type", "application/json"),
("CONTENT-LENGTH", "0"),
]),
HeaderPolicy::Except(&["content-type", "content-length"]),
)
.build()
@ -203,13 +210,24 @@ mod tests {
assert_ne!(request.headers()["content-length"], "0");
}
#[test]
fn string_headers_are_matched_in_any_case() {
let headers = string_headers(
"chat completions",
Some(Map::from_iter([("X-Api-Key".to_string(), json!("k"))])),
)
.unwrap();
assert_eq!(headers, header_map(&[("x-api-key", "k")]));
assert_eq!(headers["X-API-KEY"], "k");
}
#[test]
fn string_headers_rejects_non_string_values() {
let headers = Map::from_iter([("x-trace".to_string(), json!(7))]);
let err = string_headers("chat completions", Some(headers)).expect_err("non-string value");
assert_eq!(
err,
HeaderError {
HeaderError::NotAString {
context: "chat completions",
name: "x-trace".into(),
actual: "number"
@ -217,67 +235,86 @@ mod tests {
);
}
#[test]
fn header_lookup_is_case_insensitive() {
let headers = vec![("X-Api-Key".to_string(), "k".to_string())];
assert!(has_header(&headers, "x-api-key"));
assert!(!has_header(&headers, "authorization"));
#[rstest]
#[case::space_in_name("x trace", "v", true)]
#[case::empty_name("", "v", true)]
#[case::newline_in_value("x-trace", "a\r\nx-injected: 1", false)]
#[case::delete_in_value("x-trace", "\u{7f}", false)]
fn string_headers_rejects_what_cannot_go_on_the_wire(
#[case] name: &str,
#[case] value: &str,
#[case] bad_name: bool,
) {
let headers = Map::from_iter([(name.to_string(), json!(value))]);
let err = string_headers("messages", Some(headers)).expect_err("invalid header");
match err {
HeaderError::InvalidName {
name: ref rejected, ..
} => {
assert!(bad_name);
assert_eq!(rejected, name);
}
HeaderError::InvalidValue {
name: ref rejected, ..
} => {
assert!(!bad_name);
assert_eq!(rejected, name);
let report = litellm_error::Report::new(&err).to_string();
assert!(!report.contains(value), "{report}");
}
HeaderError::NotAString { .. } => panic!("value was a string"),
}
}
#[test]
fn header_value_reads_the_first_match_in_any_case() {
let headers = vec![
("X-Api-Key".to_string(), "first".to_string()),
("x-api-key".to_string(), "second".to_string()),
];
assert_eq!(header_value(&headers, "x-API-key"), Some("first"));
assert_eq!(header_value(&headers, "authorization"), None);
}
#[test]
fn without_headers_drops_every_casing_of_the_named_headers_and_keeps_order() {
let headers = vec![
("X-Api-Key".to_string(), "k".to_string()),
("anthropic-version".to_string(), "v".to_string()),
("AUTHORIZATION".to_string(), "Bearer t".to_string()),
("x-api-key".to_string(), "k2".to_string()),
];
#[rstest]
#[case::nothing_forwarded(
&[],
&[("x-version", "1"), ("content-type", "application/json")],
&[("x-version", "1"), ("content-type", "application/json")],
)]
#[case::forwarded_header_wins_in_any_case(
&[("X-Version", "custom"), ("x-api-key", "k")],
&[("x-version", "1"), ("content-type", "application/json")],
&[("x-version", "custom"), ("x-api-key", "k"), ("content-type", "application/json")],
)]
#[case::repeated_forwarded_header_is_kept(
&[("x-version", "a"), ("x-version", "b")],
&[("x-version", "1")],
&[("x-version", "a"), ("x-version", "b")],
)]
#[case::no_defaults(&[("x-api-key", "k")], &[], &[("x-api-key", "k")])]
fn default_headers_fill_only_missing_names(
#[case] forwarded: &[(&str, &str)],
#[case] defaults: &[(&'static str, &'static str)],
#[case] expected: &[(&str, &str)],
) {
assert_eq!(
without_headers(headers, &["x-api-key", "authorization"]),
vec![("anthropic-version".to_string(), "v".to_string())]
with_default_headers(header_map(forwarded), defaults),
header_map(expected)
);
}
#[test]
fn auth_header_detection_is_case_insensitive() {
let headers = vec![
("x-trace-id".to_string(), "trace-1".to_string()),
("authorization".to_string(), "Bearer sk-test".to_string()),
];
assert!(has_header(&headers, "authorization"));
let headers = vec![("Authorization".to_string(), "Bearer sk-test".to_string())];
assert!(has_header(&headers, "authorization"));
let headers = vec![("x-trace-id".to_string(), "trace-1".to_string())];
assert!(!has_header(&headers, "authorization"));
}
#[rstest::rstest]
#[rstest]
#[case::bearer("Authorization", "Bearer abc", true)]
#[case::lowercase("authorization", "bearer abc", true)]
#[case::blank_token("Authorization", "Bearer ", false)]
#[case::empty("authorization", "", false)]
#[case::basic("Authorization", "Basic abc", false)]
#[case::different_header("x-api-key", "abc", false)]
#[case::non_ascii("authorization", "invali\u{00e9}auth", false)]
fn bearer_detection_requires_a_non_empty_token(
#[case] name: &str,
#[case] value: &str,
#[case] expected: bool,
) {
assert_eq!(has_bearer_auth(&[(name.into(), value.into())]), expected);
assert_eq!(has_bearer_auth(&header_map(&[(name, value)])), expected);
}
#[test]
fn bearer_detection_skips_a_value_that_is_not_visible_ascii() {
let headers = HeaderMap::from_iter([(
AUTHORIZATION,
HeaderValue::from_bytes("Bearer invali\u{00e9}auth".as_bytes()).unwrap(),
)]);
assert!(!has_bearer_auth(&headers));
}
}

View file

@ -1,69 +0,0 @@
use litellm_http::request::{header_values, with_default_headers, with_header};
use rstest::{fixture, rstest};
fn headers(pairs: &[(&str, &str)]) -> Vec<(String, String)> {
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
}
#[rstest]
#[case::nothing_forwarded(
&[],
&[("x-version", "1"), ("content-type", "application/json")],
&[("x-version", "1"), ("content-type", "application/json")],
)]
#[case::forwarded_header_wins_in_any_case(
&[("X-Version", "custom"), ("x-api-key", "k")],
&[("x-version", "1"), ("content-type", "application/json")],
&[("X-Version", "custom"), ("x-api-key", "k"), ("content-type", "application/json")],
)]
#[case::no_defaults(&[("x-api-key", "k")], &[], &[("x-api-key", "k")])]
fn default_headers_fill_only_missing_names(
#[case] forwarded: &[(&str, &str)],
#[case] defaults: &[(&str, &str)],
#[case] expected: &[(&str, &str)],
) {
assert_eq!(
with_default_headers(headers(forwarded), defaults),
headers(expected)
);
}
#[fixture]
fn forwarded() -> Vec<(String, String)> {
headers(&[
("X-Mode", "first"),
("x-trace", "trace"),
("x-MODE", "second"),
])
}
#[rstest]
#[case::matching_name("X-MODE", vec!["first", "second"])]
#[case::missing_name("missing", vec![])]
fn header_values_preserve_all_matching_values_in_order(
forwarded: Vec<(String, String)>,
#[case] name: &str,
#[case] expected: Vec<&str>,
) {
assert_eq!(
header_values(&forwarded, name).collect::<Vec<_>>(),
expected
);
}
#[rstest]
#[case::replace("X-MODE", &[("x-trace", "trace"), ("x-mode", "new")])]
#[case::insert("X-New", &[("X-Mode", "first"), ("x-trace", "trace"), ("x-MODE", "second"), ("x-new", "new")])]
fn with_header_replaces_every_matching_name_and_preserves_other_headers(
forwarded: Vec<(String, String)>,
#[case] name: &str,
#[case] expected: &[(&str, &str)],
) {
assert_eq!(
with_header(forwarded, name, "new".into()),
headers(expected)
);
}

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
litellm-auth = { workspace = true, features = ["aws", "azure", "gcp"] }
litellm-cache-response.workspace = true
litellm-core-utils.workspace = true

View file

@ -41,7 +41,7 @@ pub(super) fn chat_completions_provider(provider: LlmProviders) -> Option<ChatPr
pub(super) fn string_headers(
extra_headers: Option<Map<String, Value>>,
) -> Result<Vec<(String, String)>, Error> {
) -> Result<http::HeaderMap, Error> {
shared_string_headers(HEADER_CONTEXT, extra_headers).map_err(Error::from)
}
@ -84,7 +84,7 @@ mod tests {
#[rstest]
#[case::anthropic(LlmProviders::Anthropic, ("anthropic-version", "2023-06-01"))]
#[case::bedrock(LlmProviders::Bedrock, ("Content-Type", "application/json"))]
#[case::bedrock(LlmProviders::Bedrock, ("content-type", "application/json"))]
fn resolves_supported_provider_configs(
#[case] provider: LlmProviders,
#[case] expected_header: (&str, &str),

View file

@ -168,11 +168,11 @@ mod tests {
let mut body = wire.body;
body["system"] = json!("added by the host");
Ok(WireRequest {
headers: wire
.headers
.into_iter()
.chain([("x-host".to_string(), "seen".to_string())])
.collect(),
headers: {
let mut headers = wire.headers;
headers.append("x-host", http::HeaderValue::from_static("seen"));
headers
},
body,
..wire
})

View file

@ -282,7 +282,7 @@ async fn rejects_non_string_extra_headers() {
})
.await
.unwrap_err(),
Error::Headers(litellm_http::request::HeaderError {
Error::Headers(litellm_http::request::HeaderError::NotAString {
context: "chat completions",
name: "x-trace".to_string(),
actual: "number",

View file

@ -179,11 +179,11 @@ impl<P: litellm_host::protocol::Protocol> litellm_host::interceptors::Intercepto
_: litellm_host::interceptors::RequestContext,
) -> Result<litellm_host::interceptors::WireRequest, P::Error> {
Ok(litellm_host::interceptors::WireRequest {
headers: wire
.headers
.into_iter()
.chain([("x-hook".into(), "called".into())])
.collect(),
headers: {
let mut headers = wire.headers;
headers.append("x-hook", http::HeaderValue::from_static("called"));
headers
},
..wire
})
}

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
bytes.workspace = true
futures-util.workspace = true
litellm-auth = { workspace = true, features = ["aws", "azure", "gcp"] }

View file

@ -66,7 +66,7 @@ pub(crate) fn messages_provider(provider: LlmProviders, model: &str) -> Option<M
pub(super) fn string_headers(
extra_headers: Option<Map<String, Value>>,
) -> Result<Vec<(String, String)>, Error> {
) -> Result<http::HeaderMap, Error> {
shared_string_headers(HEADER_CONTEXT, extra_headers).map_err(Error::from)
}
@ -111,7 +111,7 @@ mod tests {
let err = string_headers(Some(headers)).expect_err("non-string header rejected");
assert_eq!(
err,
Error::Headers(litellm_http::request::HeaderError {
Error::Headers(litellm_http::request::HeaderError::NotAString {
context: "messages",
name: "x-count".to_string(),
actual: "number",

View file

@ -149,7 +149,7 @@ async fn provider_call(
headers: config.request_headers(
with_default_headers(validated.headers, config.default_headers()),
&body,
),
)?,
auth: validated.auth,
};
@ -506,7 +506,7 @@ mod tests {
.headers
.iter()
.filter(|(name, _)| matches!(name.as_str(), "x-api-key" | "authorization"))
.map(|(name, value)| (name.as_str(), value.as_str()))
.map(|(name, value)| (name.as_str(), value.to_str().unwrap()))
.collect();
assert_eq!(
(auth.as_slice(), call.wire.url.as_str()),
@ -690,7 +690,7 @@ mod tests {
.headers
.iter()
.filter(|(name, _)| matches!(name.as_str(), "x-priority" | "x-scoped"))
.map(|(name, value)| (name.as_str(), value.as_str()))
.map(|(name, value)| (name.as_str(), value.to_str().unwrap()))
.collect();
assert_eq!(caller_headers, expected);
}

View file

@ -64,7 +64,7 @@ async fn messages_cache_identity_includes_provider_native_parameters(
},
Some(&WireRequest {
url: "https://example.test/v1/messages".into(),
headers: vec![],
headers: http::HeaderMap::new(),
body: json!({
"model":"test", "messages":[{"role":"user","content":"hello"}],
"max_tokens":32, (field):value

View file

@ -223,11 +223,11 @@ async fn what_before_send_returns_is_what_the_provider_receives(call: MessagesCa
let mut body = wire.body;
body["system"] = json!("added by the host");
Ok(WireRequest {
headers: wire
.headers
.into_iter()
.chain([("x-host".to_string(), "seen".to_string())])
.collect(),
headers: {
let mut headers = wire.headers;
headers.append("x-host", http::HeaderValue::from_static("seen"));
headers
},
body,
..wire
})

View file

@ -180,11 +180,11 @@ impl<P: litellm_host::protocol::Protocol> litellm_host::interceptors::Intercepto
_: litellm_host::interceptors::RequestContext,
) -> Result<litellm_host::interceptors::WireRequest, P::Error> {
Ok(litellm_host::interceptors::WireRequest {
headers: wire
.headers
.into_iter()
.chain([("x-hook".into(), "called".into())])
.collect(),
headers: {
let mut headers = wire.headers;
headers.append("x-hook", http::HeaderValue::from_static("called"));
headers
},
..wire
})
}

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
base64.workspace = true
bytes.workspace = true
futures-util.workspace = true

View file

@ -3,6 +3,7 @@ use std::{collections::BTreeMap, path::PathBuf, time::Duration};
use bytes::Bytes;
use litellm_auth::{InputSource, SecretValue, TokenProviderHandle};
use litellm_core_utils::call_arguments::CallArguments;
use litellm_http::request::string_headers;
use litellm_llms::base_llm::ocr::{
error::Error,
transformation::{OcrCredentialInputs, OcrTransportConfig, response_format},
@ -57,21 +58,6 @@ impl OcrConnectionInputs {
fn source(&self, name: &str) -> InputSource {
self.input_sources.get(name).copied().unwrap_or_default()
}
fn header_pairs(&self) -> Result<Vec<(String, String)>, Error> {
self.extra_headers
.iter()
.map(|(name, value)| {
value
.as_str()
.map(|value| (name.clone(), value.to_string()))
.ok_or_else(|| Error::RequestField {
path: format!("extra_headers.{name}"),
cause: None,
})
})
.collect()
}
}
pub struct LiteLLMOcrRequest<D = OcrDocumentInput> {
@ -207,7 +193,7 @@ impl LiteLLMOcrRequest {
) -> Result<Self, Error> {
let request = Self::new(model, document, custom_llm_provider, optional_params)?;
let transport = request.transport.clone().with_overrides(
connection.header_pairs()?,
string_headers("ocr", Some(connection.extra_headers.clone()))?,
connection.source("extra_headers"),
connection.timeout,
);
@ -275,7 +261,7 @@ mod tests {
assert!(request.credentials.api_base.is_none());
assert_eq!(
request.transport.extra_headers,
vec![("x-a".to_string(), "1".to_string())]
litellm_http::request::header_map(&[("x-a", "1")])
);
assert_eq!(request.transport.extra_headers_source, InputSource::Request);
assert_eq!(request.transport.timeout, Some(Duration::from_secs(7)));
@ -300,7 +286,7 @@ mod tests {
}
#[test]
fn from_inputs_rejects_non_string_header_values_by_path() {
fn from_inputs_rejects_non_string_header_values_by_name() {
let Err(error) = LiteLLMOcrRequest::from_inputs(
"mistral/model".into(),
document(),
@ -315,7 +301,7 @@ mod tests {
};
assert!(matches!(
error,
Error::RequestField { ref path, .. } if path == "extra_headers.x-a"
Error::Headers(litellm_http::request::HeaderError::NotAString { ref name, .. }) if name == "x-a"
));
}
}

View file

@ -105,10 +105,7 @@ async fn a_body_rewritten_by_before_send_is_what_gets_signed_and_sent() {
let host = LocalOcrHost::new(textract_request(DETECT, &upstream.uri())).with_before_send(
|mut wire, _| {
assert!(
!wire
.headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case("authorization")),
!wire.headers.contains_key(http::header::AUTHORIZATION),
"the hook ran after signing"
);
wire.body["Document"]["Bytes"] = Value::from("cmVkYWN0ZWQ=");

View file

@ -148,7 +148,7 @@ async fn headers_returned_by_before_send_are_sent() {
let host = LocalOcrHost::new(ocr_request("mistral/model", &upstream.uri(), json!({})))
.with_before_send(|mut wire, _| {
wire.headers
.push(("x-core-callback".into(), "edited".into()));
.append("x-core-callback", http::HeaderValue::from_static("edited"));
Ok(wire)
});
@ -255,29 +255,22 @@ impl litellm_host::interceptors::Interceptors<<Ocr as litellm_host::protocol::Pr
{
async fn before_provider_request(
&self,
wire: WireRequest,
mut wire: WireRequest,
_: RequestContext,
) -> Result<WireRequest, Error> {
let is_authorization = |name: &str| name.eq_ignore_ascii_case("authorization");
let authorization = wire
.headers
.iter()
.find(|(name, _)| is_authorization(name))
.map(|(_, value)| value.clone())
.get(http::header::AUTHORIZATION)
.map(|value| value.to_str().unwrap().to_string())
.unwrap_or_default();
self.trace
.lock()
.unwrap()
.push(format!("before_provider_request:{authorization}"));
let headers = wire
.headers
.into_iter()
.map(|(name, value)| match is_authorization(&name) {
true => (name, "Bearer edited".to_string()),
false => (name, value),
})
.collect();
Ok(WireRequest { headers, ..wire })
if let Some(value) = wire.headers.get_mut(http::header::AUTHORIZATION) {
*value = http::HeaderValue::from_static("Bearer edited");
}
Ok(wire)
}
async fn after_provider_response(
&self,
@ -341,11 +334,11 @@ async fn direct_execution_uses_hooks_without_a_machine() {
_: RequestContext,
) -> Result<WireRequest, Error> {
Ok(WireRequest {
headers: wire
.headers
.into_iter()
.chain([("x-direct-hook".into(), "called".into())])
.collect(),
headers: {
let mut headers = wire.headers;
headers.append("x-direct-hook", http::HeaderValue::from_static("called"));
headers
},
..wire
})
}

View file

@ -248,7 +248,7 @@ async fn interrupt_drops_provider_captures_before_returning() {
let dropped = Arc::new(AtomicBool::new(false));
let mut request = ocr_request("azure_ai/mistral-ocr", "https://example.invalid", json!({}));
request.transport = OcrTransportConfig {
extra_headers: vec![("authorization".into(), "Bearer test-key".into())],
extra_headers: litellm_http::request::header_map(&[("authorization", "Bearer test-key")]),
..request.transport
};
request.azure_ad_token_provider = Some(litellm_auth::TokenProviderHandle::new(Arc::new(

View file

@ -124,10 +124,7 @@ fn with_source(request: LiteLLMOcrRequest, source: &str) -> LiteLLMOcrRequest {
fn with_headers(request: LiteLLMOcrRequest, headers: &[(&str, &str)]) -> LiteLLMOcrRequest {
let mut request = request;
request.transport.extra_headers = headers
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect();
request.transport.extra_headers = litellm_http::request::header_map(headers);
request
}

View file

@ -308,7 +308,7 @@ async fn guardrail_headers_reach_both_upload_and_parse(
);
let host = LocalOcrHost::new(request).with_before_send(|wire, _| {
Ok(WireRequest {
headers: vec![("authorization".into(), "Bearer guarded".into())],
headers: litellm_http::request::header_map(&[("authorization", "Bearer guarded")]),
..wire
})
});

View file

@ -191,11 +191,11 @@ impl<P: litellm_host::protocol::Protocol> litellm_host::interceptors::Intercepto
_: litellm_host::interceptors::RequestContext,
) -> Result<litellm_host::interceptors::WireRequest, P::Error> {
Ok(litellm_host::interceptors::WireRequest {
headers: wire
.headers
.into_iter()
.chain([("x-hook".into(), "called".into())])
.collect(),
headers: {
let mut headers = wire.headers;
headers.append("x-hook", http::HeaderValue::from_static("called"));
headers
},
..wire
})
}

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
base64.workspace = true
bytes.workspace = true
futures-util.workspace = true

View file

@ -180,11 +180,11 @@ impl<P: litellm_host::protocol::Protocol> litellm_host::interceptors::Intercepto
_: litellm_host::interceptors::RequestContext,
) -> Result<litellm_host::interceptors::WireRequest, P::Error> {
Ok(litellm_host::interceptors::WireRequest {
headers: wire
.headers
.into_iter()
.chain([("x-hook".into(), "called".into())])
.collect(),
headers: {
let mut headers = wire.headers;
headers.append("x-hook", http::HeaderValue::from_static("called"));
headers
},
..wire
})
}

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
futures-util.workspace = true
litellm-auth.workspace = true
litellm-core-utils.workspace = true

View file

@ -6,6 +6,7 @@ license.workspace = true
repository.workspace = true
[dependencies]
http.workspace = true
litellm-cache.workspace = true
litellm-cache-response.workspace = true
litellm-framer.workspace = true

View file

@ -47,7 +47,13 @@ impl CacheRequest {
"provider": identity.provider,
"model": identity.model,
"url": wire.url,
"headers": wire.headers,
"headers": wire
.headers
.iter()
.map(|(name, value)| {
(name.as_str(), String::from_utf8_lossy(value.as_bytes()))
})
.collect::<Vec<_>>(),
"body": wire.body,
})
}),

View file

@ -28,7 +28,7 @@ pub fn outbound_request(
timeout: Option<Duration>,
) -> Result<reqwest::Request, litellm_http::Error> {
let Authenticated { headers, signer } = authenticated;
let request = json(url, &headers, body, timeout)?;
let request = json(url, headers, body, timeout)?;
match signer {
None => Ok(request),
Some(signer) => signed(request, &signer),

View file

@ -26,6 +26,7 @@ base64.workspace = true
bytes.workspace = true
data-url = "0.3.2"
futures-util.workspace = true
http.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json = { workspace = true, features = ["preserve_order"] }

View file

@ -1,7 +1,10 @@
use litellm_http::request::{with_header, without_headers};
use http::HeaderMap;
use litellm_llms_types::providers::anthropic::{BETA_HEADER, BetaProvider, BetaSet};
use crate::{anthropic::common_utils::existing_betas, base_llm::auth::Headers};
use crate::{
Error,
anthropic::common_utils::{existing_betas, with_betas},
};
/// How the `anthropic-beta` header is treated before a request leaves for a host.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@ -15,37 +18,34 @@ pub enum BetaPolicy {
}
impl BetaPolicy {
pub fn apply(self, headers: Headers) -> Headers {
pub fn apply(self, mut headers: HeaderMap) -> Result<HeaderMap, Error> {
let provider = match self {
Self::Forward => return headers,
Self::Drop => return without_headers(headers, &[BETA_HEADER]),
Self::Forward => return Ok(headers),
Self::Drop => {
headers.remove(BETA_HEADER);
return Ok(headers);
}
Self::Filter(provider) => provider,
};
let accepted: BetaSet = existing_betas(&headers)
.iter()
.filter_map(|beta| beta.on(provider))
.collect();
let headers = without_headers(headers, &[BETA_HEADER]);
headers.remove(BETA_HEADER);
if accepted.is_empty() {
return headers;
return Ok(headers);
}
with_header(headers, BETA_HEADER, accepted.to_string())
with_betas(headers, &accepted)
}
}
#[cfg(test)]
mod tests {
use crate::anthropic::beta_headers::BetaPolicy;
use litellm_http::request::header_map as headers;
use litellm_llms_types::providers::anthropic::BetaProvider;
use rstest::rstest;
fn headers(pairs: &[(&str, &str)]) -> Vec<(String, String)> {
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
}
#[rstest]
fn forward_keeps_headers_as_is() {
let input = headers(&[
@ -55,7 +55,7 @@ mod tests {
),
("x-api-key", "k"),
]);
assert_eq!(BetaPolicy::Forward.apply(input.clone()), input);
assert_eq!(BetaPolicy::Forward.apply(input.clone()), Ok(input));
}
#[rstest]
@ -66,7 +66,7 @@ mod tests {
("x-api-key", "k"),
("ANTHROPIC-BETA", "b"),
])),
headers(&[("x-api-key", "k")])
Ok(headers(&[("x-api-key", "k")]))
);
}
@ -91,7 +91,7 @@ mod tests {
("Anthropic-Beta", "advanced-tool-use-2025-11-20"),
("x-api-key", "k"),
])),
expected
Ok(expected)
);
}
@ -108,7 +108,7 @@ mod tests {
("x-api-key", "k"),
("anthropic-beta", beta_value)
])),
headers(&[("x-api-key", "k")])
Ok(headers(&[("x-api-key", "k")]))
);
}
@ -137,7 +137,10 @@ mod tests {
("x-api-key", "k"),
("anthropic-beta", second_beta),
])),
headers(&[("x-api-key", "k"), ("anthropic-beta", expected_beta)])
Ok(headers(&[
("x-api-key", "k"),
("anthropic-beta", expected_beta)
]))
);
}
@ -147,6 +150,6 @@ mod tests {
#[case::filter(BetaPolicy::Filter(BetaProvider::Bedrock))]
fn headers_without_a_beta_are_untouched(#[case] policy: BetaPolicy) {
let input = headers(&[("x-api-key", "k"), ("anthropic-version", "2023")]);
assert_eq!(policy.apply(input.clone()), input);
assert_eq!(policy.apply(input.clone()), Ok(input));
}
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::SecretValue;
use litellm_core_utils::decline::Decline;
use litellm_core_utils::{
@ -24,7 +25,7 @@ use crate::{
chat::{
streaming::StreamShape,
transformation::{
BaseConfig, Headers, ProviderChatRequestData, ProviderChatResponseData,
BaseConfig, ProviderChatRequestData, ProviderChatResponseData,
ValidatedEnvironment, unsupported_message, unsupported_param,
},
},
@ -171,7 +172,7 @@ impl BaseConfig for AnthropicConfig {
/// the deployment's key, which Python writes last.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &Map<String, Value>,
@ -689,7 +690,7 @@ mod tests {
);
let validated = config
.validate_environment(
Vec::new(),
http::HeaderMap::new(),
Some("sk-x"),
"claude-sonnet-4-5",
&Map::new(),

View file

@ -1,9 +1,7 @@
use crate::base_llm::messages::context::MessagesModelCapabilities;
use http::{HeaderMap, HeaderName, HeaderValue, header::AUTHORIZATION};
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_llms_types::{
formats::messages::{
ContentBlock, ContentBlockType, EffortLevel, Message, MessageContent, MessagesTool,
@ -19,8 +17,9 @@ use serde::Deserialize;
use serde_json::Value;
use crate::{
Error,
anthropic::ANTHROPIC_OAUTH_TOKEN_PREFIX,
base_llm::auth::{AuthScheme, Headers},
base_llm::auth::{AuthScheme, header_str, header_value},
};
pub const ANTHROPIC_API_KEY_ENV: &str = "ANTHROPIC_API_KEY";
@ -30,7 +29,6 @@ const THOUGHT_SIGNATURE_SEPARATOR: &str = "__thought__";
pub const ANTHROPIC_API_BASE_ENV: &str = "ANTHROPIC_API_BASE";
pub const ANTHROPIC_BASE_URL_ENV: &str = "ANTHROPIC_BASE_URL";
pub const API_KEY_PLACEMENT: CredentialPlacement = CredentialPlacement::Header(API_KEY_HEADER);
const AUTHORIZATION: &str = CredentialPlacement::Bearer.header_name();
pub fn supports_effort_tier(capabilities: &MessagesModelCapabilities, level: EffortLevel) -> bool {
match level {
@ -130,8 +128,8 @@ pub fn resolve_anthropic_api_key(
}
/// Whether the caller already forwarded an Anthropic credential, in either header.
pub fn has_anthropic_credential(headers: &[(String, String)]) -> bool {
has_header(headers, API_KEY_HEADER) || has_header(headers, AUTHORIZATION)
pub fn has_anthropic_credential(headers: &HeaderMap) -> bool {
headers.contains_key(API_KEY_HEADER) || headers.contains_key(AUTHORIZATION)
}
pub fn resolve_anthropic_api_base(
@ -159,8 +157,11 @@ pub fn complete_anthropic_url(
format!("{api_base}{MESSAGES_PATH}")
}
pub fn existing_betas(headers: &[(String, String)]) -> BetaSet {
header_values(headers, BETA_HEADER)
pub fn existing_betas(headers: &HeaderMap) -> BetaSet {
headers
.get_all(BETA_HEADER)
.iter()
.filter_map(|value| value.to_str().ok())
.flat_map(|value| {
value
.parse::<BetaSet>()
@ -172,12 +173,17 @@ pub fn existing_betas(headers: &[(String, String)]) -> BetaSet {
/// Python's `_merge_beta_headers`, over every casing of the header at once: the union of what
/// the caller sent and `added` replaces the header, sorted and deduplicated. Headers without
/// any beta value stay as they are.
pub fn merge_beta_headers(headers: Headers, added: BetaSet) -> Headers {
pub fn merge_beta_headers(headers: HeaderMap, added: BetaSet) -> Result<HeaderMap, Error> {
let merged = existing_betas(&headers).union(added);
if merged.is_empty() {
return headers;
return Ok(headers);
}
with_header(headers, BETA_HEADER, merged.to_string())
with_betas(headers, &merged)
}
pub fn with_betas(mut headers: HeaderMap, betas: &BetaSet) -> Result<HeaderMap, Error> {
headers.insert(BETA_HEADER, header_value(BETA_HEADER, betas.to_string())?);
Ok(headers)
}
/// The outcome of Python's `optionally_handle_anthropic_oauth`.
@ -186,45 +192,57 @@ pub enum OauthHandling {
/// An OAuth token is the whole credential. The headers carry its companions and no
/// longer any `x-api-key` or `authorization`, so the bearer is applied on top.
Bearer {
headers: Headers,
headers: HeaderMap,
token: SecretValue,
},
Untouched(Headers),
Untouched(HeaderMap),
}
/// The OAuth token a caller forwarded as `Authorization: Bearer sk-ant-oat…`.
pub fn forwarded_oauth_bearer(headers: &[(String, String)]) -> Option<OauthToken<'_>> {
header_value(headers, AUTHORIZATION)
pub fn forwarded_oauth_bearer(headers: &HeaderMap) -> Option<OauthToken<'_>> {
header_str(headers, AUTHORIZATION.as_str())
.and_then(|value| value.strip_prefix("Bearer "))
.and_then(OauthToken::parse)
}
fn with_oauth_companions(headers: Headers, dropped: &[&str]) -> Headers {
merge_beta_headers(
without_headers(headers, dropped),
BetaSet::from_iter([AnthropicBeta::Oauth20250420]),
)
.into_iter()
.chain([(DIRECT_BROWSER_ACCESS_HEADER.to_string(), "true".to_string())])
.collect()
fn with_oauth_companions(
mut headers: HeaderMap,
dropped: &[HeaderName],
) -> Result<HeaderMap, Error> {
for name in dropped {
headers.remove(name);
}
let mut headers =
merge_beta_headers(headers, BetaSet::from_iter([AnthropicBeta::Oauth20250420]))?;
headers.insert(
DIRECT_BROWSER_ACCESS_HEADER,
HeaderValue::from_static("true"),
);
Ok(headers)
}
pub fn optionally_handle_anthropic_oauth(headers: Headers, api_key: Option<&str>) -> OauthHandling {
pub fn optionally_handle_anthropic_oauth(
headers: HeaderMap,
api_key: Option<&str>,
) -> Result<OauthHandling, Error> {
if let Some(token) =
forwarded_oauth_bearer(&headers).map(|token| SecretValue::new(token.as_str()))
{
return OauthHandling::Bearer {
headers: with_oauth_companions(headers, &[API_KEY_HEADER, AUTHORIZATION]),
return Ok(OauthHandling::Bearer {
headers: with_oauth_companions(
headers,
&[HeaderName::from_static(API_KEY_HEADER), AUTHORIZATION],
)?,
token,
};
});
}
if let Some(token) = api_key.and_then(OauthToken::parse) {
return OauthHandling::Bearer {
headers: with_oauth_companions(headers, &[API_KEY_HEADER]),
return Ok(OauthHandling::Bearer {
headers: with_oauth_companions(headers, &[HeaderName::from_static(API_KEY_HEADER)])?,
token: SecretValue::new(token.as_str()),
};
});
}
OauthHandling::Untouched(headers)
Ok(OauthHandling::Untouched(headers))
}
pub fn is_tool_search_used(tools: Option<&[Recognized<MessagesTool>]>) -> bool {
@ -620,6 +638,7 @@ pub fn flatten_unencrypted_web_search_results(messages: Vec<Message>) -> Vec<Mes
#[cfg(test)]
mod tests {
use crate::base_llm::messages::context::SupportedEffortTiers;
use litellm_http::request::header_map as headers;
use rstest::{fixture, rstest};
use serde_json::json;
use strum::VariantArray;
@ -643,13 +662,6 @@ mod tests {
value.map(|tools| serde_json::from_value(tools).unwrap())
}
fn headers(pairs: &[(&str, &str)]) -> Headers {
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
}
fn betas(values: &[&str]) -> BetaSet {
values.join(",").parse().unwrap()
}
@ -716,11 +728,7 @@ mod tests {
#[case] forwarded: &[(&str, &str)],
#[case] expected: bool,
) {
let headers: Headers = forwarded
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect();
assert_eq!(has_anthropic_credential(&headers), expected);
assert_eq!(has_anthropic_credential(&headers(forwarded)), expected);
}
fn credential(auth: Option<AuthScheme>) -> Option<(&'static str, String)> {
@ -1440,7 +1448,7 @@ mod tests {
) {
assert_eq!(
merge_beta_headers(headers(input), betas(added)),
headers(expected)
Ok(headers(expected))
);
}
@ -1528,10 +1536,10 @@ mod tests {
.collect::<Vec<_>>();
assert_eq!(
optionally_handle_anthropic_oauth(headers(forwarded), api_key),
OauthHandling::Bearer {
Ok(OauthHandling::Bearer {
headers: headers(&expected),
token: SecretValue::new(OAUTH_TOKEN),
}
})
);
}
@ -1558,13 +1566,13 @@ mod tests {
) {
assert_eq!(
optionally_handle_anthropic_oauth(headers(forwarded), api_key),
OauthHandling::Bearer {
Ok(OauthHandling::Bearer {
headers: headers(&[
("anthropic-beta", "oauth-2025-04-20,web-search-2025-03-05"),
BROWSER_ACCESS,
]),
token: SecretValue::new(OAUTH_TOKEN),
}
})
);
}
@ -1580,7 +1588,7 @@ mod tests {
) {
assert_eq!(
optionally_handle_anthropic_oauth(headers(forwarded), api_key),
OauthHandling::Untouched(headers(forwarded))
Ok(OauthHandling::Untouched(headers(forwarded)))
);
}
@ -1601,18 +1609,18 @@ mod tests {
#[case::auth_token_as_a_bearer(
None,
&[("ANTHROPIC_AUTH_TOKEN", "env-token")],
Some(("Authorization", "env-token")),
Some(("authorization", "env-token")),
)]
#[case::auth_token_when_the_env_key_is_blank(
None,
&[("ANTHROPIC_API_KEY", " \t"), ("ANTHROPIC_AUTH_TOKEN", "env-token")],
Some(("Authorization", "env-token")),
Some(("authorization", "env-token")),
)]
#[case::oauth_param_as_a_bearer(Some(OAUTH_TOKEN), &[], Some(("Authorization", OAUTH_TOKEN)))]
#[case::oauth_param_as_a_bearer(Some(OAUTH_TOKEN), &[], Some(("authorization", OAUTH_TOKEN)))]
#[case::bearer_prefixed_oauth_env_key_as_a_bearer_once(
None,
&[("ANTHROPIC_API_KEY", OAUTH_BEARER)],
Some(("Authorization", OAUTH_TOKEN)),
Some(("authorization", OAUTH_TOKEN)),
)]
#[case::no_credentials(None, &[], None)]
#[case::blank_everything(Some(""), &[("ANTHROPIC_API_KEY", " "), ("ANTHROPIC_AUTH_TOKEN", " \t")], None)]

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::CredentialPlacement;
use litellm_llms_types::{
formats::messages::{
@ -22,7 +23,7 @@ use crate::{
},
base_llm::{
auth::AuthScheme,
messages::transformation::{BaseMessagesConfig, Headers, ValidatedEnvironment},
messages::transformation::{BaseMessagesConfig, ValidatedEnvironment},
},
};
@ -71,14 +72,14 @@ impl BaseMessagesConfig for AnthropicMessagesConfig {
/// `request_headers` does once the request is final.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
_litellm_params: &LitellmParams,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
let headers = match optionally_handle_anthropic_oauth(headers, api_key) {
let headers = match optionally_handle_anthropic_oauth(headers, api_key)? {
OauthHandling::Bearer { headers, token } => {
return Ok(ValidatedEnvironment {
headers,
@ -109,7 +110,11 @@ impl BaseMessagesConfig for AnthropicMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
merge_beta_headers(
headers,
provider_feature_betas(request, BetaProvider::Anthropic),
@ -155,17 +160,17 @@ fn without_null_safeguards(extra: Map<String, Value>) -> Map<String, Value> {
}
pub(crate) fn update_headers_with_anthropic_beta(
headers: Headers,
headers: HeaderMap,
request: &MessagesRequest,
) -> Headers {
) -> Result<HeaderMap, Error> {
merge_beta_headers(headers, feature_betas(request))
}
pub(crate) fn update_headers_with_safeguards_beta(
headers: Headers,
headers: HeaderMap,
request: &MessagesRequest,
provider: BetaProvider,
) -> Headers {
) -> Result<HeaderMap, Error> {
merge_beta_headers(
headers,
feature_betas(request).union(on_provider(mapped_host_betas(&request.params), provider)),
@ -428,11 +433,8 @@ mod tests {
}
}
fn headers(pairs: &[(&str, &str)]) -> Headers {
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
fn headers(pairs: &[(&str, &str)]) -> HeaderMap {
litellm_http::request::header_map(pairs)
}
fn transform(
@ -910,14 +912,14 @@ mod tests {
Some("sk-deployment"),
&[("ANTHROPIC_API_KEY", "sk-env")],
&[("anthropic-version", "2023-06-01"), ("anthropic-beta", OAUTH_BETA), BROWSER_ACCESS],
Some(("Authorization", OAUTH_TOKEN)),
Some(("authorization", OAUTH_TOKEN)),
)]
#[case::oauth_api_key(
&[("x-api-key", OAUTH_TOKEN), ("anthropic-beta", "web-search-2025-03-05")],
Some(OAUTH_TOKEN),
&[],
&[("anthropic-beta", "oauth-2025-04-20,web-search-2025-03-05"), BROWSER_ACCESS],
Some(("Authorization", OAUTH_TOKEN)),
Some(("authorization", OAUTH_TOKEN)),
)]
#[case::forwarded_x_api_key_is_kept_over_the_deployment_key(
&[("X-API-KEY", "caller-key")],
@ -959,14 +961,14 @@ mod tests {
None,
&[("ANTHROPIC_API_KEY", " \t"), ("ANTHROPIC_AUTH_TOKEN", "env-token")],
&[],
Some(("Authorization", "env-token")),
Some(("authorization", "env-token")),
)]
#[case::oauth_env_key_as_a_bearer(
&[],
None,
&[("ANTHROPIC_API_KEY", "sk-ant-oat01-env")],
&[],
Some(("Authorization", "sk-ant-oat01-env")),
Some(("authorization", "sk-ant-oat01-env")),
)]
fn validate_environment_shapes_the_headers_and_names_the_credential(
#[case] forwarded: &[(&str, &str)],
@ -1101,7 +1103,7 @@ mod tests {
client.clone(),
&request(json!({"safeguards": [{"type": "dangerous_tool_use"}]}))
),
client
Ok(client)
);
}
@ -1155,7 +1157,7 @@ mod tests {
) {
assert_eq!(
ANTHROPIC_MESSAGES_CONFIG.request_headers(headers(input), &request(fields)),
headers(expected)
Ok(headers(expected))
);
}
@ -1172,17 +1174,17 @@ mod tests {
}));
assert_eq!(
ANTHROPIC_MESSAGES_CONFIG.request_headers(environment.headers, &all_features),
headers(&[
Ok(headers(&[
BROWSER_ACCESS,
(
"anthropic-beta",
"advanced-tool-use-2025-11-20,advisor-tool-2026-03-01,compact-2026-01-12,compact-2026-09-04,context-management-2025-06-27,fast-mode-2026-02-01,oauth-2025-04-20,per-turn-control-2026-07-01,structured-outputs-2025-11-13"
),
])
]))
);
assert_eq!(
credential(&environment.auth),
Some(("Authorization", OAUTH_TOKEN))
Some(("authorization", OAUTH_TOKEN))
);
}
@ -1205,7 +1207,7 @@ mod tests {
None
};
let _ = ANTHROPIC_MESSAGES_CONFIG.validate_environment(
Vec::new(),
http::HeaderMap::new(),
None,
"claude",
&MessagesOptionalParams::default(),

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use litellm_core_utils::call_arguments::{CallArguments, parse_options};
@ -87,7 +88,7 @@ impl BaseOcrConfig for TextractAnalyzeDocumentConfig {
_model: &str,
document: OcrDocument,
optional_params: &AnalyzeDocumentOptions,
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<AnalyzeDocumentRequest, Error> {
Ok(AnalyzeDocumentRequest {
document: document_bytes(&document)?,
@ -103,7 +104,7 @@ impl BaseOcrConfig for TextractAnalyzeDocumentConfig {
model: &str,
document: OcrDocument,
optional_params: &AnalyzeDocumentOptions,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<AnalyzeDocumentRequest, Error> {
let document = inline_document(document, context).await?;
@ -463,7 +464,7 @@ mod tests {
.unwrap();
let request = TextractAnalyzeDocumentConfig
.transform_ocr_request(MODEL, document, &params, &[])
.transform_ocr_request(MODEL, document, &params, &http::HeaderMap::new())
.unwrap();
assert_eq!(

View file

@ -1,4 +1,5 @@
use base64::{Engine, engine::general_purpose::STANDARD};
use http::{HeaderMap, HeaderValue, header::CONTENT_TYPE};
use litellm_auth::AwsParams;
use litellm_auth_aws::{AwsCredentialSource, SigV4Signer, resolve_aws_region};
use litellm_http::outbound::RequestSigner;
@ -14,8 +15,7 @@ use litellm_llms_types::formats::ocr::{LiteLLMOcrResponse, OcrDocument, OcrPage,
const TEXTRACT_SERVICE: &str = "textract";
const AWS_JSON_CONTENT_TYPE: &str = "application/x-amz-json-1.1";
const TARGET_HEADER: &str = "X-Amz-Target";
const CONTENT_TYPE_HEADER: &str = "Content-Type";
const TARGET_HEADER: &str = "x-amz-target";
const UNSUPPORTED_DOCUMENT: &str = "UnsupportedDocumentException";
const SYNC_API_GUIDANCE: &str = "aws_textract uses Textract's synchronous API, which reads a JPEG, PNG, or a single-page PDF or TIFF; other formats and multi-page documents are not supported";
const SYNC_DOCUMENT_MAX_BYTES: usize = 10 * 1024 * 1024;
@ -211,13 +211,13 @@ pub struct TextractResponse {
}
pub struct TextractEnvironment {
headers: Vec<(String, String)>,
headers: HeaderMap,
region: String,
signer: SigV4Signer,
}
impl OcrEnvironment for TextractEnvironment {
fn headers(&self) -> &[(String, String)] {
fn headers(&self) -> &HeaderMap {
&self.headers
}
@ -260,28 +260,14 @@ pub(super) async fn environment(
/// A caller's copy of an operation header would reach the wire next to ours
/// while the signature covers only one value, which Textract rejects.
fn operation_headers(
extra_headers: &[(String, String)],
operation: TextractOperation,
) -> Vec<(String, String)> {
let operation = [
(TARGET_HEADER, operation.target()),
(CONTENT_TYPE_HEADER, AWS_JSON_CONTENT_TYPE),
];
extra_headers
.iter()
.filter(|(name, _)| {
!operation
.iter()
.any(|(operation_name, _)| name.eq_ignore_ascii_case(operation_name))
})
.cloned()
.chain(
operation
.iter()
.map(|(name, value)| (name.to_string(), value.to_string())),
)
.collect()
fn operation_headers(extra_headers: &HeaderMap, operation: TextractOperation) -> HeaderMap {
let mut headers = extra_headers.clone();
headers.insert(TARGET_HEADER, HeaderValue::from_static(operation.target()));
headers.insert(
CONTENT_TYPE,
HeaderValue::from_static(AWS_JSON_CONTENT_TYPE),
);
headers
}
pub(super) fn endpoint(request: &PreparedOcrRequest, environment: &TextractEnvironment) -> String {
@ -598,12 +584,7 @@ mod tests {
#[case] extra_headers: Vec<(&str, &str)>,
#[case] kept: Vec<(&str, &str)>,
) {
let owned = |headers: Vec<(&str, &str)>| -> Vec<(String, String)> {
headers
.into_iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
};
let owned = |headers: Vec<(&str, &str)>| litellm_http::request::header_map(&headers);
let headers =
operation_headers(&owned(extra_headers), TextractOperation::DetectDocumentText);

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_core_utils::call_arguments::CallArguments;
use serde::{Deserialize, Serialize};
@ -72,7 +73,7 @@ impl BaseOcrConfig for TextractDetectTextConfig {
_model: &str,
document: OcrDocument,
_optional_params: &(),
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<DetectDocumentTextRequest, Error> {
Ok(DetectDocumentTextRequest {
document: document_bytes(&document)?,
@ -84,7 +85,7 @@ impl BaseOcrConfig for TextractDetectTextConfig {
model: &str,
document: OcrDocument,
optional_params: &(),
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<DetectDocumentTextRequest, Error> {
let document = inline_document(document, context).await?;
@ -201,7 +202,7 @@ mod tests {
#[rstest]
fn the_request_is_only_the_document_bytes(document: OcrDocument) {
let request = TextractDetectTextConfig
.transform_ocr_request(MODEL, document, &(), &[])
.transform_ocr_request(MODEL, document, &(), &http::HeaderMap::new())
.unwrap();
assert_eq!(
@ -215,7 +216,7 @@ mod tests {
#[with("https://example.com/a.pdf")] document: OcrDocument,
) {
let error = TextractDetectTextConfig
.transform_ocr_request(MODEL, document, &(), &[])
.transform_ocr_request(MODEL, document, &(), &http::HeaderMap::new())
.unwrap_err();
assert!(matches!(error, Error::InvalidDataUri));
@ -227,7 +228,7 @@ mod tests {
assert!(
TextractDetectTextConfig
.transform_ocr_request(MODEL, document, &(), &[])
.transform_ocr_request(MODEL, document, &(), &http::HeaderMap::new())
.is_ok()
);
}

View file

@ -1,5 +1,6 @@
use http::HeaderMap;
use litellm_auth::SecretValue;
use litellm_http::request::{has_bearer_auth, has_header};
use litellm_http::request::has_bearer_auth;
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::{BetaProvider, DEFAULT_HEADERS, MESSAGES_PATH},
@ -19,7 +20,7 @@ use crate::{
AZURE_API_BASE_ENV, AZURE_API_KEY_ENV, resolve_azure_api_base, resolve_azure_api_key,
},
base_llm::{
auth::{AuthScheme, Headers, ValidatedEnvironment},
auth::{AuthScheme, ValidatedEnvironment},
messages::{
context::MessagesTransformContext,
normalization::{normalize_system_role_messages, strip_cache_control_scope},
@ -80,14 +81,14 @@ impl BaseMessagesConfig for AzureAnthropicMessagesConfig {
/// otherwise the Azure key goes in `x-api-key`.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
_litellm_params: &LitellmParams,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
if has_header(&headers, API_KEY_PLACEMENT.header_name()) || has_bearer_auth(&headers) {
if headers.contains_key(API_KEY_PLACEMENT.header_name()) || has_bearer_auth(&headers) {
return Ok(ValidatedEnvironment {
headers,
auth: AuthScheme::Forwarded,
@ -104,7 +105,11 @@ impl BaseMessagesConfig for AzureAnthropicMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
update_headers_with_safeguards_beta(headers, request, BetaProvider::AzureAi)
}
}
@ -232,10 +237,7 @@ mod tests {
fn validated(forwarded: &[(&str, &str)], api_key: Option<&str>) -> ValidatedEnvironment {
AZURE_ANTHROPIC_MESSAGES_CONFIG
.validate_environment(
forwarded
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect(),
litellm_http::request::header_map(forwarded),
api_key,
"claude",
&MessagesOptionalParams::default(),
@ -557,12 +559,7 @@ mod tests {
#[case] forwarded: &[(&str, &str)],
#[case] expected: &[(&str, &str)],
) {
let pairs = |pairs: &[(&str, &str)]| -> Vec<(String, String)> {
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
};
let pairs = litellm_http::request::header_map;
let serde_json::Value::Object(fields) = fields else {
panic!("case fields are an object")
};
@ -581,13 +578,10 @@ mod tests {
));
assert_eq!(
AZURE_ANTHROPIC_MESSAGES_CONFIG.request_headers(
pairs(&[("x-api-key", "k")])
.into_iter()
.chain(pairs(forwarded))
.collect(),
pairs(&[[("x-api-key", "k")].as_slice(), forwarded].concat()),
&request
),
pairs(expected)
Ok(pairs(expected))
);
}
@ -621,7 +615,7 @@ mod tests {
None
};
let _ = AZURE_ANTHROPIC_MESSAGES_CONFIG.validate_environment(
Vec::new(),
http::HeaderMap::new(),
None,
"claude",
&MessagesOptionalParams::default(),

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_core_utils::{call_arguments::CallArguments, url_utils::ApiUrl};
use serde_json::Value;
@ -22,7 +23,7 @@ pub struct AzureAICohereParseConfig;
impl BaseOcrConfig for AzureAICohereParseConfig {
type OcrParams = CohereOptions;
type ProviderRequest = CohereRequest;
type Environment = Vec<(String, String)>;
type Environment = HeaderMap;
fn get_api_key_env_var(&self) -> Option<&'static str> {
super::transformation::AzureAiOcrConfig.get_api_key_env_var()
@ -67,7 +68,7 @@ impl BaseOcrConfig for AzureAICohereParseConfig {
model: &str,
document: OcrDocument,
params: &CohereOptions,
headers: &[(String, String)],
headers: &HeaderMap,
) -> Result<CohereRequest, Error> {
CohereParseConfig.transform_ocr_request(model, document, params, headers)
}
@ -89,7 +90,7 @@ impl BaseOcrConfig for AzureAICohereParseConfig {
model: &str,
document: OcrDocument,
optional_params: &CohereOptions,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<CohereRequest, Error> {
validate_document(&document)?;

View file

@ -1,7 +1,8 @@
use std::{collections::BTreeSet, time::Duration};
use base64::{Engine, engine::general_purpose::STANDARD};
use litellm_auth::{InputSource, Sourced};
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{CredentialPlacement, InputSource, Sourced, http::apply_credential};
use litellm_auth_azure::{AzureAuthInputs, SECRET_NAMES as AZURE_AUTH_SECRET_NAMES};
use litellm_core_utils::{call_arguments::CallArguments, url_utils::ApiUrl};
use litellm_llms_types::serde_compat::{FiniteF64, LaxI64};
@ -26,7 +27,7 @@ use litellm_llms_types::formats::ocr::{
LiteLLMOcrResponse, OcrDocument, OcrPage, OcrPageDimensions, OcrResponseFormat, OcrUsageInfo,
};
const AZURE_DI_SUBSCRIPTION_HEADER: &str = "Ocp-Apim-Subscription-Key";
const AZURE_DI_SUBSCRIPTION_HEADER: &str = "ocp-apim-subscription-key";
const AZURE_DI_DEFAULT_WIDTH: f64 = 8.5;
const AZURE_DI_DEFAULT_HEIGHT: f64 = 11.0;
@ -113,7 +114,7 @@ pub struct AzureDocumentIntelligenceOcrConfig;
impl BaseOcrConfig for AzureDocumentIntelligenceOcrConfig {
type OcrParams = DocumentIntelligenceParams;
type ProviderRequest = DocumentIntelligenceRequest;
type Environment = Vec<(String, String)>;
type Environment = HeaderMap;
fn get_supported_ocr_params(&self, _model: &str) -> &'static [&'static str] {
&["pages", "features", "req_format"]
@ -205,7 +206,7 @@ impl BaseOcrConfig for AzureDocumentIntelligenceOcrConfig {
_model: &str,
document: OcrDocument,
_optional_params: &DocumentIntelligenceParams,
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<DocumentIntelligenceRequest, Error> {
build_request(document)
}
@ -437,7 +438,7 @@ async fn read_operation_response(
http_client: &litellm_http::Client,
response: reqwest::Response,
original_url: &str,
headers: &[(String, String)],
headers: &HeaderMap,
connection: &OcrConnection,
native: bool,
hooks: &dyn CallHooks<Error>,
@ -481,7 +482,7 @@ async fn read_operation_response(
async fn poll_operation(
http_client: &litellm_http::Client,
url: Url,
headers: &[(String, String)],
headers: &HeaderMap,
connection: &OcrConnection,
native: bool,
hooks: &dyn CallHooks<Error>,
@ -591,12 +592,11 @@ impl AzureDocumentIntelligenceOcrConfig {
connection: &OcrConnection,
config: &AzureAuthInputs,
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
) -> Result<Vec<(String, String)>, Error> {
if litellm_http::request::has_header(&connection.extra_headers, "authorization")
|| litellm_http::request::has_header(
&connection.extra_headers,
AZURE_DI_SUBSCRIPTION_HEADER,
)
) -> Result<HeaderMap, Error> {
if connection.extra_headers.contains_key(AUTHORIZATION)
|| connection
.extra_headers
.contains_key(AZURE_DI_SUBSCRIPTION_HEADER)
{
super::super::common_utils::validate_destination(
connection,
@ -617,11 +617,11 @@ impl AzureDocumentIntelligenceOcrConfig {
});
if let Some(key) = key {
super::super::common_utils::validate_destination(connection, key.source())?;
return Ok(
std::iter::once((AZURE_DI_SUBSCRIPTION_HEADER.into(), key.into_value()))
.chain(connection.extra_headers.clone())
.collect(),
);
return Ok(apply_credential(
connection.extra_headers.clone(),
key.value(),
CredentialPlacement::Header(AZURE_DI_SUBSCRIPTION_HEADER),
)?);
}
let token = super::super::common_utils::resolve_entra(auth, config, env_lookup)
.await?
@ -630,11 +630,11 @@ impl AzureDocumentIntelligenceOcrConfig {
guidance: "set AZURE_DOCUMENT_INTELLIGENCE_API_KEY or configure Entra ID",
})?;
super::super::common_utils::validate_destination(connection, token.source())?;
Ok(
std::iter::once(("Authorization".into(), format!("Bearer {}", token.value())))
.chain(connection.extra_headers.clone())
.collect(),
)
Ok(apply_credential(
connection.extra_headers.clone(),
token.value(),
CredentialPlacement::Bearer,
)?)
}
}
@ -873,9 +873,6 @@ mod tests {
.await
.unwrap();
assert_eq!(
headers[0],
(AZURE_DI_SUBSCRIPTION_HEADER.into(), "request-key".into())
);
assert_eq!(headers[AZURE_DI_SUBSCRIPTION_HEADER], "request-key");
}
}

View file

@ -1,4 +1,5 @@
use litellm_auth::{InputSource, Sourced};
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{CredentialPlacement, InputSource, Sourced, http::apply_credential};
use litellm_auth_azure::{AzureAuthInputs, SECRET_NAMES as AZURE_AUTH_SECRET_NAMES};
use litellm_core_utils::{call_arguments::CallArguments, params::OpaqueParams, url_utils::ApiUrl};
use serde_json::Value;
@ -25,7 +26,7 @@ pub struct AzureAiOcrConfig;
impl BaseOcrConfig for AzureAiOcrConfig {
type OcrParams = OpaqueParams;
type ProviderRequest = MistralOcrRequest;
type Environment = Vec<(String, String)>;
type Environment = HeaderMap;
fn get_supported_ocr_params(&self, model: &str) -> &'static [&'static str] {
MistralOcrConfig.get_supported_ocr_params(model)
@ -85,7 +86,7 @@ impl BaseOcrConfig for AzureAiOcrConfig {
model: &str,
document: OcrDocument,
optional_params: &OpaqueParams,
headers: &[(String, String)],
headers: &HeaderMap,
) -> Result<MistralOcrRequest, Error> {
MistralOcrConfig.transform_ocr_request(model, document, optional_params, headers)
}
@ -95,7 +96,7 @@ impl BaseOcrConfig for AzureAiOcrConfig {
model: &str,
document: OcrDocument,
optional_params: &OpaqueParams,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<MistralOcrRequest, Error> {
let document = inline_remote_document(
@ -143,9 +144,9 @@ impl AzureAiOcrConfig {
connection: &OcrConnection,
config: &AzureAuthInputs,
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
) -> Result<Vec<(String, String)>, Error> {
) -> Result<HeaderMap, Error> {
Self::resolve_api_base(connection.api_base.as_deref(), env_lookup)?;
if litellm_http::request::has_header(&connection.extra_headers, "authorization") {
if connection.extra_headers.contains_key(AUTHORIZATION) {
if config.azure_ad_token_provider.is_some() {
super::common_utils::resolve_entra(auth, config, env_lookup).await?;
}
@ -165,7 +166,7 @@ impl AzureAiOcrConfig {
});
if let Some(key) = key {
super::common_utils::validate_destination(connection, key.source())?;
return Ok(bearer_headers(connection, key.value()));
return bearer_headers(connection, key.value());
}
let key = super::common_utils::resolve_entra(auth, config, env_lookup)
.await?
@ -174,7 +175,7 @@ impl AzureAiOcrConfig {
guidance: "set AZURE_AI_API_KEY or configure Entra ID",
})?;
super::common_utils::validate_destination(connection, key.source())?;
Ok(bearer_headers(connection, key.value()))
bearer_headers(connection, key.value())
}
fn build_ocr_url(
@ -190,10 +191,12 @@ impl AzureAiOcrConfig {
}
}
fn bearer_headers(connection: &OcrConnection, key: &str) -> Vec<(String, String)> {
std::iter::once(("Authorization".into(), format!("Bearer {key}")))
.chain(connection.extra_headers.clone())
.collect()
fn bearer_headers(connection: &OcrConnection, key: &str) -> Result<HeaderMap, Error> {
Ok(apply_credential(
connection.extra_headers.clone(),
key,
CredentialPlacement::Bearer,
)?)
}
fn nonblank(value: Option<String>) -> Option<String> {
@ -250,7 +253,10 @@ mod tests {
#[tokio::test]
async fn supplied_authorization_precedes_keys(connection: OcrConnection) {
let connection = OcrConnection {
extra_headers: vec![("authorization".into(), "Bearer prepared".into())],
extra_headers: litellm_http::request::header_map(&[(
"authorization",
"Bearer prepared",
)]),
..connection
};
assert_eq!(
@ -279,8 +285,8 @@ mod tests {
&|_| { Some("environment-key".into()) }
)
.await
.unwrap()[0],
("Authorization".into(), "Bearer request-key".into())
.unwrap()["authorization"],
"Bearer request-key"
);
}
@ -329,10 +335,7 @@ mod tests {
.await
.unwrap();
assert_eq!(
headers[0],
("Authorization".into(), "Bearer request-key".into())
);
assert_eq!(headers["authorization"], "Bearer request-key");
}
#[tokio::test]
@ -352,7 +355,7 @@ mod tests {
assert_eq!(
headers,
[("Authorization".to_string(), "Bearer env-key".to_string())]
litellm_http::request::header_map(&[("authorization", "Bearer env-key")])
);
assert_eq!(url, "https://env.example/providers/mistral/azure/ocr");
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_llms_types::formats::audio_transcription::AudioTranscriptionResponseData;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
@ -9,7 +10,7 @@ pub struct AudioTranscriptionRequestData {
pub body: Value,
}
pub use crate::base_llm::auth::{Headers, ValidatedEnvironment};
pub use crate::base_llm::auth::ValidatedEnvironment;
pub trait BaseAudioTranscriptionConfig: Sync {
fn secret_names(&self) -> Vec<&'static str>;
@ -52,7 +53,7 @@ pub trait BaseAudioTranscriptionConfig: Sync {
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
model: &str,
optional_params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,

View file

@ -5,12 +5,26 @@
//! that split: `validate_environment` shapes the forwarded headers and names the credential
//! as an [`AuthScheme`], and [`resolve_auth`] turns the scheme into headers and a signer.
use http::{HeaderMap, HeaderValue, header::InvalidHeaderValue};
use litellm_auth::{AuthServices, CredentialPlacement, SecretValue, TokenProviderHandle};
use litellm_auth_aws::{AwsCredentialSource, SigV4Signer};
use litellm_auth_gcp::VertexConfig;
use litellm_http::request::with_header;
pub type Headers = Vec<(String, String)>;
use crate::{Error, ErrorDetail};
/// A header value a config derives from the request or the deployment, which is the caller's
/// fault when it cannot go on the wire.
pub fn header_value<V>(subject: &'static str, value: V) -> Result<HeaderValue, Error>
where
HeaderValue: TryFrom<V, Error = InvalidHeaderValue>,
{
HeaderValue::try_from(value)
.map_err(|error| Error::InvalidRequest(ErrorDetail::invalid(subject, error)))
}
pub fn header_str<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {
headers.get(name).and_then(|value| value.to_str().ok())
}
#[derive(Clone, Debug)]
pub enum AuthScheme {
@ -41,13 +55,13 @@ pub enum AuthScheme {
/// call authenticates.
#[derive(Clone, Debug)]
pub struct ValidatedEnvironment {
pub headers: Headers,
pub headers: HeaderMap,
pub auth: AuthScheme,
}
#[derive(Debug)]
pub struct Authenticated {
pub headers: Headers,
pub headers: HeaderMap,
pub signer: Option<SigV4Signer>,
}
@ -63,7 +77,7 @@ pub async fn resolve_auth(
signer: None,
}),
AuthScheme::Credential { placement, secret } => Ok(Authenticated {
headers: with_credential(headers, placement, secret.expose()),
headers: with_credential(headers, placement, secret.expose())?,
signer: None,
}),
AuthScheme::Token { provider } => {
@ -73,14 +87,14 @@ pub async fn resolve_auth(
headers,
CredentialPlacement::Bearer,
token.secret().expose(),
),
)?,
signer: None,
})
}
AuthScheme::GcpAccessToken { config } => {
let token = services.gcp.access_token(&config, env_lookup).await?;
Ok(Authenticated {
headers: with_credential(headers, CredentialPlacement::Bearer, &token),
headers: with_credential(headers, CredentialPlacement::Bearer, &token)?,
signer: None,
})
}
@ -98,13 +112,14 @@ pub async fn resolve_auth(
}
}
fn with_credential(headers: Headers, placement: CredentialPlacement, credential: &str) -> Headers {
let name = placement.header_name();
let value = match placement {
CredentialPlacement::Bearer => format!("Bearer {credential}"),
CredentialPlacement::Header(_) => credential.to_string(),
};
with_header(headers, name, value)
fn with_credential(
mut headers: HeaderMap,
placement: CredentialPlacement,
credential: &str,
) -> Result<HeaderMap, litellm_auth::Error> {
let (name, value) = placement.header(credential)?;
headers.insert(name, value);
Ok(headers)
}
#[cfg(test)]
@ -113,6 +128,7 @@ mod tests {
use litellm_auth::{AuthServices, ResolvedCredential, TokenFuture, TokenProvider};
use litellm_auth_aws::Credentials;
use litellm_http::request::header_map as headers;
use rstest::rstest;
use super::*;
@ -121,14 +137,7 @@ mod tests {
None
}
fn headers(pairs: &[(&str, &str)]) -> Headers {
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
}
async fn resolve(headers: Headers, auth: AuthScheme) -> Authenticated {
async fn resolve(headers: HeaderMap, auth: AuthScheme) -> Authenticated {
resolve_auth(
&AuthServices::default(),
ValidatedEnvironment { headers, auth },

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_core_utils::decline::Decline;
use litellm_llms_types::formats::chat_completions::{
ChatCompletionsResponse, ChatMessage, ChatMessageContent,
@ -28,7 +29,7 @@ pub const STREAM_PARAM: &str = "stream";
/// presence does not make a request untranslatable.
const IGNORABLE_MESSAGE_FIELDS: &[&str] = &["name"];
pub use crate::base_llm::auth::{Headers, ValidatedEnvironment};
pub use crate::base_llm::auth::ValidatedEnvironment;
pub trait BaseConfig: Sync {
fn secret_names(&self) -> Vec<&'static str>;
@ -67,7 +68,7 @@ pub trait BaseConfig: Sync {
/// for every config.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
model: &str,
optional_params: &Map<String, Value>,

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_llms_types::formats::messages::{
MessagesOptionalParams, MessagesRequest, MessagesResponse,
};
@ -6,7 +7,7 @@ use serde_json::Value;
use super::context::MessagesTransformContext;
pub use crate::base_llm::auth::{Headers, ValidatedEnvironment};
pub use crate::base_llm::auth::ValidatedEnvironment;
use crate::{Error, base_llm::messages::streaming::StreamDecoder};
pub trait BaseMessagesConfig: Sync {
@ -54,7 +55,7 @@ pub trait BaseMessagesConfig: Sync {
/// for every config.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
model: &str,
optional_params: &MessagesOptionalParams,
@ -73,8 +74,12 @@ pub trait BaseMessagesConfig: Sync {
&[("content-type", "application/json")]
}
fn request_headers(&self, headers: Headers, _request: &MessagesRequest) -> Headers {
headers
fn request_headers(
&self,
headers: HeaderMap,
_request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
Ok(headers)
}
/// The JSON that goes on the wire, for a host whose body differs from the typed request:
@ -88,6 +93,7 @@ pub trait BaseMessagesConfig: Sync {
mod tests {
use super::*;
use crate::base_llm::auth::AuthScheme;
use litellm_http::request::header_map as headers;
use rstest::rstest;
struct DefaultsConfig;
@ -110,7 +116,7 @@ mod tests {
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
_api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
@ -135,7 +141,7 @@ mod tests {
.unwrap();
assert_eq!(
DefaultsConfig.request_headers(headers(&[("x-api-key", "sk")]), &request),
headers(&[("x-api-key", "sk")])
Ok(headers(&[("x-api-key", "sk")]))
);
}
@ -159,11 +165,4 @@ mod tests {
&[("content-type", "application/json")]
);
}
fn headers(pairs: &[(&str, &str)]) -> Headers {
pairs
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect()
}
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use std::sync::Arc;
use bytes::{Bytes, BytesMut};
@ -8,7 +9,6 @@ use litellm_http::{
Client, ClientVariant, HttpClientConfig, HttpClientPool,
media::{MediaFetcher, UrlPolicy},
outbound::{RequestSigner, json, signed},
request::header_pairs,
};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;
@ -128,7 +128,7 @@ pub async fn ocr<C: BaseOcrConfig>(
) -> Result<LiteLLMOcrResponse, Error> {
let http = config.prepare_request(request, client, hooks).await?;
let url = http.url().to_string();
let headers = header_pairs(http.headers());
let headers = http.headers().clone();
let response = client
.provider_http()
.execute(http)
@ -207,7 +207,7 @@ pub async fn transform_request_body<C: BaseOcrConfig, B: Serialize>(
config: &C,
request: &PreparedOcrRequest,
url: &str,
headers: &[(String, String)],
headers: &HeaderMap,
body: B,
signer: Option<&dyn RequestSigner>,
hooks: &dyn CallHooks<Error>,
@ -230,7 +230,7 @@ pub async fn transform_request_body<C: BaseOcrConfig, B: Serialize>(
config.validate_request_body(&changed.body)?;
let built = json(
url,
&changed.headers,
changed.headers,
&changed.body,
Some(request.connection.timeout),
)?;
@ -240,10 +240,10 @@ pub async fn transform_request_body<C: BaseOcrConfig, B: Serialize>(
})
}
fn wire_request(url: &str, headers: &[(String, String)], body: Value) -> WireRequest {
fn wire_request(url: &str, headers: &HeaderMap, body: Value) -> WireRequest {
WireRequest {
url: url.into(),
headers: headers.to_vec(),
headers: headers.clone(),
body,
}
}
@ -251,23 +251,18 @@ fn wire_request(url: &str, headers: &[(String, String)], body: Value) -> WireReq
pub fn build_http_request(
request: &PreparedOcrRequest,
url: String,
headers: Vec<(String, String)>,
headers: HeaderMap,
body: &impl Serialize,
) -> Result<reqwest::Request, Error> {
Ok(json(
&url,
&headers,
body,
Some(request.connection.timeout),
)?)
Ok(json(&url, headers, body, Some(request.connection.timeout))?)
}
pub async fn guardrail_document(
request: &PreparedOcrRequest,
url: &str,
headers: &[(String, String)],
headers: &HeaderMap,
hooks: &dyn CallHooks<Error>,
) -> Result<(OcrDocument, Vec<(String, String)>), Error> {
) -> Result<(OcrDocument, HeaderMap), Error> {
let body = serde_json::to_value(&request.document)
.map_err(|error| Error::request_field("document", error))?;
let changed = hooks

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use serde::{
Serialize,
de::{DeserializeOwned, IntoDeserializer},
@ -49,7 +50,7 @@ impl OcrCredentialInputs {
#[derive(Clone)]
pub struct OcrTransportConfig {
pub extra_headers: Vec<(String, String)>,
pub extra_headers: HeaderMap,
pub extra_headers_source: InputSource,
pub timeout: Option<Duration>,
pub max_response_bytes: usize,
@ -58,7 +59,7 @@ pub struct OcrTransportConfig {
impl Default for OcrTransportConfig {
fn default() -> Self {
Self {
extra_headers: Vec::new(),
extra_headers: HeaderMap::new(),
extra_headers_source: InputSource::Deployment,
timeout: None,
max_response_bytes: OCR_RESPONSE_MAX_BYTES,
@ -69,7 +70,7 @@ impl Default for OcrTransportConfig {
impl OcrTransportConfig {
pub fn with_overrides(
self,
extra_headers: Vec<(String, String)>,
extra_headers: HeaderMap,
extra_headers_source: InputSource,
timeout: Option<Duration>,
) -> Self {
@ -94,7 +95,7 @@ pub struct OcrConnection {
pub api_key_source: InputSource,
pub api_base: Option<String>,
pub api_base_source: InputSource,
pub extra_headers: Vec<(String, String)>,
pub extra_headers: HeaderMap,
pub extra_headers_source: InputSource,
pub timeout: Duration,
pub max_response_bytes: usize,
@ -234,15 +235,15 @@ const HEALTH_CHECK_PDF_DATA_URI: &str = "data:application/pdf;base64,JVBERi0xLjQ
/// Output of `validate_environment`: whatever a provider resolves up front
/// (headers at minimum; Vertex also carries the project id).
pub trait OcrEnvironment: Send + Sync {
fn headers(&self) -> &[(String, String)];
fn headers(&self) -> &HeaderMap;
fn signer(&self) -> Option<&dyn RequestSigner> {
None
}
}
impl OcrEnvironment for Vec<(String, String)> {
fn headers(&self) -> &[(String, String)] {
impl OcrEnvironment for HeaderMap {
fn headers(&self) -> &HeaderMap {
self
}
}
@ -260,7 +261,7 @@ pub struct OcrResponseContext<'a> {
pub hooks: &'a dyn CallHooks<Error>,
pub request_format: OcrResponseFormat,
pub url: &'a str,
pub headers: &'a [(String, String)],
pub headers: &'a HeaderMap,
}
pub trait BaseOcrConfig: Send + Sync + Sized + 'static {
@ -322,7 +323,7 @@ pub trait BaseOcrConfig: Send + Sync + Sized + 'static {
model: &str,
document: OcrDocument,
optional_params: &Self::OcrParams,
headers: &[(String, String)],
headers: &HeaderMap,
) -> Result<Self::ProviderRequest, Error>;
fn async_transform_ocr_request(
@ -330,7 +331,7 @@ pub trait BaseOcrConfig: Send + Sync + Sized + 'static {
model: &str,
document: OcrDocument,
optional_params: &Self::OcrParams,
headers: &[(String, String)],
headers: &HeaderMap,
_context: OcrRequestContext<'_>,
) -> impl Future<Output = Result<Self::ProviderRequest, Error>> + Send {
async move { self.transform_ocr_request(model, document, optional_params, headers) }

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_llms_types::formats::responses::{
ResponsesApiResponse, streaming::ResponsesStreamEvent,
};
@ -15,7 +16,7 @@ pub trait BaseResponsesApiConfig: Sync {
fn validate_environment(
&self,
headers: Vec<(String, String)>,
headers: HeaderMap,
api_key: Option<&str>,
lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error>;

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::AwsParams;
use litellm_auth_aws::{
AwsCredentialSource, bedrock_model_id_and_region,
@ -17,8 +18,7 @@ use crate::{
Error, ErrorDetail,
base_llm::{
audio_transcription::transformation::{
AudioTranscriptionRequestData, BaseAudioTranscriptionConfig, Headers,
ValidatedEnvironment,
AudioTranscriptionRequestData, BaseAudioTranscriptionConfig, ValidatedEnvironment,
},
auth::AuthScheme,
},
@ -168,12 +168,12 @@ impl BaseAudioTranscriptionConfig for BedrockAudioTranscriptionConfig {
}
fn default_headers(&self) -> &'static [(&'static str, &'static str)] {
&[("Content-Type", "application/json")]
&[("content-type", "application/json")]
}
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
model: &str,
optional_params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::AwsParams;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_auth_aws::{
@ -26,7 +27,7 @@ use crate::{
chat::{
streaming::StreamShape,
transformation::{
BaseConfig, Headers, ProviderChatRequestData, ProviderChatResponseData,
BaseConfig, ProviderChatRequestData, ProviderChatResponseData,
ValidatedEnvironment, unsupported_message, unsupported_param,
},
},
@ -221,7 +222,7 @@ impl BaseConfig for AmazonConverseConfig {
/// principal instead, which is the identity swap this branch exists to prevent.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
model: &str,
optional_params: &Map<String, Value>,
@ -254,7 +255,7 @@ impl BaseConfig for AmazonConverseConfig {
}
fn default_headers(&self) -> &'static [(&'static str, &'static str)] {
&[("Content-Type", "application/json")]
&[("content-type", "application/json")]
}
fn config_params(&self) -> &'static [&'static str] {
@ -646,7 +647,7 @@ mod tests {
fn signs_with_sigv4_in_the_resolved_region() {
let validated = BEDROCK_CHAT_COMPLETIONS_CONFIG
.validate_environment(
Vec::new(),
http::HeaderMap::new(),
None,
"eu-central-1/anthropic.claude-v2",
&Map::new(),
@ -672,7 +673,7 @@ mod tests {
bearer_or_region(
BEDROCK_CHAT_COMPLETIONS_CONFIG
.validate_environment(
Vec::new(),
http::HeaderMap::new(),
api_key,
"eu-central-1/anthropic.claude-v2",
&Map::new(),

View file

@ -1,9 +1,10 @@
use std::sync::LazyLock;
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{AwsParams, CredentialPlacement, ParamKind, ParamSpec, 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_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::{BetaProvider, DEFAULT_HEADERS, MESSAGES_PATH},
@ -18,7 +19,7 @@ use crate::{
transformation::{transform_messages_request, update_headers_with_safeguards_beta},
},
base_llm::{
auth::{AuthScheme, Headers, ValidatedEnvironment},
auth::{AuthScheme, ValidatedEnvironment, header_str, header_value},
messages::{context::MessagesTransformContext, transformation::BaseMessagesConfig},
},
bedrock::common_utils::validated_region,
@ -58,14 +59,14 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
fn validate_environment(
&self,
headers: Headers,
mut headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
optional_params: &MessagesOptionalParams,
params: &LitellmParams,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
let workspace = header_value(&headers, WORKSPACE_HEADER)
let workspace = header_str(&headers, WORKSPACE_HEADER)
.filter(|value| !value.trim().is_empty())
.or_else(|| {
WORKSPACE_FIELDS.iter().find_map(|field| {
@ -75,18 +76,19 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
});
let workspace = resolve_non_empty(workspace, env, WORKSPACE_ENVS)
.ok_or_else(|| Error::Auth(WORKSPACE.missing("Claude Platform on AWS")))?;
let headers = with_header(headers, WORKSPACE_HEADER, workspace);
headers.insert(WORKSPACE_HEADER, header_value(WORKSPACE_HEADER, workspace)?);
let key = resolve_non_empty(api_key, env, &["ANTHROPIC_AWS_API_KEY"]);
let headers = without_headers(headers, &["authorization"]);
if header_value(&headers, "x-api-key").is_some_and(|value| !value.trim().is_empty()) {
headers.remove(AUTHORIZATION);
if header_str(&headers, "x-api-key").is_some_and(|value| !value.trim().is_empty()) {
return Ok(ValidatedEnvironment {
headers,
auth: AuthScheme::Forwarded,
});
}
headers.remove("x-api-key");
if let Some(key) = key {
return Ok(ValidatedEnvironment {
headers: without_headers(headers, &["x-api-key"]),
headers,
auth: AuthScheme::Credential {
placement: CredentialPlacement::Header("x-api-key"),
secret: SecretValue::new(key),
@ -95,7 +97,7 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
}
let base = resolve_non_empty(params.api_base.as_deref(), env, BASE_ENVS);
Ok(ValidatedEnvironment {
headers: without_headers(headers, &["x-api-key"]),
headers,
auth: AuthScheme::AwsSigV4 {
region: platform_region(base.as_deref(), params, env)?,
service: SERVICE,
@ -161,8 +163,12 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
let headers = match WORKSPACE_FIELDS.iter().find_map(|field| {
fn request_headers(
&self,
mut headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
if let Some(workspace) = WORKSPACE_FIELDS.iter().find_map(|field| {
request
.params
.extra
@ -170,9 +176,8 @@ impl BaseMessagesConfig for BedrockClaudePlatformMessagesConfig {
.as_str()
.filter(|value| !value.trim().is_empty())
}) {
Some(workspace) => with_header(headers, WORKSPACE_HEADER, workspace.into()),
None => headers,
};
headers.insert(WORKSPACE_HEADER, header_value(WORKSPACE_HEADER, workspace)?);
}
update_headers_with_safeguards_beta(headers, request, BetaProvider::Anthropic)
}
@ -251,6 +256,7 @@ fn invalid_request(error: serde_json::Error) -> Error {
#[cfg(test)]
mod tests {
use super::*;
use litellm_http::request::header_map;
use litellm_llms_types::providers::anthropic::AnthropicBeta;
use rstest::rstest;
use serde_json::json;
@ -260,7 +266,7 @@ mod tests {
assert_eq!(
BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG
.validate_environment(
vec![],
HeaderMap::new(),
Some("key"),
"claude-test",
&MessagesOptionalParams::default(),
@ -278,10 +284,7 @@ mod tests {
fn forwarded_keys_win_and_blank_keys_fall_back(#[case] key: &str, #[case] forwarded: bool) {
let environment = BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG
.validate_environment(
vec![
("X-Api-Key".into(), key.into()),
("authorization".into(), "stale".into()),
],
header_map(&[("X-Api-Key", key), ("authorization", "stale")]),
Some("explicit"),
"claude-test",
&MessagesOptionalParams::default(),
@ -290,15 +293,15 @@ mod tests {
)
.unwrap();
assert_eq!(
header_value(&environment.headers, WORKSPACE_HEADER),
header_str(&environment.headers, WORKSPACE_HEADER),
Some("workspace")
);
assert_eq!(header_value(&environment.headers, "authorization"), None);
assert_eq!(header_str(&environment.headers, "authorization"), None);
if forwarded {
assert_eq!(header_value(&environment.headers, "x-api-key"), Some(key));
assert_eq!(header_str(&environment.headers, "x-api-key"), Some(key));
assert!(matches!(environment.auth, AuthScheme::Forwarded));
} else {
assert_eq!(header_value(&environment.headers, "x-api-key"), None);
assert_eq!(header_str(&environment.headers, "x-api-key"), None);
assert!(
matches!(environment.auth, AuthScheme::Credential { secret, placement: CredentialPlacement::Header("x-api-key") } if secret.expose() == "explicit")
);
@ -351,7 +354,7 @@ mod tests {
.unwrap();
let environment = BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG
.validate_environment(
vec![],
HeaderMap::new(),
None,
"claude-test",
&MessagesOptionalParams::default(),
@ -376,9 +379,11 @@ mod tests {
#[case] expected: Option<&str>,
) {
let request = serde_json::from_value(json!({"model": "claude-test", "max_tokens": 16, "messages": [], "safeguards": safeguards})).unwrap();
let headers = BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG.request_headers(vec![], &request);
let headers = BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG
.request_headers(HeaderMap::new(), &request)
.unwrap();
assert_eq!(
header_value(&headers, "anthropic-beta").map(|value| value.to_string()),
header_str(&headers, "anthropic-beta").map(|value| value.to_string()),
expected.map(str::to_string)
);
}
@ -404,10 +409,12 @@ mod tests {
let transformed = BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG
.transform_anthropic_messages_request(request, &MessagesTransformContext::default())
.unwrap();
let headers = BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG.request_headers(vec![], &transformed);
let headers = BEDROCK_CLAUDE_PLATFORM_MESSAGES_CONFIG
.request_headers(HeaderMap::new(), &transformed)
.unwrap();
let expected_beta = AnthropicBeta::ContextManagement20250627.to_string();
assert_eq!(
header_value(&headers, "anthropic-beta")
header_str(&headers, "anthropic-beta")
.is_some_and(|value| value.split(',').any(|beta| beta == expected_beta)),
keep_context
);

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_core_utils::decline::Decline;
use std::convert::Infallible;
@ -33,7 +34,7 @@ use crate::{
base_model_iterator::{StreamError, StreamTransformer, transform_stream},
messages::{
streaming::{ByteStream, EventStream, StreamDecoder},
transformation::{BaseMessagesConfig, Headers, ValidatedEnvironment},
transformation::{BaseMessagesConfig, ValidatedEnvironment},
},
},
bedrock::chat::invoke_handler::{decode_invoke_anthropic_chunk, invoke_chunk_stream},
@ -144,7 +145,7 @@ impl BaseMessagesConfig for AmazonAnthropicClaudeMessagesConfig {
/// caller passed none. Without one the request is signed with SigV4.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
model: &str,
_optional_params: &MessagesOptionalParams,
@ -617,7 +618,7 @@ mod tests {
};
let validated = AmazonAnthropicClaudeMessagesConfig
.validate_environment(
Vec::new(),
http::HeaderMap::new(),
None,
"anthropic.claude-3",
&MessagesOptionalParams::default(),
@ -658,7 +659,7 @@ mod tests {
};
let validated = AmazonAnthropicClaudeMessagesConfig
.validate_environment(
vec![("authorization".into(), "Bearer forwarded".into())],
litellm_http::request::header_map(&[("authorization", "Bearer forwarded")]),
api_key,
"anthropic.claude-3",
&MessagesOptionalParams::default(),

View file

@ -1,13 +1,14 @@
use std::sync::LazyLock;
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{AwsParams, 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_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::{AnthropicBeta, BetaProvider, BetaSet, DEFAULT_HEADERS},
providers::anthropic::{AnthropicBeta, BETA_HEADER, BetaProvider, BetaSet, DEFAULT_HEADERS},
};
use litellm_router_types::LitellmParams;
use serde_json::Value;
@ -22,16 +23,17 @@ use crate::{
},
},
base_llm::{
auth::AuthScheme,
auth::{AuthScheme, header_value},
messages::{
context::MessagesTransformContext,
transformation::{BaseMessagesConfig, Headers, ValidatedEnvironment},
transformation::{BaseMessagesConfig, ValidatedEnvironment},
},
},
bedrock::common_utils::validated_region,
};
const API_BASE_ENV: &str = "BEDROCK_MANTLE_API_BASE";
const WORKSPACE_HEADER: &str = "anthropic-workspace-id";
const REGION_ENVS: &[&str] = &[
"BEDROCK_MANTLE_REGION",
"AWS_REGION_NAME",
@ -56,28 +58,22 @@ pub const BEDROCK_MANTLE_MESSAGES_CONFIG: BedrockMantleAnthropicMessagesConfig =
impl BaseMessagesConfig for BedrockMantleAnthropicMessagesConfig {
fn validate_environment(
&self,
headers: Headers,
mut headers: HeaderMap,
api_key: Option<&str>,
model: &str,
_optional_params: &MessagesOptionalParams,
params: &LitellmParams,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
let headers: Headers = headers
.into_iter()
.filter(|(name, _)| {
!name.eq_ignore_ascii_case("authorization")
&& !name.eq_ignore_ascii_case("x-api-key")
})
.collect();
let headers = match params
headers.remove(AUTHORIZATION);
headers.remove("x-api-key");
if let Some(project) = params
.extra
.get("aws_bedrock_project_id")
.and_then(Value::as_str)
{
Some(project) => with_header(headers, "anthropic-workspace-id", project.to_string()),
None => headers,
};
headers.insert(WORKSPACE_HEADER, header_value(WORKSPACE_HEADER, project)?);
}
let auth = match resolve_non_empty(api_key, env, KEY_ENVS) {
Some(token) => AuthScheme::Credential {
placement: CredentialPlacement::Bearer,
@ -178,7 +174,11 @@ impl BaseMessagesConfig for BedrockMantleAnthropicMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
mut headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
let body_betas: BetaSet = request
.params
.extra
@ -204,8 +204,9 @@ impl BaseMessagesConfig for BedrockMantleAnthropicMessagesConfig {
.then_some(AnthropicBeta::DangerousToolUse20260903)
.into_iter()
.collect();
headers.remove(BETA_HEADER);
merge_beta_headers(
without_headers(headers, &["anthropic-beta"]),
headers,
caller
.union(provider_feature_betas(request, BetaProvider::BedrockMantle))
.union(safeguards),
@ -263,7 +264,8 @@ fn mantle_region(
#[cfg(test)]
mod tests {
use super::*;
use litellm_http::request::header_value;
use crate::base_llm::auth::header_str;
use litellm_http::request::header_map;
use rstest::rstest;
use serde_json::json;
@ -319,7 +321,7 @@ mod tests {
);
let environment = BEDROCK_MANTLE_MESSAGES_CONFIG
.validate_environment(
vec![],
http::HeaderMap::new(),
None,
model,
&MessagesOptionalParams::default(),
@ -356,7 +358,7 @@ mod tests {
.unwrap();
let environment = BEDROCK_MANTLE_MESSAGES_CONFIG
.validate_environment(
vec![],
http::HeaderMap::new(),
None,
"claude-test",
&MessagesOptionalParams::default(),
@ -410,10 +412,7 @@ mod tests {
) {
let environment = BEDROCK_MANTLE_MESSAGES_CONFIG
.validate_environment(
vec![
("X-Api-Key".into(), "stale".into()),
("Authorization".into(), "stale".into()),
],
header_map(&[("X-Api-Key", "stale"), ("Authorization", "stale")]),
key,
"claude-test",
&MessagesOptionalParams::default(),
@ -478,14 +477,16 @@ mod tests {
#[rstest]
fn caller_betas_are_filtered_renamed_and_merged_with_feature_betas() {
let request: MessagesRequest = serde_json::from_value(json!({"model": "claude-test", "max_tokens": 16, "messages": [], "safeguards": [{"type": "dangerous_tool_use"}]})).unwrap();
let headers = BEDROCK_MANTLE_MESSAGES_CONFIG.request_headers(
vec![(
"Anthropic-Beta".into(),
"advanced-tool-use-2025-11-20,code-execution-2025-08-25,custom-beta".into(),
)],
&request,
);
let actual: std::collections::BTreeSet<&str> = header_value(&headers, "anthropic-beta")
let headers = BEDROCK_MANTLE_MESSAGES_CONFIG
.request_headers(
header_map(&[(
"Anthropic-Beta",
"advanced-tool-use-2025-11-20,code-execution-2025-08-25,custom-beta",
)]),
&request,
)
.unwrap();
let actual: std::collections::BTreeSet<&str> = header_str(&headers, "anthropic-beta")
.unwrap()
.split(',')
.collect();
@ -502,7 +503,7 @@ mod tests {
#[rstest]
#[case::body_only(
json!(["custom-beta", "advanced-tool-use-2025-11-20"]),
vec![],
&[],
vec![
AnthropicBeta::Other("custom-beta".into()),
AnthropicBeta::ToolSearchTool20251019,
@ -510,7 +511,7 @@ mod tests {
)]
#[case::header_and_body(
json!(["custom-b"]),
vec![("Anthropic-Beta".into(), "custom-a".into())],
&[("Anthropic-Beta", "custom-a")],
vec![
AnthropicBeta::Other("custom-a".into()),
AnthropicBeta::Other("custom-b".into()),
@ -518,7 +519,7 @@ mod tests {
)]
fn body_betas_are_filtered_renamed_and_merged_with_headers(
#[case] body_betas: Value,
#[case] headers: Headers,
#[case] headers: &[(&str, &str)],
#[case] expected: Vec<AnthropicBeta>,
) {
let request: MessagesRequest = serde_json::from_value(json!({
@ -528,8 +529,10 @@ mod tests {
"anthropic_beta": body_betas
}))
.unwrap();
let headers = BEDROCK_MANTLE_MESSAGES_CONFIG.request_headers(headers, &request);
let actual: BetaSet = header_value(&headers, "anthropic-beta")
let headers = BEDROCK_MANTLE_MESSAGES_CONFIG
.request_headers(header_map(headers), &request)
.unwrap();
let actual: BetaSet = header_str(&headers, "anthropic-beta")
.unwrap()
.parse()
.unwrap();
@ -537,6 +540,41 @@ mod tests {
assert_eq!(actual, expected);
}
#[rstest]
fn a_body_beta_that_cannot_be_a_header_value_is_the_callers_fault() {
let request: MessagesRequest = serde_json::from_value(json!({
"model": "claude-test",
"max_tokens": 16,
"messages": [],
"anthropic_beta": ["custom\r\nx-injected: 1"]
}))
.unwrap();
let error = BEDROCK_MANTLE_MESSAGES_CONFIG
.request_headers(HeaderMap::new(), &request)
.unwrap_err();
assert!(error.is_request(), "{error:?}");
}
#[rstest]
fn a_project_id_that_cannot_be_a_header_value_is_the_callers_fault() {
let params: LitellmParams = serde_json::from_value(json!({
"model": "claude-test",
"aws_bedrock_project_id": "project\nx-injected: 1",
}))
.unwrap();
let error = BEDROCK_MANTLE_MESSAGES_CONFIG
.validate_environment(
HeaderMap::new(),
Some("key"),
"claude-test",
&MessagesOptionalParams::default(),
&params,
&|_| None,
)
.unwrap_err();
assert!(error.is_request(), "{error:?}");
}
#[rstest]
fn wire_body_keeps_streaming_and_removes_sdk_fields() {
assert_eq!(BEDROCK_MANTLE_MESSAGES_CONFIG.wire_body(json!({"model": "mantle/us-west-2/anthropic.claude-test", "stream": true, "anthropic_version": "sdk", "anthropic_beta": ["sdk"], "messages": []})), json!({"model": "anthropic.claude-test", "stream": true, "messages": []}));

View file

@ -1,3 +1,5 @@
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{CredentialPlacement, http::apply_credential};
use litellm_core_utils::{
call_arguments::{CallArguments, parse_options},
url_utils::ApiUrl,
@ -96,7 +98,7 @@ pub struct CohereParseConfig;
impl BaseOcrConfig for CohereParseConfig {
type OcrParams = CohereOptions;
type ProviderRequest = CohereRequest;
type Environment = Vec<(String, String)>;
type Environment = HeaderMap;
fn get_supported_ocr_params(&self, _model: &str) -> &'static [&'static str] {
&["output_format", "req_format"]
@ -155,7 +157,7 @@ impl BaseOcrConfig for CohereParseConfig {
model: &str,
document: OcrDocument,
optional_params: &CohereOptions,
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<CohereRequest, Error> {
let image_url = image_url(document)?;
Ok(build_request(model, image_url, optional_params))
@ -180,8 +182,8 @@ impl CohereParseConfig {
&self,
connection: &OcrConnection,
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
) -> Result<Vec<(String, String)>, Error> {
if litellm_http::request::has_header(&connection.extra_headers, "authorization") {
) -> Result<HeaderMap, Error> {
if connection.extra_headers.contains_key(AUTHORIZATION) {
return Ok(connection.extra_headers.clone());
}
let key = connection
@ -199,11 +201,11 @@ impl CohereParseConfig {
provider: "Cohere",
environment_variable: COHERE_API_KEY_ENV,
}))?;
Ok(
std::iter::once(("Authorization".into(), format!("Bearer {key}")))
.chain(connection.extra_headers.clone())
.collect(),
)
Ok(apply_credential(
connection.extra_headers.clone(),
&key,
CredentialPlacement::Bearer,
)?)
}
fn build_ocr_url(&self, api_base: &str) -> Result<String, Error> {
@ -469,7 +471,7 @@ mod tests {
)
.unwrap();
let body = CohereParseConfig
.transform_ocr_request("parse", document, &params, &[])
.transform_ocr_request("parse", document, &params, &http::HeaderMap::new())
.unwrap();
assert_eq!(
serde_json::to_value(body).unwrap(),
@ -697,7 +699,7 @@ mod tests {
}))
.unwrap(),
&serde_json::from_value(json!({})).unwrap(),
&[],
&http::HeaderMap::new(),
)
.unwrap();
assert_eq!(
@ -754,7 +756,7 @@ mod tests {
assert_eq!(
headers,
[("Authorization".to_string(), "Bearer env-key".to_string())]
litellm_http::request::header_map(&[("authorization", "Bearer env-key")])
);
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_llms_types::{
@ -18,7 +19,7 @@ use crate::{
},
},
base_llm::{
auth::{AuthScheme, Headers, ValidatedEnvironment},
auth::{AuthScheme, ValidatedEnvironment},
messages::{context::MessagesTransformContext, transformation::BaseMessagesConfig},
},
};
@ -101,7 +102,7 @@ impl BaseMessagesConfig for DeepSeekAnthropicMessagesConfig {
/// key goes in `x-api-key`.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
@ -133,7 +134,11 @@ impl BaseMessagesConfig for DeepSeekAnthropicMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
update_headers_with_anthropic_beta(headers, request)
}
}
@ -293,10 +298,7 @@ mod tests {
env: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
DEEPSEEK_ANTHROPIC_MESSAGES_CONFIG.validate_environment(
forwarded
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect(),
litellm_http::request::header_map(forwarded),
api_key,
"deepseek-v4-pro",
&MessagesOptionalParams::default(),
@ -465,14 +467,11 @@ mod tests {
"messages": [{"role": "user", "content": "hi"}],
"safeguards": safeguards
});
let headers: Headers = client
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect();
let headers: HeaderMap = litellm_http::request::header_map(client);
assert_eq!(
DEEPSEEK_ANTHROPIC_MESSAGES_CONFIG
.request_headers(headers.clone(), &request_from(body.clone())),
headers
Ok(headers)
);
assert_eq!(transformed(body)["safeguards"], safeguards);
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_llms_types::formats::messages::{
@ -17,7 +18,7 @@ use crate::{
auth::AuthScheme,
messages::{
context::MessagesTransformContext,
transformation::{BaseMessagesConfig, Headers, ValidatedEnvironment},
transformation::{BaseMessagesConfig, ValidatedEnvironment},
},
},
openai_like::messages::transformation::{complete_messages_url, portable_cache_control},
@ -33,7 +34,7 @@ pub const EDENAI_MESSAGES_CONFIG: EdenAIAnthropicMessagesConfig = EdenAIAnthropi
impl BaseMessagesConfig for EdenAIAnthropicMessagesConfig {
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
@ -99,7 +100,11 @@ impl BaseMessagesConfig for EdenAIAnthropicMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
update_headers_with_anthropic_beta(headers, request)
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_llms_types::{
@ -16,7 +17,7 @@ use crate::{
},
},
base_llm::{
auth::{AuthScheme, Headers, ValidatedEnvironment},
auth::{AuthScheme, ValidatedEnvironment},
messages::{context::MessagesTransformContext, transformation::BaseMessagesConfig},
},
};
@ -39,7 +40,7 @@ impl BaseMessagesConfig for MinimaxMessagesConfig {
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
@ -110,7 +111,11 @@ impl BaseMessagesConfig for MinimaxMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
update_headers_with_anthropic_beta(headers, request)
}
}
@ -185,7 +190,7 @@ mod tests {
};
let environment = MINIMAX_MESSAGES_CONFIG
.validate_environment(
vec![],
http::HeaderMap::new(),
key,
"test-model",
&MessagesOptionalParams::default(),

View file

@ -1,3 +1,5 @@
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{CredentialPlacement, http::apply_credential};
use litellm_core_utils::{call_arguments::CallArguments, params::OpaqueParams, url_utils::ApiUrl};
use serde::{Deserialize, Serialize};
use serde_json::Value;
@ -47,7 +49,7 @@ pub struct MistralOcrConfig;
impl BaseOcrConfig for MistralOcrConfig {
type OcrParams = OpaqueParams;
type ProviderRequest = MistralOcrRequest;
type Environment = Vec<(String, String)>;
type Environment = HeaderMap;
fn get_supported_ocr_params(&self, _model: &str) -> &'static [&'static str] {
&[
@ -113,7 +115,7 @@ impl BaseOcrConfig for MistralOcrConfig {
model: &str,
document: OcrDocument,
optional_params: &OpaqueParams,
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<MistralOcrRequest, Error> {
Ok(MistralOcrRequest {
model: model.to_string(),
@ -137,8 +139,8 @@ impl MistralOcrConfig {
&self,
connection: &OcrConnection,
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
) -> Result<Vec<(String, String)>, Error> {
if litellm_http::request::has_header(&connection.extra_headers, "authorization") {
) -> Result<HeaderMap, Error> {
if connection.extra_headers.contains_key(AUTHORIZATION) {
return Ok(connection.extra_headers.clone());
}
let api_key = connection
@ -156,11 +158,11 @@ impl MistralOcrConfig {
provider: "Mistral",
environment_variable: MISTRAL_OCR_API_KEY_ENV_VAR,
})?;
Ok(
std::iter::once(("Authorization".into(), format!("Bearer {api_key}")))
.chain(connection.extra_headers.clone())
.collect(),
)
Ok(apply_credential(
connection.extra_headers.clone(),
&api_key,
CredentialPlacement::Bearer,
)?)
}
fn build_ocr_url(&self, api_base: Option<&str>) -> Result<String, Error> {
@ -216,11 +218,11 @@ mod tests {
#[fixture]
fn connection(
#[default(None)] api_key: Option<&str>,
#[default(vec![])] extra_headers: Vec<(String, String)>,
#[default(&[])] extra_headers: &[(&str, &str)],
) -> OcrConnection {
OcrConnection {
api_key: api_key.map(litellm_auth::SecretValue::new),
extra_headers,
extra_headers: litellm_http::request::header_map(extra_headers),
..OcrConnection::default()
}
}
@ -312,7 +314,7 @@ mod tests {
fn request_transform_uses_already_mapped_params_without_filtering_again(document: OcrDocument) {
let params = serde_json::from_value(json!({"extension":{"nested":null}})).unwrap();
let body = MistralOcrConfig
.transform_ocr_request("model", document, &params, &[])
.transform_ocr_request("model", document, &params, &http::HeaderMap::new())
.unwrap();
assert_eq!(
serde_json::to_value(body).unwrap()["extension"],
@ -483,7 +485,7 @@ mod tests {
.unwrap();
let result = serde_json::to_value(
MistralOcrConfig
.transform_ocr_request("model", document.clone(), &params, &[])
.transform_ocr_request("model", document.clone(), &params, &http::HeaderMap::new())
.unwrap(),
)
.unwrap();
@ -509,7 +511,12 @@ mod tests {
let params: OpaqueParams = serde_json::from_value(json!({name:value.clone()})).unwrap();
let result = serde_json::to_value(
MistralOcrConfig
.transform_ocr_request("mistral-ocr-latest", document, &params, &[])
.transform_ocr_request(
"mistral-ocr-latest",
document,
&params,
&http::HeaderMap::new(),
)
.unwrap(),
)
.unwrap();
@ -527,7 +534,12 @@ mod tests {
.unwrap();
let result = serde_json::to_value(
MistralOcrConfig
.transform_ocr_request("mistral-ocr-latest", document, &params, &[])
.transform_ocr_request(
"mistral-ocr-latest",
document,
&params,
&http::HeaderMap::new(),
)
.unwrap(),
)
.unwrap();
@ -614,15 +626,14 @@ mod tests {
assert_eq!(
MistralOcrConfig
.resolve_headers(&connection, &|_| Some("environment".into()))
.unwrap()[0],
("Authorization".into(), expected.into())
.unwrap(),
litellm_http::request::header_map(&[("authorization", expected)])
);
}
#[rstest]
fn environment_preserves_forwarded_authorization(
#[with(None, vec![("authorization".into(), "Bearer forwarded".into())])]
connection: OcrConnection,
#[with(None, &[("authorization", "Bearer forwarded")])] connection: OcrConnection,
) {
assert_eq!(
MistralOcrConfig
@ -634,17 +645,16 @@ mod tests {
#[rstest]
fn environment_keeps_extra_headers_after_the_bearer_key(
#[with(Some("explicit"), vec![("X-Trace".into(), "trace-1".into())])]
connection: OcrConnection,
#[with(Some("explicit"), &[("X-Trace", "trace-1")])] connection: OcrConnection,
) {
assert_eq!(
MistralOcrConfig
.resolve_headers(&connection, &|_| None)
.unwrap(),
[
("Authorization".to_string(), "Bearer explicit".to_string()),
("X-Trace".to_string(), "trace-1".to_string()),
]
litellm_http::request::header_map(&[
("authorization", "Bearer explicit"),
("x-trace", "trace-1"),
])
);
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_llms_types::formats::responses::{
ResponsesApiResponse, streaming::ResponsesStreamEvent,
};
@ -120,7 +121,7 @@ impl BaseResponsesApiConfig for OpenAiResponsesApiConfig {
fn validate_environment(
&self,
headers: Vec<(String, String)>,
headers: HeaderMap,
api_key: Option<&str>,
lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {

View file

@ -4,6 +4,7 @@
//! the `max_completion_tokens` -> `max_tokens` rename and the usage
//! `*_tokens` null-to-zero sanitize.
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::core_helpers::unix_now;
use litellm_llms_types::formats::chat_completions::{
@ -17,8 +18,7 @@ use crate::{
base_llm::{
auth::AuthScheme,
chat::transformation::{
BaseConfig, Headers, ProviderChatRequestData, ProviderChatResponseData,
ValidatedEnvironment,
BaseConfig, ProviderChatRequestData, ProviderChatResponseData, ValidatedEnvironment,
},
},
openai_like::common_utils::{complete_openai_like_url, openai_compatible_provider_info},
@ -188,16 +188,13 @@ impl BaseConfig for OpenAILikeChatConfig {
/// endpoints take no key; Python still sends `Bearer ` in that case.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &Map<String, Value>,
env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
if headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case("authorization"))
{
if headers.contains_key(AUTHORIZATION) {
return Ok(ValidatedEnvironment {
headers,
auth: AuthScheme::Forwarded,
@ -438,7 +435,7 @@ mod tests {
fn the_resolved_key_authenticates_as_a_bearer() {
let validated = OPENAI_LIKE_CHAT_COMPLETIONS_CONFIG
.validate_environment(
vec![],
http::HeaderMap::new(),
Some("sk-test"),
"my-model",
&params(json!({})),
@ -458,7 +455,7 @@ mod tests {
fn the_key_falls_back_to_the_environment() {
let validated = OPENAI_LIKE_CHAT_COMPLETIONS_CONFIG
.validate_environment(
vec![],
http::HeaderMap::new(),
None,
"my-model",
&params(json!({})),
@ -475,7 +472,7 @@ mod tests {
fn a_forwarded_authorization_is_the_whole_credential() {
// Python adds `Bearer <key>` only when the caller did not already send
// `Authorization`, so the forwarded header wins over the deployment key.
let headers = vec![("Authorization".to_string(), "Bearer caller".to_string())];
let headers = litellm_http::request::header_map(&[("Authorization", "Bearer caller")]);
let validated = OPENAI_LIKE_CHAT_COMPLETIONS_CONFIG
.validate_environment(
headers,
@ -493,7 +490,13 @@ mod tests {
// vllm-compatible endpoints require no api key; Python resolves `""` and
// sends `Bearer `, so validation must not fail on the missing key.
let validated = OPENAI_LIKE_CHAT_COMPLETIONS_CONFIG
.validate_environment(vec![], None, "my-model", &params(json!({})), &no_env)
.validate_environment(
http::HeaderMap::new(),
None,
"my-model",
&params(json!({})),
&no_env,
)
.expect("validates");
let AuthScheme::Credential { secret, .. } = validated.auth else {
panic!("expected a bearer credential");

View file

@ -1,6 +1,7 @@
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_http::request::{has_bearer_auth, without_headers};
use litellm_http::request::has_bearer_auth;
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest},
providers::anthropic::DEFAULT_HEADERS,
@ -14,7 +15,7 @@ use crate::{
messages::transformation::update_headers_with_anthropic_beta,
},
base_llm::{
auth::{AuthScheme, Headers, ValidatedEnvironment},
auth::{AuthScheme, ValidatedEnvironment},
messages::{context::MessagesTransformContext, transformation::BaseMessagesConfig},
},
};
@ -31,14 +32,14 @@ pub const OPENROUTER_MESSAGES_CONFIG: OpenRouterAnthropicMessagesConfig =
impl BaseMessagesConfig for OpenRouterAnthropicMessagesConfig {
fn validate_environment(
&self,
headers: Headers,
mut headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
_params: &LitellmParams,
env: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
let headers = without_headers(headers, &["x-api-key"]);
headers.remove("x-api-key");
if has_bearer_auth(&headers) {
return Ok(ValidatedEnvironment {
headers,
@ -51,8 +52,9 @@ impl BaseMessagesConfig for OpenRouterAnthropicMessagesConfig {
environment_variable: API_KEY_ENV,
},
))?;
headers.remove(AUTHORIZATION);
Ok(ValidatedEnvironment {
headers: without_headers(headers, &["authorization"]),
headers,
auth: AuthScheme::Credential {
placement: CredentialPlacement::Bearer,
secret: SecretValue::new(key),
@ -108,7 +110,11 @@ impl BaseMessagesConfig for OpenRouterAnthropicMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
update_headers_with_anthropic_beta(headers, request)
}
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_llms_types::formats::responses::ResponsesApiResponse;
@ -39,7 +40,7 @@ impl BaseResponsesApiConfig for OpenRouterResponsesApiConfig {
fn validate_environment(
&self,
headers: Vec<(String, String)>,
headers: HeaderMap,
api_key: Option<&str>,
lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
@ -128,7 +129,7 @@ mod tests {
#[case] expected: &str,
) {
let environment = OPENROUTER_RESPONSES_CONFIG
.validate_environment(vec![], api_key, &env(secrets))
.validate_environment(http::HeaderMap::new(), api_key, &env(secrets))
.unwrap();
let AuthScheme::Credential { placement, secret } = environment.auth else {
panic!("expected a credential");
@ -137,7 +138,7 @@ mod tests {
assert_eq!(secret.expose(), expected);
assert_eq!(
environment.headers,
vec![("content-type".to_owned(), "application/json".to_owned())]
litellm_http::request::header_map(&[("content-type", "application/json")])
);
}
@ -148,7 +149,11 @@ mod tests {
#[case] secrets: &'static [(&'static str, &'static str)],
) {
assert!(matches!(
OPENROUTER_RESPONSES_CONFIG.validate_environment(vec![], None, &env(secrets)),
OPENROUTER_RESPONSES_CONFIG.validate_environment(
http::HeaderMap::new(),
None,
&env(secrets)
),
Err(Error::Auth(litellm_auth::Error::MissingApiKey {
provider: "OpenRouter",
environment_variable: "OPENROUTER_API_KEY",

View file

@ -1,5 +1,8 @@
use std::collections::BTreeMap;
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::{CredentialPlacement, http::apply_credential};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value, json};
@ -91,7 +94,7 @@ pub struct ReductoParseV3Config;
impl BaseOcrConfig for ReductoParseV3Config {
type OcrParams = ReductoV3Params;
type ProviderRequest = ReductoV3Request;
type Environment = Vec<(String, String)>;
type Environment = HeaderMap;
fn secret_names(&self) -> Vec<&'static str> {
vec![REDUCTO_API_KEY_ENV]
@ -135,7 +138,7 @@ impl BaseOcrConfig for ReductoParseV3Config {
_model: &str,
document: OcrDocument,
optional_params: &Self::OcrParams,
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<Self::ProviderRequest, Error> {
Ok(ReductoV3Request {
input: uploaded_file_id(document)?,
@ -148,7 +151,7 @@ impl BaseOcrConfig for ReductoParseV3Config {
_model: &str,
document: OcrDocument,
optional_params: &ReductoV3Params,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<ReductoV3Request, Error> {
let file_id = ensure_file_id_async(document, headers, context).await?;
@ -183,7 +186,7 @@ pub struct ReductoParseLegacyConfig;
impl BaseOcrConfig for ReductoParseLegacyConfig {
type OcrParams = ReductoLegacyParams;
type ProviderRequest = ReductoLegacyRequest;
type Environment = Vec<(String, String)>;
type Environment = HeaderMap;
fn secret_names(&self) -> Vec<&'static str> {
vec![REDUCTO_API_KEY_ENV]
@ -227,7 +230,7 @@ impl BaseOcrConfig for ReductoParseLegacyConfig {
_model: &str,
document: OcrDocument,
optional_params: &Self::OcrParams,
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<Self::ProviderRequest, Error> {
Ok(build_legacy_body(
uploaded_file_id(document)?,
@ -240,7 +243,7 @@ impl BaseOcrConfig for ReductoParseLegacyConfig {
_model: &str,
document: OcrDocument,
optional_params: &ReductoLegacyParams,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<ReductoLegacyRequest, Error> {
let file_id = ensure_file_id_async(document, headers, context).await?;
@ -269,7 +272,7 @@ impl BaseOcrConfig for ReductoParseLegacyConfig {
/// Reducto differs from the shared `BaseOcrConfig::prepare_request` flow:
/// guardrails see the *source* document before it is uploaded, because the
/// final body only carries the opaque Reducto file id.
async fn prepare_upload_request<C: BaseOcrConfig<Environment = Vec<(String, String)>>>(
async fn prepare_upload_request<C: BaseOcrConfig<Environment = HeaderMap>>(
config: &C,
request: &PreparedOcrRequest,
client: &OcrClient,
@ -451,8 +454,8 @@ fn complete_endpoint_url(api_base: Option<&str>, path: &str) -> Result<String, E
fn resolve_headers(
connection: &OcrConnection,
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
) -> Result<Vec<(String, String)>, Error> {
if litellm_http::request::has_header(&connection.extra_headers, "authorization") {
) -> Result<HeaderMap, Error> {
if connection.extra_headers.contains_key(AUTHORIZATION) {
return Ok(connection.extra_headers.clone());
}
let api_key = connection
@ -470,11 +473,11 @@ fn resolve_headers(
credential: REDUCTO_API_KEY_ENV,
guidance: "set it in the environment or pass api_key to litellm.ocr()/litellm.aocr()",
})?;
Ok(
std::iter::once(("Authorization".into(), format!("Bearer {api_key}")))
.chain(connection.extra_headers.clone())
.collect(),
)
Ok(apply_credential(
connection.extra_headers.clone(),
&api_key,
CredentialPlacement::Bearer,
)?)
}
fn build_legacy_body(
@ -494,7 +497,7 @@ fn build_legacy_body(
async fn ensure_file_id_async(
document: OcrDocument,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<ReductoFileId, Error> {
if document.source().starts_with(REDUCTO_ID_PREFIX) {
@ -518,7 +521,7 @@ async fn ensure_file_id_async(
async fn upload_bytes_async(
bytes: Vec<u8>,
mime: &str,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<ReductoFileId, Error> {
let OcrRequestContext { client, connection } = context;
@ -589,7 +592,7 @@ mod tests {
"parse-v3",
document,
&params,
&[],
&http::HeaderMap::new(),
OcrRequestContext {
client: &client,
connection: &connection,
@ -694,7 +697,7 @@ mod tests {
..Default::default()
};
let headers = resolve_headers(&connection, &|_| Some("env-key".into())).unwrap();
assert_eq!(headers[0].1, "Bearer passed-key");
assert_eq!(headers[AUTHORIZATION], "Bearer passed-key");
}
#[test]
@ -704,13 +707,16 @@ mod tests {
..Default::default()
};
let headers = resolve_headers(&connection, &|_| Some(" env-key ".into())).unwrap();
assert_eq!(headers[0].1, "Bearer env-key");
assert_eq!(headers[AUTHORIZATION], "Bearer env-key");
}
#[test]
fn existing_authorization_skips_key_lookup() {
let connection = OcrConnection {
extra_headers: vec![("authorization".into(), "Bearer existing".into())],
extra_headers: litellm_http::request::header_map(&[(
"authorization",
"Bearer existing",
)]),
..Default::default()
};
assert_eq!(

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth::{CredentialPlacement, SecretValue};
use litellm_core_utils::settings::resolve_non_empty;
use litellm_llms_types::{
@ -16,7 +17,7 @@ use crate::{
},
},
base_llm::{
auth::{AuthScheme, Headers, ValidatedEnvironment},
auth::{AuthScheme, ValidatedEnvironment},
messages::{context::MessagesTransformContext, transformation::BaseMessagesConfig},
},
};
@ -40,7 +41,7 @@ impl BaseMessagesConfig for TencentAnthropicMessagesConfig {
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
@ -115,7 +116,11 @@ impl BaseMessagesConfig for TencentAnthropicMessagesConfig {
DEFAULT_HEADERS
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
update_headers_with_anthropic_beta(headers, request)
}
}
@ -231,7 +236,7 @@ mod tests {
};
let environment = TENCENT_MESSAGES_CONFIG
.validate_environment(
vec![],
http::HeaderMap::new(),
key,
"test-model",
&MessagesOptionalParams::default(),

View file

@ -1,9 +1,9 @@
use http::{HeaderMap, header::AUTHORIZATION};
use litellm_auth::VertexParams;
use litellm_auth_gcp::{
VertexConfig, get_vertex_ai_location, get_vertex_ai_project,
get_vertex_ai_project_from_credentials, secret_names,
};
use litellm_http::request::has_header;
use litellm_llms_types::{
formats::messages::{MessagesOptionalParams, MessagesRequest, MessagesTool, OutputConfig},
providers::anthropic::{AnthropicBeta, BetaProvider, BetaSet},
@ -22,7 +22,7 @@ use crate::{
},
},
base_llm::{
auth::{AuthScheme, Headers, ValidatedEnvironment},
auth::{AuthScheme, ValidatedEnvironment},
messages::{
context::MessagesTransformContext,
normalization::{normalize_system_role_messages, strip_cache_control_scope},
@ -111,14 +111,14 @@ impl BaseMessagesConfig for VertexAiPartnerModelsAnthropicMessagesConfig {
/// then application default credentials) is acquired when the request is sent.
fn validate_environment(
&self,
headers: Headers,
headers: HeaderMap,
_api_key: Option<&str>,
_model: &str,
_optional_params: &MessagesOptionalParams,
litellm_params: &LitellmParams,
_env_lookup: &dyn Fn(&str) -> Option<String>,
) -> Result<ValidatedEnvironment, Error> {
if has_header(&headers, "authorization") {
if headers.contains_key(AUTHORIZATION) {
return Ok(ValidatedEnvironment {
headers,
auth: AuthScheme::Forwarded,
@ -132,7 +132,11 @@ impl BaseMessagesConfig for VertexAiPartnerModelsAnthropicMessagesConfig {
})
}
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
fn request_headers(
&self,
headers: HeaderMap,
request: &MessagesRequest,
) -> Result<HeaderMap, Error> {
merge_beta_headers(headers, vertex_feature_betas(request))
}
@ -434,10 +438,7 @@ mod tests {
fn validated(forwarded: &[(&str, &str)]) -> ValidatedEnvironment {
VERTEX_ANTHROPIC_MESSAGES_CONFIG
.validate_environment(
forwarded
.iter()
.map(|(name, value)| (name.to_string(), value.to_string()))
.collect(),
litellm_http::request::header_map(forwarded),
Some("ignored-api-key"),
"claude-sonnet-4-5",
&MessagesOptionalParams::default(),
@ -637,10 +638,18 @@ mod tests {
})
.unwrap();
VERTEX_ANTHROPIC_MESSAGES_CONFIG
.request_headers(Vec::new(), &request)
.into_iter()
.filter(|(name, _)| name == "anthropic-beta")
.flat_map(|(_, value)| value.split(',').map(str::to_string).collect::<Vec<_>>())
.request_headers(HeaderMap::new(), &request)
.unwrap()
.get_all("anthropic-beta")
.iter()
.flat_map(|value| {
value
.to_str()
.unwrap()
.split(',')
.map(str::to_string)
.collect::<Vec<_>>()
})
.collect()
}

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth_gcp as vertex;
use litellm_core_utils::{call_arguments::CallArguments, params::OpaqueParams, url_utils::ApiUrl};
use serde::{Deserialize, Serialize};
@ -151,7 +152,7 @@ impl BaseOcrConfig for VertexAIDeepSeekOCRConfig {
model: &str,
document: OcrDocument,
optional_params: &DeepSeekOcrParams,
headers: &[(String, String)],
headers: &HeaderMap,
_context: OcrRequestContext<'_>,
) -> Result<DeepSeekOcrRequest, Error> {
self.transform_ocr_request(model, document, optional_params, headers)
@ -171,7 +172,7 @@ impl BaseOcrConfig for VertexAIDeepSeekOCRConfig {
model: &str,
document: OcrDocument,
optional_params: &DeepSeekOcrParams,
_headers: &[(String, String)],
_headers: &HeaderMap,
) -> Result<DeepSeekOcrRequest, Error> {
if document.source().is_empty() {
return Err(Error::MissingDocumentUrl);
@ -501,7 +502,12 @@ mod tests {
serde_json::from_value(json!({name: value.clone(), "ignored": true})).unwrap();
let result = serde_json::to_value(
VertexAIDeepSeekOCRConfig
.transform_ocr_request("deepseek-ai/deepseek-ocr-maas", document(), &params, &[])
.transform_ocr_request(
"deepseek-ai/deepseek-ocr-maas",
document(),
&params,
&http::HeaderMap::new(),
)
.unwrap(),
)
.unwrap();
@ -523,7 +529,7 @@ mod tests {
"deepseek-ai/deepseek-ocr-maas",
document(),
&params,
&[],
&http::HeaderMap::new(),
)
.unwrap(),
)
@ -546,7 +552,7 @@ mod tests {
"deepseek-ai/deepseek-ocr-maas",
document(),
&DeepSeekOcrParams::default(),
&[],
&http::HeaderMap::new(),
)
.unwrap();
let composed =
@ -568,7 +574,7 @@ mod tests {
"deepseek-ai/deepseek-ocr-maas",
serde_json::from_value(document).unwrap(),
&DeepSeekOcrParams::default(),
&[],
&http::HeaderMap::new(),
)
.unwrap();
let result = serde_json::to_value(request).unwrap();

View file

@ -1,3 +1,4 @@
use http::HeaderMap;
use litellm_auth_gcp::{self as vertex, VertexConfig};
use litellm_core_utils::{call_arguments::CallArguments, params::OpaqueParams, url_utils::ApiUrl};
use serde_json::Value;
@ -79,7 +80,7 @@ impl BaseOcrConfig for VertexAiOcrConfig {
model: &str,
document: OcrDocument,
optional_params: &OpaqueParams,
headers: &[(String, String)],
headers: &HeaderMap,
) -> Result<MistralOcrRequest, Error> {
MistralOcrConfig.transform_ocr_request(model, document, optional_params, headers)
}
@ -89,7 +90,7 @@ impl BaseOcrConfig for VertexAiOcrConfig {
model: &str,
document: OcrDocument,
optional_params: &OpaqueParams,
headers: &[(String, String)],
headers: &HeaderMap,
context: OcrRequestContext<'_>,
) -> Result<MistralOcrRequest, Error> {
let document = inline_remote_document(
@ -116,7 +117,7 @@ impl BaseOcrConfig for VertexAiOcrConfig {
}
impl OcrEnvironment for vertex::VertexEnvironment {
fn headers(&self) -> &[(String, String)] {
fn headers(&self) -> &HeaderMap {
&self.headers
}
}

View file

@ -458,7 +458,7 @@ kwargs = {}
);
assert_eq!(
projected.transport.extra_headers,
[("x-source".to_string(), "original".to_string())]
litellm_http::request::header_map(&[("x-source", "original")])
);
assert_eq!(
projected.transport.timeout,
@ -679,7 +679,7 @@ kwargs = {
);
assert_eq!(
projected.transport.extra_headers,
[("x-source".to_string(), "kwargs".to_string())]
litellm_http::request::header_map(&[("x-source", "kwargs")])
);
assert_eq!(
projected.transport.timeout,