Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

send one authorization header in openai-completions requests (#3757)

kt85755cb8ae11
diff --git a/crates/pa-ai/src/providers/mistral/mod.rs b/crates/pa-ai/src/providers/mistral/mod.rs
index f90e65112c..1662edbdf5 100644
--- a/crates/pa-ai/src/providers/mistral/mod.rs
+++ b/crates/pa-ai/src/providers/mistral/mod.rs
@@ -108,8 +108,12 @@ pub(crate) fn build_request_headers(
options: &MistralOptions,
api_key: &str,
) -> Vec<(String, String)> {
- let mut headers: Vec<(String, String)> = Vec::new();
+ // The default authorization goes in first so model and caller headers replace it (header names
+ // compare case-insensitively) instead of shipping a second Authorization alongside theirs.
+ let mut headers: Vec<(String, String)> =
+ vec![("authorization".into(), format!("Bearer {api_key}"))];
for (name, value) in model.headers.iter().flatten() {
+ headers.retain(|(existing, _)| !existing.eq_ignore_ascii_case(name));
headers.push((name.clone(), value.clone()));
}
if let Some(options_headers) = &options.base.headers {
@@ -125,7 +129,6 @@ pub(crate) fn build_request_headers(
headers.push(("x-affinity".into(), session_id.clone()));
}
}
- headers.push(("authorization".into(), format!("Bearer {api_key}")));
headers
}
@@ -244,6 +247,73 @@ impl Provider for MistralConversationsProvider {
mod tests {
use super::*;
+ /// An explicit Authorization from the model or the caller (a custom provider's `authHeader:
+ /// true` puts one in the caller headers) replaces the default bearer header instead of
+ /// shipping beside it, whatever the header name's case.
+ #[test]
+ fn explicit_authorization_replaces_the_default() {
+ let authorizations = |model: &Model, caller: Option<&str>| {
+ let options = MistralOptions {
+ base: StreamOptions {
+ headers: caller.map(|token| {
+ std::collections::HashMap::from([(
+ "Authorization".to_string(),
+ format!("Bearer {token}"),
+ )])
+ }),
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+ build_request_headers(model, &options, "default-key")
+ .into_iter()
+ .filter(|(name, _)| name.eq_ignore_ascii_case("authorization"))
+ .collect::<Vec<_>>()
+ };
+ let mut model = Model {
+ id: "mistral-small-2603".into(),
+ name: "mistral-small".into(),
+ api: "mistral-conversations".into(),
+ provider: "mistral".into(),
+ base_url: "https://api.mistral.ai".into(),
+ reasoning: true,
+ thinking_level_map: None,
+ input: vec![crate::types::ModelInput::Text],
+ cost: crate::types::zero_model_cost(),
+ context_window: 128_000,
+ max_tokens: 8192,
+ max_tokens_explicit: false,
+ featured: None,
+ headers: None,
+ compat: None,
+ };
+ assert_eq!(
+ authorizations(&model, None),
+ vec![(
+ "authorization".to_string(),
+ "Bearer default-key".to_string()
+ )]
+ );
+ assert_eq!(
+ authorizations(&model, Some("caller-token")),
+ vec![(
+ "Authorization".to_string(),
+ "Bearer caller-token".to_string()
+ )]
+ );
+ model.headers = Some(std::collections::BTreeMap::from([(
+ "AUTHORIZATION".to_string(),
+ "Bearer model-token".to_string(),
+ )]));
+ assert_eq!(
+ authorizations(&model, None),
+ vec![(
+ "AUTHORIZATION".to_string(),
+ "Bearer model-token".to_string()
+ )]
+ );
+ }
+
#[test]
fn tool_choice_serializes() {
assert_eq!(MistralToolChoice::Auto.to_json(), json!("auto"));
diff --git a/crates/pa-ai/src/providers/openai_completions/params.rs b/crates/pa-ai/src/providers/openai_completions/params.rs
index 4a0667889b..8ea4a7c178 100644
--- a/crates/pa-ai/src/providers/openai_completions/params.rs
+++ b/crates/pa-ai/src/providers/openai_completions/params.rs
@@ -327,8 +327,12 @@ pub(crate) fn build_headers(
compat: &ResolvedCompat,
conversation_id: Option<&str>,
) -> Vec<(String, String)> {
- let mut headers: Vec<(String, String)> = Vec::new();
+ // The default Authorization goes in first so model and caller headers replace it (header names
+ // compare case-insensitively) instead of shipping a second Authorization alongside theirs.
+ let mut headers: Vec<(String, String)> =
+ vec![("Authorization".into(), format!("Bearer {api_key}"))];
for (name, value) in model.headers.iter().flatten() {
+ headers.retain(|(existing, _)| !existing.eq_ignore_ascii_case(name));
headers.push((name.clone(), value.clone()));
}
@@ -347,7 +351,6 @@ pub(crate) fn build_headers(
}
}
- headers.insert(0, ("Authorization".into(), format!("Bearer {api_key}")));
let _ = conversation_id;
headers
}
@@ -391,6 +394,63 @@ mod tests {
);
}
+ /// Discussion #3562: an explicit Authorization from the model or the caller replaces the
+ /// default bearer header instead of shipping beside it, whatever the header name's case.
+ #[test]
+ fn explicit_authorization_replaces_the_default_without_case_duplicates() {
+ let mut model = models_generated::get_model("prime-inference", "z-ai/glm-5.3")
+ .expect("the catalog carries a prime-inference model")
+ .clone();
+ let authorizations = |model: &Model, caller: Option<&HashMap<String, String>>| {
+ let compat = crate::providers::openai_completions::get_compat(model);
+ build_headers(model, "default-key", caller, None, &compat, None)
+ .into_iter()
+ .filter(|(name, _)| name.eq_ignore_ascii_case("authorization"))
+ .collect::<Vec<_>>()
+ };
+
+ model.headers = None;
+ assert_eq!(
+ authorizations(&model, None),
+ vec![(
+ "Authorization".to_string(),
+ "Bearer default-key".to_string()
+ )]
+ );
+
+ let caller = HashMap::from([(
+ "authorization".to_string(),
+ "Bearer caller-token".to_string(),
+ )]);
+ assert_eq!(
+ authorizations(&model, Some(&caller)),
+ vec![(
+ "authorization".to_string(),
+ "Bearer caller-token".to_string()
+ )]
+ );
+
+ model.headers = Some(std::collections::BTreeMap::from([(
+ "AUTHORIZATION".to_string(),
+ "Bearer model-token".to_string(),
+ )]));
+ assert_eq!(
+ authorizations(&model, None),
+ vec![(
+ "AUTHORIZATION".to_string(),
+ "Bearer model-token".to_string()
+ )]
+ );
+ // The caller still wins over the model.
+ assert_eq!(
+ authorizations(&model, Some(&caller)),
+ vec![(
+ "authorization".to_string(),
+ "Bearer caller-token".to_string()
+ )]
+ );
+ }
+
/// Assemble params for a compiled catalog model with a reasoning level requested, mirroring
/// `streamSimpleOpenAICompletions`: the level clamps through the model's thinking-level map;
/// the effort and the on/off toggle ride along unless clamped to off.
diff --git a/crates/pa-ai/src/providers/openai_responses.rs b/crates/pa-ai/src/providers/openai_responses.rs
index d37a329a4f..14a14e0c48 100644
--- a/crates/pa-ai/src/providers/openai_responses.rs
+++ b/crates/pa-ai/src/providers/openai_responses.rs
@@ -166,8 +166,12 @@ fn build_headers(
cache_session_id: Option<&str>,
) -> Vec<(String, String)> {
let compat = get_responses_compat(model);
- let mut headers: Vec<(String, String)> = Vec::new();
+ // The default Authorization goes in first so model and caller headers replace it (header names
+ // compare case-insensitively) instead of shipping a second Authorization alongside theirs.
+ let mut headers: Vec<(String, String)> =
+ vec![("Authorization".into(), format!("Bearer {api_key}"))];
for (name, value) in model.headers.iter().flatten() {
+ headers.retain(|(existing, _)| !existing.eq_ignore_ascii_case(name));
headers.push((name.clone(), value.clone()));
}
if let Some(cache_session_id) = cache_session_id {
@@ -182,7 +186,6 @@ fn build_headers(
headers.push((name.clone(), value.clone()));
}
}
- headers.push(("Authorization".into(), format!("Bearer {api_key}")));
headers
}
@@ -509,6 +512,57 @@ mod tests {
.unwrap()
}
+ /// An explicit Authorization from the model or the caller (a custom provider's `authHeader:
+ /// true` puts one in the caller headers) replaces the default bearer header instead of
+ /// shipping beside it, whatever the header name's case.
+ #[test]
+ fn explicit_authorization_replaces_the_default() {
+ let authorizations = |model: &Model, caller: Option<&str>| {
+ let options = OpenAIResponsesOptions {
+ base: StreamOptions {
+ headers: caller.map(|token| {
+ std::collections::HashMap::from([(
+ "authorization".to_string(),
+ format!("Bearer {token}"),
+ )])
+ }),
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+ build_headers(model, "default-key", &options, None)
+ .into_iter()
+ .filter(|(name, _)| name.eq_ignore_ascii_case("authorization"))
+ .collect::<Vec<_>>()
+ };
+ let mut model = compat_model(serde_json::json!({}));
+ assert_eq!(
+ authorizations(&model, None),
+ vec![(
+ "Authorization".to_string(),
+ "Bearer default-key".to_string()
+ )]
+ );
+ assert_eq!(
+ authorizations(&model, Some("caller-token")),
+ vec![(
+ "authorization".to_string(),
+ "Bearer caller-token".to_string()
+ )]
+ );
+ model.headers = Some(std::collections::BTreeMap::from([(
+ "AUTHORIZATION".to_string(),
+ "Bearer model-token".to_string(),
+ )]));
+ assert_eq!(
+ authorizations(&model, None),
+ vec![(
+ "AUTHORIZATION".to_string(),
+ "Bearer model-token".to_string()
+ )]
+ );
+ }
+
#[test]
fn the_shared_key_only_compat_decodes_for_responses_models() {
// TS `getXaiSubscriptionModel`'s compat carries only `supportsLongCacheRetention: false`;