The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(messages): route Azure Anthropic /messages through Rust behind rust:true (#33616)
devin-ai-integration[bot]c4f19c3e4c96
diff --git a/basedpyright-code-budget.json b/basedpyright-code-budget.jsonindex edfb3536ad3..75d4d13eb71 100644--- a/basedpyright-code-budget.json+++ b/basedpyright-code-budget.json@@ -24,7 +24,7 @@"limit": 42},"reportExplicitAny": {- "limit": 10397+ "limit": 10389},"reportFunctionMemberAccess": {"limit": 11diff --git a/litellm-rust/crates/ai-gateway/src/constants.rs b/litellm-rust/crates/ai-gateway/src/constants.rsindex 109b648f5db..557fe5d53d4 100644--- a/litellm-rust/crates/ai-gateway/src/constants.rs+++ b/litellm-rust/crates/ai-gateway/src/constants.rs@@ -28,3 +28,15 @@ pub(crate) const DEFAULT_FLUSH_INTERVAL_MS: u64 = 500;/// Provider attributed to realtime sessions in the logging payload.#[cfg(feature = "server")]pub(crate) const DEFAULT_PROVIDER: &str = "openai";++/// Full-request timeout ceiling for Anthropic Messages provider calls, in+/// seconds. Mirrors the Python Anthropic Messages default. The per-request+/// timeout from `litellm_params` still overrides this on the request builder.+pub(crate) const MESSAGES_TIMEOUT_SECS: u64 = 600;++/// Connect timeout for Anthropic Messages provider calls, in seconds.+pub(crate) const MESSAGES_CONNECT_TIMEOUT_SECS: u64 = 10;++/// Max characters of an upstream error body echoed across the host boundary+/// before truncation, so provider bodies are bounded and data-minimized.+pub(crate) const MESSAGES_ERROR_BODY_MAX_CHARS: usize = 256;diff --git a/litellm-rust/crates/ai-gateway/src/io/messages.rs b/litellm-rust/crates/ai-gateway/src/io/messages.rsnew file mode 100644index 00000000000..b784d2b62a1--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/io/messages.rs@@ -0,0 +1 @@+pub use crate::messages::{messages, MessagesRequest};diff --git a/litellm-rust/crates/ai-gateway/src/io/mod.rs b/litellm-rust/crates/ai-gateway/src/io/mod.rsindex 3b566027646..7bc9642d192 100644--- a/litellm-rust/crates/ai-gateway/src/io/mod.rs+++ b/litellm-rust/crates/ai-gateway/src/io/mod.rs@@ -1,3 +1,4 @@+pub mod messages;pub mod ocr;pub mod realtime;pub mod realtime_pool;diff --git a/litellm-rust/crates/ai-gateway/src/lib.rs b/litellm-rust/crates/ai-gateway/src/lib.rsindex d8ef7bb5ba1..db4c8211a5a 100644--- a/litellm-rust/crates/ai-gateway/src/lib.rs+++ b/litellm-rust/crates/ai-gateway/src/lib.rs@@ -12,6 +12,7 @@//! for the load-time config reader.pub mod io;+pub mod messages;pub mod ocr;/// GIL-activity tracking. Pure (atomics only); shared by the `server` routes anddiff --git a/litellm-rust/crates/ai-gateway/src/messages/client.rs b/litellm-rust/crates/ai-gateway/src/messages/client.rsnew file mode 100644index 00000000000..6281270b964--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/messages/client.rs@@ -0,0 +1,15 @@+use std::sync::OnceLock;+use std::time::Duration;++use crate::constants::{MESSAGES_CONNECT_TIMEOUT_SECS, MESSAGES_TIMEOUT_SECS};++pub(super) fn http_client() -> &'static reqwest::Client {+ static CLIENT: OnceLock<reqwest::Client> = OnceLock::new();+ CLIENT.get_or_init(|| {+ reqwest::Client::builder()+ .timeout(Duration::from_secs(MESSAGES_TIMEOUT_SECS))+ .connect_timeout(Duration::from_secs(MESSAGES_CONNECT_TIMEOUT_SECS))+ .build()+ .unwrap_or_else(|_| reqwest::Client::new())+ })+}diff --git a/litellm-rust/crates/ai-gateway/src/messages/common_utils.rs b/litellm-rust/crates/ai-gateway/src/messages/common_utils.rsnew file mode 100644index 00000000000..fe4ac4cf26f--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/messages/common_utils.rs@@ -0,0 +1,50 @@+use litellm_core::error::{json_type_name, CoreError};+use litellm_core::messages::transformation::AnthropicMessagesProviderConfig;+use litellm_core::providers::azure_ai::messages::transformation::AZURE_ANTHROPIC_MESSAGES_CONFIG;+use litellm_core::CoreResult;+use serde_json::{Map, Value};++use crate::constants::MESSAGES_ERROR_BODY_MAX_CHARS;++pub(super) fn truncate_error_body(body: &str) -> String {+ if body.chars().count() <= MESSAGES_ERROR_BODY_MAX_CHARS {+ return body.to_string();+ }+ let truncated: String = body.chars().take(MESSAGES_ERROR_BODY_MAX_CHARS).collect();+ format!("{truncated}... (truncated)")+}++pub(super) fn messages_provider_config(+ provider: &str,+) -> Option<&'static dyn AnthropicMessagesProviderConfig> {+ match provider {+ "azure_ai" => Some(&AZURE_ANTHROPIC_MESSAGES_CONFIG),+ _ => None,+ }+}++pub(super) fn string_headers(+ extra_headers: Option<Map<String, Value>>,+) -> CoreResult<Vec<(String, String)>> {+ extra_headers+ .unwrap_or_default()+ .into_iter()+ .map(|(key, value)| {+ value+ .as_str()+ .map(|value| (key.clone(), value.to_string()))+ .ok_or_else(|| {+ CoreError::InvalidRequest(format!(+ "messages extra_headers.{key} must be a string, got {}",+ json_type_name(&value)+ ))+ })+ })+ .collect()+}++pub(super) fn has_header(headers: &[(String, String)], name: &str) -> bool {+ headers+ .iter()+ .any(|(key, _)| key.eq_ignore_ascii_case(name))+}diff --git a/litellm-rust/crates/ai-gateway/src/messages/handler.rs b/litellm-rust/crates/ai-gateway/src/messages/handler.rsnew file mode 100644index 00000000000..dd4a2f22aa7--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/messages/handler.rs@@ -0,0 +1,47 @@+use litellm_core::error::CoreError;+use litellm_core::CoreResult;+use serde_json::Value;++use super::client::http_client;+use super::common_utils::truncate_error_body;+use super::types::ProviderMessagesRequest;++pub(super) async fn execute_messages_provider_call(+ request: ProviderMessagesRequest,+) -> CoreResult<Value> {+ let mut request_builder = http_client().post(&request.url).json(&request.body);+ for (key, value) in &request.upstream_headers {+ request_builder = request_builder.header(key, value);+ }+ if let Some(duration) = request.timeout {+ request_builder = request_builder.timeout(duration);+ }++ let response = request_builder+ .send()+ .await+ .map_err(|err| CoreError::Network(err.to_string()))?;++ let status = response.status();+ let text = response+ .text()+ .await+ .map_err(|err| CoreError::Network(err.to_string()))?;++ if !status.is_success() {+ return Err(CoreError::Http {+ status: status.as_u16(),+ body: truncate_error_body(&text),+ });+ }++ let response = serde_json::from_str(&text).map_err(|err| {+ CoreError::InvalidResponse(format!("invalid messages response JSON: {err}"))+ })?;+ let transformed = request+ .config+ .transform_response(&request.model, response)?;+ serde_json::to_value(transformed).map_err(|err| {+ CoreError::InvalidResponse(format!("failed to serialize messages response: {err}"))+ })+}diff --git a/litellm-rust/crates/ai-gateway/src/messages/mod.rs b/litellm-rust/crates/ai-gateway/src/messages/mod.rsnew file mode 100644index 00000000000..7ed81474c47--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/messages/mod.rs@@ -0,0 +1,21 @@+use litellm_core::CoreResult;+use serde_json::Value;++mod client;+mod common_utils;+mod handler;+mod prepare;+mod types;++pub use types::MessagesRequest;++use handler::execute_messages_provider_call;+use prepare::prepare_messages_call;++pub async fn messages(request: MessagesRequest<'_>) -> CoreResult<Value> {+ let prepared = prepare_messages_call(request)?;+ execute_messages_provider_call(prepared).await+}++#[cfg(test)]+mod tests;diff --git a/litellm-rust/crates/ai-gateway/src/messages/prepare.rs b/litellm-rust/crates/ai-gateway/src/messages/prepare.rsnew file mode 100644index 00000000000..47105b39954--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/messages/prepare.rs@@ -0,0 +1,72 @@+use litellm_core::messages::transformation::MessagesAuthStrategy;+use litellm_core::routing_utils::provider::{get_custom_llm_provider, CustomLlmProvider};+use litellm_core::CoreError;+use litellm_core::CoreResult;++use super::common_utils::{has_header, messages_provider_config, string_headers};+use super::types::{MessagesRequest, ProviderMessagesRequest};++pub(super) fn prepare_messages_call(+ request: MessagesRequest<'_>,+) -> CoreResult<ProviderMessagesRequest> {+ let provider_info = get_custom_llm_provider(request.model, request.custom_llm_provider)+ .or_else(|| {+ request+ .custom_llm_provider+ .map(|provider| CustomLlmProvider {+ model: request.model,+ custom_llm_provider: provider,+ })+ })+ .ok_or_else(|| {+ CoreError::InvalidProvider(+ "unable to resolve custom_llm_provider for messages request".to_string(),+ )+ })?;+ let model = provider_info.model.to_string();+ let provider = provider_info.custom_llm_provider;++ let config = messages_provider_config(provider)+ .ok_or_else(|| CoreError::InvalidProvider(provider.to_string()))?;+ let env_lookup = |key: &str| std::env::var(key).ok();++ let mut headers = string_headers(request.extra_headers)?;++ let auth_strategy = config.auth_strategy();+ if !has_header(&headers, auth_strategy.header_name()) {+ let api_key = config.resolve_api_key(request.api_key, &env_lookup)?;+ let auth_header = match auth_strategy {+ MessagesAuthStrategy::Bearer => {+ ("authorization".to_string(), format!("Bearer {api_key}"))+ }+ MessagesAuthStrategy::Header(name) => (name.to_string(), api_key),+ };+ headers.push(auth_header);+ }++ for (name, value) in config.default_headers() {+ if !has_header(&headers, name) {+ headers.push((name.to_string(), value.to_string()));+ }+ }++ let url = config.complete_url(request.api_base, &model, &env_lookup)?;+ let typed_request = serde_json::from_value(request.body).map_err(|err| {+ CoreError::InvalidRequest(format!("invalid Anthropic messages request: {err}"))+ })?;+ let transformed = config.transform_request(typed_request)?;+ let body = serde_json::to_value(transformed).map_err(|err| {+ CoreError::InvalidRequest(format!(+ "failed to serialize Anthropic messages request: {err}"+ ))+ })?;++ Ok(ProviderMessagesRequest {+ model,+ config,+ url,+ body,+ upstream_headers: headers,+ timeout: request.timeout,+ })+}diff --git a/litellm-rust/crates/ai-gateway/src/messages/tests.rs b/litellm-rust/crates/ai-gateway/src/messages/tests.rsnew file mode 100644index 00000000000..30f6642400e--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/messages/tests.rs@@ -0,0 +1,259 @@+use std::time::Duration;++use litellm_core::error::CoreError;+use serde_json::{json, Map, Value};+use tokio::io::{AsyncReadExt, AsyncWriteExt};+use tokio::net::{TcpListener, TcpStream};++use super::common_utils::{+ has_header, messages_provider_config, string_headers, truncate_error_body,+};+use super::{messages, MessagesRequest};++async fn read_http_request(socket: &mut TcpStream) -> String {+ let mut request = Vec::new();+ let mut buffer = [0_u8; 1024];+ let header_end = loop {+ let n = socket.read(&mut buffer).await.expect("reads request");+ if n == 0 {+ break request.len();+ }+ request.extend_from_slice(&buffer[..n]);+ if let Some(position) = request.windows(4).position(|window| window == b"\r\n\r\n") {+ break position + 4;+ }+ };+ let headers = String::from_utf8_lossy(&request[..header_end]);+ let content_length = headers+ .lines()+ .find_map(|line| {+ let (name, value) = line.split_once(':')?;+ name.eq_ignore_ascii_case("content-length")+ .then(|| value.trim().parse::<usize>().ok())+ .flatten()+ })+ .unwrap_or(0);+ while request.len().saturating_sub(header_end) < content_length {+ let n = socket.read(&mut buffer).await.expect("reads body");+ if n == 0 {+ break;+ }+ request.extend_from_slice(&buffer[..n]);+ }+ String::from_utf8(request).expect("request is utf8")+}++fn write_response(body: &str) -> String {+ format!(+ "HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",+ body.len(),+ body+ )+}++#[test]+fn provider_config_only_resolves_azure_ai() {+ assert!(messages_provider_config("azure_ai").is_some());+ assert!(messages_provider_config("anthropic").is_none());+ assert!(messages_provider_config("openai").is_none());+}++#[test]+fn truncate_error_body_caps_long_payloads() {+ let body = "x".repeat(400);+ let truncated = truncate_error_body(&body);+ assert!(truncated.ends_with("... (truncated)"));+ let prefix_chars = truncated+ .strip_suffix("... (truncated)")+ .expect("truncated marker present")+ .chars()+ .count();+ assert_eq!(prefix_chars, 256);+}++#[test]+fn string_headers_rejects_non_string_values() {+ let headers = json!({"x-count": 3}).as_object().unwrap().clone();+ let err = string_headers(Some(headers)).expect_err("non-string header rejected");+ assert!(matches!(err, CoreError::InvalidRequest(_)));+}++#[test]+fn has_header_is_case_insensitive() {+ let headers = vec![("X-Api-Key".to_string(), "secret".to_string())];+ assert!(has_header(&headers, "x-api-key"));+ assert!(!has_header(&headers, "authorization"));+}++#[tokio::test]+async fn messages_round_trip_builds_azure_request_and_passes_response_through() {+ let listener = TcpListener::bind("127.0.0.1:0").await.expect("binds");+ let addr = listener.local_addr().expect("addr");++ let server = tokio::spawn(async move {+ let (mut socket, _) = listener.accept().await.expect("accepts request");+ let request = read_http_request(&mut socket).await;+ let response_body = r#"{"id":"msg_1","type":"message","role":"assistant","content":[{"type":"text","text":"hi"}],"model":"claude-sonnet-4-5","stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":2}}"#;+ socket+ .write_all(write_response(response_body).as_bytes())+ .await+ .expect("writes response");+ request+ });++ let response = messages(MessagesRequest {+ model: "claude-sonnet-4-5",+ body: json!({+ "model": "claude-sonnet-4-5",+ "max_tokens": 1024,+ "messages": [{+ "role": "user",+ "content": [{+ "type": "text",+ "text": "hi",+ "cache_control": {"type": "ephemeral", "scope": "global"}+ }]+ }]+ }),+ api_key: Some("sk-azure"),+ api_base: Some(&format!("http://{addr}")),+ custom_llm_provider: Some("azure_ai"),+ extra_headers: None,+ timeout: Some(Duration::from_secs(5)),+ })+ .await+ .expect("messages request succeeds");++ assert_eq!(response["content"][0]["text"], "hi");+ assert_eq!(response["stop_reason"], "end_turn");++ let request = server.await.expect("server task completes");+ let (head, body) = request.split_once("\r\n\r\n").expect("has body");+ assert!(head.starts_with("POST /anthropic/v1/messages "), "{head}");+ let head_lower = head.to_ascii_lowercase();+ assert!(head_lower.contains("x-api-key: sk-azure"), "{head}");+ assert!(+ head_lower.contains("anthropic-version: 2023-06-01"),+ "{head}"+ );+ assert!(+ head_lower.contains("content-type: application/json"),+ "{head}"+ );++ let sent_body: Value = serde_json::from_str(body).expect("body is json");+ assert_eq!(+ sent_body["messages"][0]["content"][0]["cache_control"],+ json!({"type": "ephemeral"})+ );+}++#[tokio::test]+async fn messages_does_not_duplicate_auth_when_x_api_key_supplied() {+ let listener = TcpListener::bind("127.0.0.1:0").await.expect("binds");+ let addr = listener.local_addr().expect("addr");++ let server = tokio::spawn(async move {+ let (mut socket, _) = listener.accept().await.expect("accepts request");+ let request = read_http_request(&mut socket).await;+ let response_body =+ r#"{"id":"msg_2","type":"message","role":"assistant","content":[],"model":"m"}"#;+ socket+ .write_all(write_response(response_body).as_bytes())+ .await+ .expect("writes response");+ request+ });++ let mut headers = Map::new();+ headers.insert(+ "x-api-key".to_string(),+ Value::String("from-python".to_string()),+ );+ headers.insert(+ "anthropic-beta".to_string(),+ Value::String("token-efficient-tools-2025-02-19".to_string()),+ );++ messages(MessagesRequest {+ model: "claude-sonnet-4-5",+ body: json!({"model": "claude-sonnet-4-5", "max_tokens": 8, "messages": []}),+ api_key: Some("rust-fallback-key"),+ api_base: Some(&format!("http://{addr}")),+ custom_llm_provider: Some("azure_ai"),+ extra_headers: Some(headers),+ timeout: Some(Duration::from_secs(5)),+ })+ .await+ .expect("messages request succeeds");++ let request = server.await.expect("server task completes");+ let head = request+ .split_once("\r\n\r\n")+ .expect("has body")+ .0+ .to_ascii_lowercase();+ let api_key_count = head+ .lines()+ .filter(|line| line.starts_with("x-api-key:"))+ .count();+ assert_eq!(api_key_count, 1, "{head}");+ assert!(head.contains("x-api-key: from-python"), "{head}");+ assert!(+ head.contains("anthropic-beta: token-efficient-tools-2025-02-19"),+ "{head}"+ );+ assert!(!head.contains("rust-fallback-key"), "{head}");+}++#[tokio::test]+async fn messages_maps_provider_error_status_to_http_error() {+ let listener = TcpListener::bind("127.0.0.1:0").await.expect("binds");+ let addr = listener.local_addr().expect("addr");++ tokio::spawn(async move {+ let (mut socket, _) = listener.accept().await.expect("accepts request");+ let _ = read_http_request(&mut socket).await;+ let body = "unauthorized";+ let response = format!(+ "HTTP/1.1 401 Unauthorized\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",+ body.len(),+ body+ );+ socket+ .write_all(response.as_bytes())+ .await+ .expect("writes response");+ });++ let err = messages(MessagesRequest {+ model: "claude-sonnet-4-5",+ body: json!({"model": "claude-sonnet-4-5", "max_tokens": 8, "messages": []}),+ api_key: Some("sk-azure"),+ api_base: Some(&format!("http://{addr}")),+ custom_llm_provider: Some("azure_ai"),+ extra_headers: None,+ timeout: Some(Duration::from_secs(5)),+ })+ .await+ .expect_err("provider error propagates");++ assert!(matches!(err, CoreError::Http { status: 401, .. }));+}++#[tokio::test]+async fn messages_rejects_unsupported_provider() {+ let err = messages(MessagesRequest {+ model: "claude-3-5-sonnet",+ body: json!({"model": "claude-3-5-sonnet", "max_tokens": 8, "messages": []}),+ api_key: Some("sk"),+ api_base: Some("http://127.0.0.1:1"),+ custom_llm_provider: Some("anthropic"),+ extra_headers: None,+ timeout: Some(Duration::from_millis(50)),+ })+ .await+ .expect_err("unsupported provider errors");++ assert!(matches!(err, CoreError::InvalidProvider(provider) if provider == "anthropic"));+}diff --git a/litellm-rust/crates/ai-gateway/src/messages/types.rs b/litellm-rust/crates/ai-gateway/src/messages/types.rsnew file mode 100644index 00000000000..6840ff57cc4--- /dev/null+++ b/litellm-rust/crates/ai-gateway/src/messages/types.rs@@ -0,0 +1,23 @@+use std::time::Duration;++use litellm_core::messages::transformation::AnthropicMessagesProviderConfig;+use serde_json::{Map, Value};++pub struct MessagesRequest<'a> {+ pub model: &'a str,+ pub body: Value,+ pub api_key: Option<&'a str>,+ pub api_base: Option<&'a str>,+ pub custom_llm_provider: Option<&'a str>,+ pub extra_headers: Option<Map<String, Value>>,+ pub timeout: Option<Duration>,+}++pub(crate) struct ProviderMessagesRequest {+ pub(crate) model: String,+ pub(crate) config: &'static dyn AnthropicMessagesProviderConfig,+ pub(crate) url: String,+ pub(crate) body: Value,+ pub(crate) upstream_headers: Vec<(String, String)>,+ pub(crate) timeout: Option<Duration>,+}diff --git a/litellm-rust/crates/core/src/error.rs b/litellm-rust/crates/core/src/error.rsindex b3e0519b772..c2b08eee0c0 100644--- a/litellm-rust/crates/core/src/error.rs+++ b/litellm-rust/crates/core/src/error.rs@@ -19,9 +19,9 @@ pub enum CoreError {InvalidRequest(String),#[error("{0}")]Auth(String),- #[error("OCR request failed with status {status}: {body}")]+ #[error("upstream request failed with status {status}: {body}")]Http { status: u16, body: String },- #[error("OCR network error: {0}")]+ #[error("upstream network error: {0}")]Network(String),#[error("routing error: {0}")]Routing(String),diff --git a/litellm-rust/crates/core/src/lib.rs b/litellm-rust/crates/core/src/lib.rsindex 555a04ce853..27154f5a08b 100644--- a/litellm-rust/crates/core/src/lib.rs+++ b/litellm-rust/crates/core/src/lib.rs@@ -1,5 +1,6 @@pub mod call_lifecycle;pub mod error;+pub mod messages;pub mod ocr;pub mod providers;pub mod realtime;diff --git a/litellm-rust/crates/core/src/messages/mod.rs b/litellm-rust/crates/core/src/messages/mod.rsnew file mode 100644index 00000000000..ec2fbb969a6--- /dev/null+++ b/litellm-rust/crates/core/src/messages/mod.rs@@ -0,0 +1,2 @@+pub mod transformation;+pub mod types;diff --git a/litellm-rust/crates/core/src/messages/transformation.rs b/litellm-rust/crates/core/src/messages/transformation.rsnew file mode 100644index 00000000000..3a34a58de6f--- /dev/null+++ b/litellm-rust/crates/core/src/messages/transformation.rs@@ -0,0 +1,59 @@+use crate::error::CoreResult;++use super::types::{AnthropicMessagesRequest, AnthropicMessagesResponse};++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum MessagesAuthStrategy {+ Bearer,+ Header(&'static str),+}++impl MessagesAuthStrategy {+ pub fn header_name(self) -> &'static str {+ match self {+ Self::Bearer => "authorization",+ Self::Header(header_name) => header_name,+ }+ }+}++pub trait AnthropicMessagesProviderConfig: Sync {+ fn complete_url(+ &self,+ api_base: Option<&str>,+ model: &str,+ env_lookup: &dyn Fn(&str) -> Option<String>,+ ) -> CoreResult<String>;++ fn resolve_api_key(+ &self,+ api_key: Option<&str>,+ env_lookup: &dyn Fn(&str) -> Option<String>,+ ) -> CoreResult<String>;++ fn auth_strategy(&self) -> MessagesAuthStrategy {+ MessagesAuthStrategy::Header("x-api-key")+ }++ fn default_headers(&self) -> &'static [(&'static str, &'static str)] {+ &[+ ("anthropic-version", "2023-06-01"),+ ("content-type", "application/json"),+ ]+ }++ fn transform_request(+ &self,+ request: AnthropicMessagesRequest,+ ) -> CoreResult<AnthropicMessagesRequest> {+ Ok(request)+ }++ fn transform_response(+ &self,+ _model: &str,+ response: AnthropicMessagesResponse,+ ) -> CoreResult<AnthropicMessagesResponse> {+ Ok(response)+ }+}diff --git a/litellm-rust/crates/core/src/messages/types.rs b/litellm-rust/crates/core/src/messages/types.rsnew file mode 100644index 00000000000..11fe17ea40f--- /dev/null+++ b/litellm-rust/crates/core/src/messages/types.rs@@ -0,0 +1,110 @@+use serde::{Deserialize, Serialize};+use serde_json::{Map, Value};++#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]+#[serde(untagged)]+pub enum SystemPrompt {+ Text(String),+ Blocks(Vec<ContentBlock>),+}++#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]+#[serde(untagged)]+pub enum MessageContent {+ Text(String),+ Blocks(Vec<ContentBlock>),+}++#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]+pub struct ContentBlock {+ #[serde(skip_serializing_if = "Option::is_none")]+ pub cache_control: Option<CacheControl>,+ #[serde(flatten)]+ pub extra: Map<String, Value>,+}++#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]+pub struct CacheControl {+ #[serde(rename = "type", skip_serializing_if = "Option::is_none")]+ pub cache_type: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub ttl: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub scope: Option<String>,+ #[serde(flatten)]+ pub extra: Map<String, Value>,+}++#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]+pub struct AnthropicMessage {+ pub role: String,+ pub content: MessageContent,+ #[serde(flatten)]+ pub extra: Map<String, Value>,+}++#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]+pub struct AnthropicMessagesRequest {+ pub model: String,+ pub messages: Vec<AnthropicMessage>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub max_tokens: Option<u64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub system: Option<SystemPrompt>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub metadata: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub stop_sequences: Option<Vec<String>>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub stream: Option<bool>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub temperature: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub top_p: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub top_k: Option<i64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub tools: Option<Vec<Value>>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub tool_choice: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub thinking: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub service_tier: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub container: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub mcp_servers: Option<Vec<Value>>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub context_management: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub output_format: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub output_config: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub speed: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub inference_geo: Option<String>,+ #[serde(flatten)]+ pub extra: Map<String, Value>,+}++#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]+pub struct AnthropicMessagesResponse {+ pub id: String,+ #[serde(rename = "type")]+ pub message_type: String,+ pub role: String,+ pub model: String,+ pub content: Vec<Value>,+ // Anthropic always includes stop_reason / stop_sequence, null until the turn+ // ends; serialize them even when None so callers see the same shape as Python.+ pub stop_reason: Option<String>,+ pub stop_sequence: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub usage: Option<Value>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub container: Option<Value>,+ #[serde(flatten)]+ pub extra: Map<String, Value>,+}diff --git a/litellm-rust/crates/core/src/providers/anthropic/messages/mod.rs b/litellm-rust/crates/core/src/providers/anthropic/messages/mod.rsnew file mode 100644index 00000000000..f239b6921fa--- /dev/null+++ b/litellm-rust/crates/core/src/providers/anthropic/messages/mod.rs@@ -0,0 +1 @@+pub mod transformation;diff --git a/litellm-rust/crates/core/src/providers/anthropic/messages/transformation.rs b/litellm-rust/crates/core/src/providers/anthropic/messages/transformation.rsnew file mode 100644index 00000000000..829f2260d3c--- /dev/null+++ b/litellm-rust/crates/core/src/providers/anthropic/messages/transformation.rs@@ -0,0 +1,142 @@+use crate::error::{CoreError, CoreResult};+use crate::messages::transformation::{AnthropicMessagesProviderConfig, MessagesAuthStrategy};++const ANTHROPIC_API_KEY_ENV: &str = "ANTHROPIC_API_KEY";+const ANTHROPIC_API_BASE_ENV: &str = "ANTHROPIC_API_BASE";+const DEFAULT_ANTHROPIC_API_BASE: &str = "https://api.anthropic.com";+const MESSAGES_PATH_SUFFIX: &str = "/v1/messages";++pub struct AnthropicMessagesConfig;++pub const ANTHROPIC_MESSAGES_CONFIG: AnthropicMessagesConfig = AnthropicMessagesConfig;++pub fn non_empty(value: Option<&str>) -> Option<&str> {+ value.map(str::trim).filter(|value| !value.is_empty())+}++pub fn resolve_anthropic_api_key(+ api_key: Option<&str>,+ env_lookup: &dyn Fn(&str) -> Option<String>,+) -> CoreResult<String> {+ non_empty(api_key)+ .map(str::to_string)+ .or_else(|| env_lookup(ANTHROPIC_API_KEY_ENV).filter(|value| !value.trim().is_empty()))+ .ok_or_else(|| {+ CoreError::Auth(+ "Missing Anthropic API Key - Set `api_key` or the ANTHROPIC_API_KEY \+ environment variable"+ .to_string(),+ )+ })+}++pub fn complete_anthropic_url(+ api_base: Option<&str>,+ env_lookup: &dyn Fn(&str) -> Option<String>,+) -> String {+ let api_base = non_empty(api_base)+ .map(str::to_string)+ .or_else(|| env_lookup(ANTHROPIC_API_BASE_ENV).filter(|value| !value.trim().is_empty()))+ .unwrap_or_else(|| DEFAULT_ANTHROPIC_API_BASE.to_string());++ let api_base = api_base.trim_end_matches('/');+ if api_base.ends_with(MESSAGES_PATH_SUFFIX) {+ return api_base.to_string();+ }+ format!("{api_base}{MESSAGES_PATH_SUFFIX}")+}++impl AnthropicMessagesProviderConfig for AnthropicMessagesConfig {+ fn complete_url(+ &self,+ api_base: Option<&str>,+ _model: &str,+ env_lookup: &dyn Fn(&str) -> Option<String>,+ ) -> CoreResult<String> {+ Ok(complete_anthropic_url(api_base, env_lookup))+ }++ fn resolve_api_key(+ &self,+ api_key: Option<&str>,+ env_lookup: &dyn Fn(&str) -> Option<String>,+ ) -> CoreResult<String> {+ resolve_anthropic_api_key(api_key, env_lookup)+ }++ fn auth_strategy(&self) -> MessagesAuthStrategy {+ MessagesAuthStrategy::Header("x-api-key")+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn url_defaults_to_public_anthropic_endpoint() {+ assert_eq!(+ complete_anthropic_url(None, &|_| None),+ "https://api.anthropic.com/v1/messages"+ );+ }++ #[test]+ fn url_appends_messages_suffix_to_custom_base() {+ assert_eq!(+ complete_anthropic_url(Some("https://proxy.internal"), &|_| None),+ "https://proxy.internal/v1/messages"+ );+ }++ #[test]+ fn url_leaves_complete_messages_endpoint_untouched() {+ assert_eq!(+ complete_anthropic_url(Some("https://proxy.internal/v1/messages"), &|_| None),+ "https://proxy.internal/v1/messages"+ );+ }++ #[test]+ fn url_falls_back_to_env_base() {+ let with_env = |key: &str| {+ (key == ANTHROPIC_API_BASE_ENV).then(|| "https://env.anthropic".to_string())+ };+ assert_eq!(+ complete_anthropic_url(Some(" "), &with_env),+ "https://env.anthropic/v1/messages"+ );+ }++ #[test]+ fn api_key_prefers_param_then_env_then_errors() {+ assert_eq!(+ resolve_anthropic_api_key(Some("sk-param"), &|_| None).unwrap(),+ "sk-param"+ );+ let with_env = |key: &str| (key == ANTHROPIC_API_KEY_ENV).then(|| "sk-env".to_string());+ assert_eq!(+ resolve_anthropic_api_key(Some(" "), &with_env).unwrap(),+ "sk-env"+ );+ assert!(matches!(+ resolve_anthropic_api_key(None, &|_| None).expect_err("missing key"),+ CoreError::Auth(_)+ ));+ }++ #[test]+ fn auth_strategy_and_default_headers_match_anthropic() {+ assert_eq!(+ ANTHROPIC_MESSAGES_CONFIG.auth_strategy().header_name(),+ "x-api-key"+ );+ assert_eq!(+ ANTHROPIC_MESSAGES_CONFIG.default_headers(),+ &[+ ("anthropic-version", "2023-06-01"),+ ("content-type", "application/json"),+ ]+ );+ }+}diff --git a/litellm-rust/crates/core/src/providers/anthropic/mod.rs b/litellm-rust/crates/core/src/providers/anthropic/mod.rsnew file mode 100644index 00000000000..ba63992f3cb--- /dev/null+++ b/litellm-rust/crates/core/src/providers/anthropic/mod.rs@@ -0,0 +1 @@+pub mod messages;diff --git a/litellm-rust/crates/core/src/providers/azure_ai/messages/mod.rs b/litellm-rust/crates/core/src/providers/azure_ai/messages/mod.rsnew file mode 100644index 00000000000..f239b6921fa--- /dev/null+++ b/litellm-rust/crates/core/src/providers/azure_ai/messages/mod.rs@@ -0,0 +1 @@+pub mod transformation;diff --git a/litellm-rust/crates/core/src/providers/azure_ai/messages/transformation.rs b/litellm-rust/crates/core/src/providers/azure_ai/messages/transformation.rsnew file mode 100644index 00000000000..13e79b087c7--- /dev/null+++ b/litellm-rust/crates/core/src/providers/azure_ai/messages/transformation.rs@@ -0,0 +1,512 @@+use crate::error::{CoreError, CoreResult};+use crate::messages::transformation::{AnthropicMessagesProviderConfig, MessagesAuthStrategy};+use crate::messages::types::{+ AnthropicMessage, AnthropicMessagesRequest, AnthropicMessagesResponse, ContentBlock,+ MessageContent, SystemPrompt,+};+use crate::providers::anthropic::messages::transformation::{+ non_empty, AnthropicMessagesConfig, ANTHROPIC_MESSAGES_CONFIG,+};+use serde_json::{Map, Value};++const AZURE_API_KEY_ENV: &str = "AZURE_API_KEY";+const AZURE_API_BASE_ENV: &str = "AZURE_API_BASE";+const ANTHROPIC_PATH_SEGMENT: &str = "/anthropic";+const MESSAGES_PATH_SUFFIX: &str = "/v1/messages";+const SYSTEM_ROLE: &str = "system";+const TEXT_BLOCK_TYPE: &str = "text";++pub struct AzureAnthropicMessagesConfig {+ anthropic: AnthropicMessagesConfig,+}++pub const AZURE_ANTHROPIC_MESSAGES_CONFIG: AzureAnthropicMessagesConfig =+ AzureAnthropicMessagesConfig {+ anthropic: ANTHROPIC_MESSAGES_CONFIG,+ };++pub fn resolve_azure_api_key(+ api_key: Option<&str>,+ env_lookup: &dyn Fn(&str) -> Option<String>,+) -> CoreResult<String> {+ non_empty(api_key)+ .map(str::to_string)+ .or_else(|| env_lookup(AZURE_API_KEY_ENV).filter(|value| !value.trim().is_empty()))+ .ok_or_else(|| {+ CoreError::Auth(+ "Missing Azure API Key - Set `api_key` or the AZURE_API_KEY environment variable"+ .to_string(),+ )+ })+}++pub fn complete_azure_anthropic_url(+ api_base: Option<&str>,+ env_lookup: &dyn Fn(&str) -> Option<String>,+) -> CoreResult<String> {+ let api_base = non_empty(api_base)+ .map(str::to_string)+ .or_else(|| env_lookup(AZURE_API_BASE_ENV).filter(|value| !value.trim().is_empty()))+ .ok_or_else(|| {+ CoreError::Auth(+ "Missing Azure API Base - Set `api_base` or the AZURE_API_BASE environment variable. \+ Expected format: https://<resource-name>.services.ai.azure.com/anthropic"+ .to_string(),+ )+ })?;++ let api_base = api_base.trim_end_matches('/');++ if api_base.ends_with(MESSAGES_PATH_SUFFIX) {+ return Ok(api_base.to_string());+ }++ let with_anthropic = match api_base.split_once(ANTHROPIC_PATH_SEGMENT) {+ Some((prefix, _)) => format!("{prefix}{ANTHROPIC_PATH_SEGMENT}"),+ None => format!("{api_base}{ANTHROPIC_PATH_SEGMENT}"),+ };+ Ok(format!("{with_anthropic}{MESSAGES_PATH_SUFFIX}"))+}++fn strip_scope_from_block(block: &mut ContentBlock) {+ if let Some(cache_control) = block.cache_control.as_mut() {+ cache_control.scope = None;+ }+}++fn strip_scope_from_system(system: &mut SystemPrompt) {+ if let SystemPrompt::Blocks(blocks) = system {+ blocks.iter_mut().for_each(strip_scope_from_block);+ }+}++fn strip_scope_from_message(message: &mut AnthropicMessage) {+ if let MessageContent::Blocks(blocks) = &mut message.content {+ blocks.iter_mut().for_each(strip_scope_from_block);+ }+}++fn text_content_block(text: String) -> ContentBlock {+ let extra = Map::from_iter([+ (+ "type".to_string(),+ Value::String(TEXT_BLOCK_TYPE.to_string()),+ ),+ ("text".to_string(), Value::String(text)),+ ]);+ ContentBlock {+ cache_control: None,+ extra,+ }+}++fn content_into_blocks(content: MessageContent) -> Vec<ContentBlock> {+ match content {+ MessageContent::Text(text) => vec![text_content_block(text)],+ MessageContent::Blocks(blocks) => blocks,+ }+}++fn system_into_blocks(system: Option<SystemPrompt>) -> Vec<ContentBlock> {+ match system {+ None => Vec::new(),+ Some(SystemPrompt::Text(text)) => vec![text_content_block(text)],+ Some(SystemPrompt::Blocks(blocks)) => blocks,+ }+}++fn fold_system_role_messages(request: AnthropicMessagesRequest) -> AnthropicMessagesRequest {+ if !request.messages.iter().any(|msg| msg.role == SYSTEM_ROLE) {+ return request;+ }++ let (system_messages, chat_messages): (Vec<AnthropicMessage>, Vec<AnthropicMessage>) = request+ .messages+ .into_iter()+ .partition(|msg| msg.role == SYSTEM_ROLE);++ let folded_system: Vec<ContentBlock> = system_into_blocks(request.system)+ .into_iter()+ .chain(+ system_messages+ .into_iter()+ .flat_map(|msg| content_into_blocks(msg.content)),+ )+ .collect();++ AnthropicMessagesRequest {+ messages: chat_messages,+ system: (!folded_system.is_empty()).then_some(SystemPrompt::Blocks(folded_system)),+ ..request+ }+}++impl AnthropicMessagesProviderConfig for AzureAnthropicMessagesConfig {+ fn complete_url(+ &self,+ api_base: Option<&str>,+ _model: &str,+ env_lookup: &dyn Fn(&str) -> Option<String>,+ ) -> CoreResult<String> {+ complete_azure_anthropic_url(api_base, env_lookup)+ }++ fn resolve_api_key(+ &self,+ api_key: Option<&str>,+ env_lookup: &dyn Fn(&str) -> Option<String>,+ ) -> CoreResult<String> {+ resolve_azure_api_key(api_key, env_lookup)+ }++ fn auth_strategy(&self) -> MessagesAuthStrategy {+ self.anthropic.auth_strategy()+ }++ fn default_headers(&self) -> &'static [(&'static str, &'static str)] {+ self.anthropic.default_headers()+ }++ fn transform_request(+ &self,+ request: AnthropicMessagesRequest,+ ) -> CoreResult<AnthropicMessagesRequest> {+ let mut request = fold_system_role_messages(request);+ if let Some(system) = request.system.as_mut() {+ strip_scope_from_system(system);+ }+ request+ .messages+ .iter_mut()+ .for_each(strip_scope_from_message);+ self.anthropic.transform_request(request)+ }++ fn transform_response(+ &self,+ model: &str,+ response: AnthropicMessagesResponse,+ ) -> CoreResult<AnthropicMessagesResponse> {+ self.anthropic.transform_response(model, response)+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use serde_json::json;++ fn request_from(value: serde_json::Value) -> AnthropicMessagesRequest {+ serde_json::from_value(value).expect("valid request")+ }++ fn to_value(request: AnthropicMessagesRequest) -> serde_json::Value {+ serde_json::to_value(request).expect("serializable request")+ }++ #[test]+ fn url_appends_anthropic_and_messages_suffix() {+ let url =+ complete_azure_anthropic_url(Some("https://resource.services.ai.azure.com"), &|_| None)+ .expect("url builds");+ assert_eq!(+ url,+ "https://resource.services.ai.azure.com/anthropic/v1/messages"+ );+ }++ #[test]+ fn url_keeps_existing_anthropic_segment() {+ let url = complete_azure_anthropic_url(+ Some("https://resource.services.ai.azure.com/anthropic"),+ &|_| None,+ )+ .expect("url builds");+ assert_eq!(+ url,+ "https://resource.services.ai.azure.com/anthropic/v1/messages"+ );+ }++ #[test]+ fn url_leaves_complete_messages_endpoint_untouched() {+ for base in [+ "https://resource.services.ai.azure.com/anthropic/v1/messages",+ "https://resource.services.ai.azure.com/v1/messages",+ ] {+ assert_eq!(+ complete_azure_anthropic_url(Some(base), &|_| None).expect("url builds"),+ base+ );+ }+ }++ #[test]+ fn url_trims_trailing_slash_and_truncates_after_anthropic() {+ let url = complete_azure_anthropic_url(+ Some("https://resource.services.ai.azure.com/anthropic/extra/"),+ &|_| None,+ )+ .expect("url builds");+ assert_eq!(+ url,+ "https://resource.services.ai.azure.com/anthropic/v1/messages"+ );+ }++ #[test]+ fn url_falls_back_to_env_then_errors_when_absent() {+ let with_env = |key: &str| {+ (key == AZURE_API_BASE_ENV).then(|| "https://env.services.ai.azure.com".to_string())+ };+ assert_eq!(+ complete_azure_anthropic_url(None, &with_env).expect("url builds"),+ "https://env.services.ai.azure.com/anthropic/v1/messages"+ );+ let err = complete_azure_anthropic_url(Some(" "), &|_| None).expect_err("missing base");+ assert!(matches!(err, CoreError::Auth(_)));+ }++ #[test]+ fn resolve_api_key_prefers_param_then_env() {+ assert_eq!(+ resolve_azure_api_key(Some("sk-param"), &|_| None).unwrap(),+ "sk-param"+ );+ let with_env = |key: &str| (key == AZURE_API_KEY_ENV).then(|| "sk-env".to_string());+ assert_eq!(+ resolve_azure_api_key(Some(" "), &with_env).unwrap(),+ "sk-env"+ );+ assert!(matches!(+ resolve_azure_api_key(None, &|_| None).expect_err("missing key"),+ CoreError::Auth(_)+ ));+ }++ #[test]+ fn auth_strategy_is_x_api_key() {+ assert_eq!(+ AZURE_ANTHROPIC_MESSAGES_CONFIG+ .auth_strategy()+ .header_name(),+ "x-api-key"+ );+ }++ #[test]+ fn default_headers_match_python() {+ assert_eq!(+ AZURE_ANTHROPIC_MESSAGES_CONFIG.default_headers(),+ &[+ ("anthropic-version", "2023-06-01"),+ ("content-type", "application/json"),+ ]+ );+ }++ #[test]+ fn transform_request_strips_scope_from_system_and_messages() {+ let request = request_from(json!({+ "model": "claude-sonnet-4-5",+ "max_tokens": 1024,+ "system": [+ {+ "type": "text",+ "text": "sys",+ "cache_control": {"type": "ephemeral", "ttl": "1h", "scope": "global"}+ }+ ],+ "messages": [+ {+ "role": "user",+ "content": [+ {+ "type": "text",+ "text": "hi",+ "cache_control": {"type": "ephemeral", "scope": "global"}+ },+ {"type": "text", "text": "no cache control"}+ ]+ }+ ]+ }));++ let transformed = to_value(+ AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_request(request)+ .expect("request transforms"),+ );++ assert_eq!(+ transformed["system"][0]["cache_control"],+ json!({"type": "ephemeral", "ttl": "1h"})+ );+ assert_eq!(+ transformed["messages"][0]["content"][0]["cache_control"],+ json!({"type": "ephemeral"})+ );+ assert_eq!(+ transformed["messages"][0]["content"][1],+ json!({"type": "text", "text": "no cache control"})+ );+ }++ #[test]+ fn transform_request_is_idempotent_and_preserves_string_system() {+ let request = request_from(json!({+ "model": "claude-sonnet-4-5",+ "max_tokens": 16,+ "system": "plain string system",+ "messages": [{"role": "user", "content": "hi"}]+ }));+ let once = AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_request(request)+ .expect("request transforms");+ let twice = AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_request(once.clone())+ .expect("request transforms");+ assert_eq!(once, twice);+ assert_eq!(to_value(once)["system"], json!("plain string system"));+ }++ #[test]+ fn transform_request_preserves_all_supported_params() {+ let body = json!({+ "model": "claude-sonnet-4-5",+ "max_tokens": 256,+ "messages": [{"role": "user", "content": "hi"}],+ "system": "be terse",+ "metadata": {"user_id": "u1"},+ "stop_sequences": ["STOP"],+ "stream": false,+ "temperature": 0.4,+ "top_p": 0.9,+ "top_k": 40,+ "tools": [{"name": "get_weather", "input_schema": {"type": "object"}}],+ "tool_choice": {"type": "auto"},+ "thinking": {"type": "enabled", "budget_tokens": 1024},+ "service_tier": "auto",+ "container": {"id": "c1"},+ "mcp_servers": [{"type": "url", "url": "https://mcp.example", "name": "x"}],+ "context_management": {"edits": []},+ "output_format": {"type": "json_schema"},+ "output_config": {"effort": "high"},+ "speed": "fast",+ "inference_geo": "us",+ "litellm_metadata": {"trace": "abc"}+ });+ let transformed = to_value(+ AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_request(request_from(body.clone()))+ .expect("request transforms"),+ );+ assert_eq!(transformed, body);+ }++ #[test]+ fn transform_request_folds_system_role_message_into_top_level_system() {+ let request = request_from(json!({+ "model": "claude-sonnet-4-5",+ "max_tokens": 256,+ "system": [{"type": "text", "text": "base system"}],+ "messages": [+ {"role": "user", "content": "fix the bug"},+ {"role": "system", "content": "Available agent types: claude"}+ ]+ }));++ let transformed = to_value(+ AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_request(request)+ .expect("request transforms"),+ );++ assert_eq!(+ transformed["messages"],+ json!([{"role": "user", "content": "fix the bug"}])+ );+ assert_eq!(+ transformed["system"],+ json!([+ {"type": "text", "text": "base system"},+ {"type": "text", "text": "Available agent types: claude"}+ ])+ );+ }++ #[test]+ fn transform_request_folds_system_role_when_no_top_level_system() {+ let request = request_from(json!({+ "model": "claude-sonnet-4-5",+ "max_tokens": 256,+ "messages": [+ {"role": "user", "content": [{"type": "text", "text": "hi"}]},+ {"role": "system", "content": [{"type": "text", "text": "sys block"}]}+ ]+ }));++ let transformed = to_value(+ AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_request(request)+ .expect("request transforms"),+ );++ assert_eq!(+ transformed["messages"],+ json!([{"role": "user", "content": [{"type": "text", "text": "hi"}]}])+ );+ assert_eq!(+ transformed["system"],+ json!([{"type": "text", "text": "sys block"}])+ );+ }++ #[test]+ fn transform_request_leaves_requests_without_system_role_untouched() {+ let body = json!({+ "model": "claude-sonnet-4-5",+ "max_tokens": 256,+ "system": "be terse",+ "messages": [+ {"role": "user", "content": "hi"},+ {"role": "assistant", "content": "hello"}+ ]+ });+ let transformed = to_value(+ AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_request(request_from(body.clone()))+ .expect("request transforms"),+ );+ assert_eq!(transformed, body);+ }++ #[test]+ fn transform_request_rejects_non_object_body() {+ let err = serde_json::from_value::<AnthropicMessagesRequest>(json!("bad"))+ .expect_err("non-object body should error");+ assert!(err.is_data());+ }++ #[test]+ fn transform_response_passes_through() {+ let response: AnthropicMessagesResponse = serde_json::from_value(json!({+ "id": "msg_1",+ "type": "message",+ "role": "assistant",+ "content": [{"type": "text", "text": "hello"}],+ "model": "claude-sonnet-4-5",+ "stop_reason": "end_turn",+ "stop_sequence": null,+ "usage": {"input_tokens": 1, "output_tokens": 2}+ }))+ .expect("valid response");+ let transformed = AZURE_ANTHROPIC_MESSAGES_CONFIG+ .transform_response("claude-sonnet-4-5", response)+ .expect("response transforms");+ let value = serde_json::to_value(transformed).expect("serializable");+ assert_eq!(value["stop_reason"], json!("end_turn"));+ assert_eq!(value["stop_sequence"], json!(null));+ assert_eq!(value["content"][0]["text"], json!("hello"));+ }+}diff --git a/litellm-rust/crates/core/src/providers/azure_ai/mod.rs b/litellm-rust/crates/core/src/providers/azure_ai/mod.rsindex 3621ff6a2fd..5d13fa93e00 100644--- a/litellm-rust/crates/core/src/providers/azure_ai/mod.rs+++ b/litellm-rust/crates/core/src/providers/azure_ai/mod.rs@@ -1 +1,2 @@+pub mod messages;pub mod ocr;diff --git a/litellm-rust/crates/core/src/providers/mod.rs b/litellm-rust/crates/core/src/providers/mod.rsindex d75e750a0ba..dc9dc515e7d 100644--- a/litellm-rust/crates/core/src/providers/mod.rs+++ b/litellm-rust/crates/core/src/providers/mod.rs@@ -1,3 +1,4 @@+pub mod anthropic;pub mod azure_ai;pub mod mistral;pub mod openai;diff --git a/litellm-rust/crates/python-bridge/src/lib.rs b/litellm-rust/crates/python-bridge/src/lib.rsindex 271864581f3..77f4427127a 100644--- a/litellm-rust/crates/python-bridge/src/lib.rs+++ b/litellm-rust/crates/python-bridge/src/lib.rs@@ -1,5 +1,6 @@use std::time::Duration;+use litellm_ai_gateway::io::messages::{messages as run_messages, MessagesRequest};use litellm_ai_gateway::io::ocr::{ocr as run_ocr, OcrRequest};use litellm_core::error::CoreError;use pyo3::exceptions::{PyRuntimeError, PyValueError};@@ -171,6 +172,92 @@ fn aocr(})}+type MarshaledMessagesInputs = (Value, Option<Map<String, Value>>, Option<Duration>);++fn marshal_messages_inputs(+ py: Python<'_>,+ body: Py<PyAny>,+ extra_headers: Option<Py<PyAny>>,+ timeout_seconds: Option<f64>,+) -> PyResult<MarshaledMessagesInputs> {+ let body = py_to_json(py, body.bind(py))?;+ if !body.is_object() {+ return Err(PyValueError::new_err("body must be a dict"));+ }+ let extra_headers = match extra_headers {+ Some(headers) => Some(optional_object_to_map(py, "extra_headers", Some(headers))?),+ None => None,+ };+ Ok((body, extra_headers, optional_timeout(timeout_seconds)))+}++#[pyfunction]+#[pyo3(signature = (model, body, api_key=None, api_base=None, custom_llm_provider=None, extra_headers=None, timeout_seconds=None))]+#[allow(clippy::too_many_arguments)]+fn messages(+ py: Python<'_>,+ model: String,+ body: Py<PyAny>,+ api_key: Option<String>,+ api_base: Option<String>,+ custom_llm_provider: Option<String>,+ extra_headers: Option<Py<PyAny>>,+ timeout_seconds: Option<f64>,+) -> PyResult<Py<PyAny>> {+ let (body, extra_headers, timeout) =+ marshal_messages_inputs(py, body, extra_headers, timeout_seconds)?;++ let result = gil::release_gil(py, || {+ pyo3_async_runtimes::tokio::get_runtime().block_on(run_messages(MessagesRequest {+ model: &model,+ body,+ api_key: api_key.as_deref(),+ api_base: api_base.as_deref(),+ custom_llm_provider: custom_llm_provider.as_deref(),+ extra_headers,+ timeout,+ }))+ });++ match result {+ Ok(value) => json_to_py(py, value),+ Err(err) => Err(core_error_to_pyerr(err)),+ }+}++#[pyfunction]+#[pyo3(signature = (model, body, api_key=None, api_base=None, custom_llm_provider=None, extra_headers=None, timeout_seconds=None))]+#[allow(clippy::too_many_arguments)]+fn amessages(+ py: Python<'_>,+ model: String,+ body: Py<PyAny>,+ api_key: Option<String>,+ api_base: Option<String>,+ custom_llm_provider: Option<String>,+ extra_headers: Option<Py<PyAny>>,+ timeout_seconds: Option<f64>,+) -> PyResult<Bound<'_, PyAny>> {+ let (body, extra_headers, timeout) =+ marshal_messages_inputs(py, body, extra_headers, timeout_seconds)?;++ pyo3_async_runtimes::tokio::future_into_py(py, async move {+ let value = run_messages(MessagesRequest {+ model: &model,+ body,+ api_key: api_key.as_deref(),+ api_base: api_base.as_deref(),+ custom_llm_provider: custom_llm_provider.as_deref(),+ extra_headers,+ timeout,+ })+ .await+ .map_err(core_error_to_pyerr)?;++ Python::attach(|py| json_to_py(py, value))+ })+}+#[pyfunction]fn gil_stats(py: Python<'_>) -> PyResult<Py<PyAny>> {let stats = PyDict::new(py);@@ -182,6 +269,8 @@ fn gil_stats(py: Python<'_>) -> PyResult<Py<PyAny>> {fn _native(module: &Bound<'_, PyModule>) -> PyResult<()> {module.add_function(wrap_pyfunction!(ocr, module)?)?;module.add_function(wrap_pyfunction!(aocr, module)?)?;+ module.add_function(wrap_pyfunction!(messages, module)?)?;+ module.add_function(wrap_pyfunction!(amessages, module)?)?;module.add_function(wrap_pyfunction!(gil_stats, module)?)?;Ok(())}diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.pyindex b47fc50e196..3e6f9ee08ee 100644--- a/litellm/llms/custom_httpx/llm_http_handler.py+++ b/litellm/llms/custom_httpx/llm_http_handler.py@@ -153,6 +153,9 @@ if TYPE_CHECKING:from aiohttp import ClientSessionfrom litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj+ from litellm.llms.anthropic.experimental_pass_through.messages.streaming_iterator import (+ AnthropicMessagesStreamingResponse,+ )from litellm.llms.base_llm.passthrough.transformation import BasePassthroughConfigfrom litellm.types.llms.openai_evals import (CancelEvalResponse,@@ -2091,6 +2094,37 @@ class BaseLLMHTTPHandler:},)+ rust_messages_response = await self._maybe_rust_anthropic_messages(+ custom_llm_provider=custom_llm_provider,+ litellm_params=litellm_params,+ stream=stream or False,+ rust_stream_eligible=bool(stream) and not self._has_agentic_completion_hook(logging_obj),+ model=model,+ api_key=api_key,+ api_base=api_base,+ headers=headers,+ request_body=request_body,+ timeout=self._resolve_anthropic_messages_timeout(+ litellm_params=litellm_params,+ stream=stream or False,+ custom_llm_provider=custom_llm_provider,+ ),+ )+ if rust_messages_response is not None:+ if stream:+ return self._rust_anthropic_messages_fake_stream(rust_messages_response)+ return await self._finalize_anthropic_messages_response(+ initial_response=rust_messages_response,+ model=model,+ messages=messages,+ anthropic_messages_provider_config=anthropic_messages_provider_config,+ anthropic_messages_optional_request_params=anthropic_messages_optional_request_params,+ logging_obj=logging_obj,+ custom_llm_provider=custom_llm_provider,+ api_key=api_key,+ kwargs=kwargs,+ )+response = await self._async_post_anthropic_messages_with_http_error_retry(async_httpx_client=async_httpx_client,request_url=request_url,@@ -2165,6 +2199,31 @@ class BaseLLMHTTPHandler:logging_obj=logging_obj,)+ return await self._finalize_anthropic_messages_response(+ initial_response=initial_response,+ model=model,+ messages=messages,+ anthropic_messages_provider_config=anthropic_messages_provider_config,+ anthropic_messages_optional_request_params=anthropic_messages_optional_request_params,+ logging_obj=logging_obj,+ custom_llm_provider=custom_llm_provider,+ api_key=api_key,+ kwargs=kwargs,+ )++ async def _finalize_anthropic_messages_response(+ self,+ *,+ initial_response: AnthropicMessagesResponse,+ model: str,+ messages: list[dict],+ anthropic_messages_provider_config: BaseAnthropicMessagesConfig,+ anthropic_messages_optional_request_params: dict,+ logging_obj: LiteLLMLoggingObj,+ custom_llm_provider: str,+ api_key: str | None,+ kwargs: dict,+ ) -> AnthropicMessagesResponse | AsyncIterator:# Inject api_key into kwargs so follow-up calls in agentic hooks can# authenticate. api_key is a named param here (not in kwargs), so# _prepare_followup_kwargs would miss it otherwise.@@ -2188,6 +2247,70 @@ class BaseLLMHTTPHandler:"anthropic_messages",)+ @staticmethod+ async def _maybe_rust_anthropic_messages(+ *,+ custom_llm_provider: str,+ litellm_params: GenericLiteLLMParams,+ stream: bool,+ rust_stream_eligible: bool,+ model: str,+ api_key: str | None,+ api_base: str | None,+ headers: dict,+ request_body: dict,+ timeout: float | httpx.Timeout | None,+ ) -> AnthropicMessagesResponse | None:+ if custom_llm_provider != "azure_ai" or litellm_params.get("rust") is not True:+ return None+ if stream and not rust_stream_eligible:+ return None++ from litellm.rust_bridge import messages as rust_messages_bridge++ upstream_body = {key: value for key, value in request_body.items() if key != "stream"}+ try:+ rust_response = await rust_messages_bridge.amessages(+ model=model,+ body=upstream_body,+ api_key=api_key,+ api_base=api_base,+ custom_llm_provider=custom_llm_provider,+ extra_headers=headers,+ timeout=timeout,+ )+ except Exception as rust_error: # noqa: BLE001 # rollout-safety fallback: any Rust bridge failure must fall back to the Python path+ verbose_logger.debug(+ "Rust Anthropic messages bridge raised %s; falling back to Python path",+ type(rust_error).__name__,+ )+ return None+ if rust_response is None:+ return None++ response_obj = cast(AnthropicMessagesResponse, dict(rust_response))+ response_obj["_hidden_params"] = {"additional_headers": {"x-litellm-rust": "true"}}+ return response_obj++ @staticmethod+ def _rust_anthropic_messages_fake_stream(+ rust_response: AnthropicMessagesResponse,+ ) -> "AnthropicMessagesStreamingResponse":+ from litellm.llms.anthropic.experimental_pass_through.messages.fake_stream_iterator import (+ FakeAnthropicMessagesStreamIterator,+ )+ from litellm.llms.anthropic.experimental_pass_through.messages.streaming_iterator import (+ AnthropicMessagesStreamHiddenParams,+ AnthropicMessagesStreamingResponse,+ )++ completion_stream = cast(AsyncIterator[bytes], FakeAnthropicMessagesStreamIterator(response=rust_response))+ hidden_params = AnthropicMessagesStreamHiddenParams(additional_headers={"x-litellm-rust": "true"})+ return AnthropicMessagesStreamingResponse(+ completion_stream=completion_stream,+ hidden_params=hidden_params,+ )+def anthropic_messages_handler(self,model: str,diff --git a/litellm/rust_bridge/messages.py b/litellm/rust_bridge/messages.pynew file mode 100644index 00000000000..5abb21879d3--- /dev/null+++ b/litellm/rust_bridge/messages.py@@ -0,0 +1,135 @@+"""Thin Python wrapper for the native Rust Anthropic Messages bridge."""++from __future__ import annotations++from dataclasses import dataclass+from typing import Awaitable, Final, Protocol, Union, cast++import httpx++from litellm.rust_bridge.timeouts import timeout_to_seconds+++class RustMessages(Protocol):+ def __call__(+ self,+ model: str,+ body: dict[str, object],+ api_key: str | None,+ api_base: str | None,+ custom_llm_provider: str | None,+ extra_headers: dict[str, object] | None,+ timeout_seconds: float | None,+ ) -> dict[str, object]:+ raise NotImplementedError+++class RustAmessages(Protocol):+ def __call__(+ self,+ model: str,+ body: dict[str, object],+ api_key: str | None,+ api_base: str | None,+ custom_llm_provider: str | None,+ extra_headers: dict[str, object] | None,+ timeout_seconds: float | None,+ ) -> Awaitable[dict[str, object]]:+ raise NotImplementedError+++class _Unset:+ pass+++_UNSET: Final[_Unset] = _Unset()+++@dataclass(slots=True)+class _RustMessagesState:+ messages: RustMessages | None = None+ amessages: RustAmessages | None = None+++_STATE: Final[_RustMessagesState] = _RustMessagesState()+++def set_rust_messages(+ *,+ messages: RustMessages | None | _Unset = _UNSET,+ amessages: RustAmessages | None | _Unset = _UNSET,+) -> None:+ if not isinstance(messages, _Unset):+ _STATE.messages = messages+ if not isinstance(amessages, _Unset):+ _STATE.amessages = amessages+++def load_rust_messages() -> RustMessages | None:+ if _STATE.messages is not None:+ return _STATE.messages+ from litellm.rust_bridge import get_native_bridge++ native_bridge = get_native_bridge()+ if native_bridge is None:+ return None+ return cast(RustMessages, getattr(native_bridge, "messages", None))+++def load_rust_amessages() -> RustAmessages | None:+ if _STATE.amessages is not None:+ return _STATE.amessages+ from litellm.rust_bridge import get_native_bridge++ native_bridge = get_native_bridge()+ if native_bridge is None:+ return None+ return cast(RustAmessages, getattr(native_bridge, "amessages", None))+++def messages(+ *,+ model: str,+ body: dict[str, object],+ api_key: str | None,+ api_base: str | None,+ custom_llm_provider: str | None,+ extra_headers: dict[str, object] | None,+ timeout: Union[float, httpx.Timeout] | None,+) -> dict[str, object] | None:+ rust_messages = load_rust_messages()+ if rust_messages is None:+ return None+ return rust_messages(+ model=model,+ body=body,+ api_key=api_key,+ api_base=api_base,+ custom_llm_provider=custom_llm_provider,+ extra_headers=extra_headers,+ timeout_seconds=timeout_to_seconds(timeout),+ )+++async def amessages(+ *,+ model: str,+ body: dict[str, object],+ api_key: str | None,+ api_base: str | None,+ custom_llm_provider: str | None,+ extra_headers: dict[str, object] | None,+ timeout: Union[float, httpx.Timeout] | None,+) -> dict[str, object] | None:+ rust_amessages = load_rust_amessages()+ if rust_amessages is None:+ return None+ return await rust_amessages(+ model=model,+ body=body,+ api_key=api_key,+ api_base=api_base,+ custom_llm_provider=custom_llm_provider,+ extra_headers=extra_headers,+ timeout_seconds=timeout_to_seconds(timeout),+ )diff --git a/litellm/rust_bridge/ocr.py b/litellm/rust_bridge/ocr.pyindex 36a088b6b1a..35de2eb9727 100644--- a/litellm/rust_bridge/ocr.py+++ b/litellm/rust_bridge/ocr.py@@ -3,10 +3,15 @@from __future__ import annotationsimport os-from typing import Any, Awaitable, Final, Protocol, Union, cast+from typing import TYPE_CHECKING, Awaitable, Final, Protocol, Union, castimport httpx+from litellm.rust_bridge.timeouts import timeout_to_seconds as _timeout_to_seconds++if TYPE_CHECKING:+ from litellm.rust_bridge.messages import RustAmessages, RustMessages+class RustOcr(Protocol):def __call__(@@ -64,13 +69,28 @@ def use_litellm_rust(*,ocr: RustOcr | None | _Unset = _UNSET,aocr: RustAocr | None | _Unset = _UNSET,+ messages: RustMessages | None | _Unset = _UNSET,+ amessages: RustAmessages | None | _Unset = _UNSET,) -> None:global _rust_ocr_enabled, _rust_ocr_impl, _rust_aocr_impl- _rust_ocr_enabled = enabled+ configuring_ocr = not isinstance(ocr, _Unset) or not isinstance(aocr, _Unset)+ configuring_messages = not isinstance(messages, _Unset) or not isinstance(amessages, _Unset)+ if configuring_ocr or not configuring_messages:+ _rust_ocr_enabled = enabledif not isinstance(ocr, _Unset):_rust_ocr_impl = ocrif not isinstance(aocr, _Unset):_rust_aocr_impl = aocr+ if not configuring_messages:+ return+ from litellm.rust_bridge.messages import set_rust_messages++ if not isinstance(messages, _Unset) and not isinstance(amessages, _Unset):+ set_rust_messages(messages=messages, amessages=amessages)+ elif not isinstance(messages, _Unset):+ set_rust_messages(messages=messages)+ else:+ set_rust_messages(amessages=amessages)def rust_ocr_enabled() -> bool:@@ -99,22 +119,14 @@ def load_rust_aocr() -> RustAocr | None:return cast(RustAocr, getattr(native_bridge, "aocr", None))-def _timeout_to_seconds(timeout: Union[float, httpx.Timeout] | None) -> float | None:- if timeout is None:- return None- if isinstance(timeout, httpx.Timeout):- return timeout.read- return float(timeout)--def ocr(*,model: str,- document: dict[str, Any],+ document: dict[str, object],api_key: str | None,api_base: str | None,custom_llm_provider: str | None,- extra_headers: dict[str, Any] | None,+ extra_headers: dict[str, object] | None,optional_params: dict[str, object],timeout: Union[float, httpx.Timeout] | None,) -> dict[str, object] | None:@@ -123,11 +135,11 @@ def ocr(return Nonereturn rust_ocr(model=model,- document=cast(dict[str, object], document),+ document=document,api_key=api_key,api_base=api_base,custom_llm_provider=custom_llm_provider,- extra_headers=cast(dict[str, object] | None, extra_headers),+ extra_headers=extra_headers,optional_params=optional_params,timeout_seconds=_timeout_to_seconds(timeout),)@@ -136,11 +148,11 @@ def ocr(async def aocr(*,model: str,- document: dict[str, Any],+ document: dict[str, object],api_key: str | None,api_base: str | None,custom_llm_provider: str | None,- extra_headers: dict[str, Any] | None,+ extra_headers: dict[str, object] | None,optional_params: dict[str, object],timeout: Union[float, httpx.Timeout] | None,) -> dict[str, object] | None:@@ -149,11 +161,11 @@ async def aocr(return Nonereturn await rust_aocr(model=model,- document=cast(dict[str, object], document),+ document=document,api_key=api_key,api_base=api_base,custom_llm_provider=custom_llm_provider,- extra_headers=cast(dict[str, object] | None, extra_headers),+ extra_headers=extra_headers,optional_params=optional_params,timeout_seconds=_timeout_to_seconds(timeout),)diff --git a/litellm/rust_bridge/timeouts.py b/litellm/rust_bridge/timeouts.pynew file mode 100644index 00000000000..4407986c3da--- /dev/null+++ b/litellm/rust_bridge/timeouts.py@@ -0,0 +1,15 @@+"""Shared timeout conversion for the native Rust bridges."""++from __future__ import annotations++from typing import Union++import httpx+++def timeout_to_seconds(timeout: Union[float, httpx.Timeout] | None) -> float | None:+ if timeout is None:+ return None+ if isinstance(timeout, httpx.Timeout):+ return timeout.read+ return float(timeout)diff --git a/tests/test_litellm/anthropic_interface/test_rust_bridge_messages.py b/tests/test_litellm/anthropic_interface/test_rust_bridge_messages.pynew file mode 100644index 00000000000..26ca7d27210--- /dev/null+++ b/tests/test_litellm/anthropic_interface/test_rust_bridge_messages.py@@ -0,0 +1,354 @@+"""Tests for the optional Rust-backed Anthropic Messages path."""++import importlib+from typing import cast++import httpx+import pytest++import litellm+from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler+from litellm.types.llms.anthropic_messages.anthropic_response import (+ AnthropicMessagesResponse,+)+from litellm.types.router import GenericLiteLLMParams++rust_messages = importlib.import_module("litellm.rust_bridge.messages")+rust_bridge_loader = importlib.import_module("litellm.rust_bridge.loader")++FAKE_MESSAGES_RESPONSE: dict[str, object] = {+ "id": "msg_123",+ "type": "message",+ "role": "assistant",+ "model": "claude-sonnet-4-5-20250929",+ "content": [{"type": "text", "text": "hello world"}],+ "stop_reason": "end_turn",+ "usage": {"input_tokens": 5, "output_tokens": 3},+}++REQUEST_BODY: dict[str, object] = {+ "model": "claude-sonnet-4-5",+ "max_tokens": 64,+ "messages": [{"role": "user", "content": "hi"}],+}+++class RecordingMessages:+ def __init__(self) -> None:+ self.calls: list[dict[str, object]] = []++ def __call__(+ self,+ model: str,+ body: dict[str, object],+ api_key: str | None,+ api_base: str | None,+ custom_llm_provider: str | None,+ extra_headers: dict[str, object] | None,+ timeout_seconds: float | None,+ ) -> dict[str, object]:+ self.calls.append(+ {+ "model": model,+ "body": body,+ "api_key": api_key,+ "api_base": api_base,+ "custom_llm_provider": custom_llm_provider,+ "extra_headers": extra_headers,+ "timeout_seconds": timeout_seconds,+ }+ )+ return dict(FAKE_MESSAGES_RESPONSE)+++class RecordingAsyncMessages:+ def __init__(self) -> None:+ self.calls: list[dict[str, object]] = []++ async def __call__(+ self,+ model: str,+ body: dict[str, object],+ api_key: str | None,+ api_base: str | None,+ custom_llm_provider: str | None,+ extra_headers: dict[str, object] | None,+ timeout_seconds: float | None,+ ) -> dict[str, object]:+ self.calls.append(+ {+ "model": model,+ "body": body,+ "api_key": api_key,+ "api_base": api_base,+ "custom_llm_provider": custom_llm_provider,+ "extra_headers": extra_headers,+ "timeout_seconds": timeout_seconds,+ }+ )+ return dict(FAKE_MESSAGES_RESPONSE)+++class ExplodingAsyncMessages:+ def __init__(self) -> None:+ self.calls = 0++ async def __call__(self, **kwargs: object) -> dict[str, object]:+ self.calls += 1+ raise AssertionError("bridge must not be called")+++class RaisingAsyncMessages:+ def __init__(self) -> None:+ self.calls = 0++ async def __call__(self, **kwargs: object) -> dict[str, object]:+ self.calls += 1+ raise RuntimeError("upstream request failed with status 400: bad request")++[email protected](autouse=True)+def _reset_rust_flag():+ litellm.use_litellm_rust(False, messages=None, amessages=None)+ rust_bridge_loader._cached_bridge = rust_bridge_loader._BRIDGE_SENTINEL+ yield+ litellm.use_litellm_rust(False, messages=None, amessages=None)+ rust_bridge_loader._cached_bridge = rust_bridge_loader._BRIDGE_SENTINEL+++def test_load_rust_messages_returns_injected_impl():+ bridge = RecordingMessages()+ litellm.use_litellm_rust(True, messages=bridge)+ assert rust_messages.load_rust_messages() is bridge+++def test_configuring_messages_does_not_enable_ocr():+ from litellm.rust_bridge.ocr import rust_ocr_enabled++ litellm.use_litellm_rust(False)+ assert rust_ocr_enabled() is False++ litellm.use_litellm_rust(True, messages=RecordingMessages())++ assert rust_ocr_enabled() is False+++def test_bare_use_litellm_rust_still_toggles_ocr():+ from litellm.rust_bridge.ocr import rust_ocr_enabled++ litellm.use_litellm_rust(True)+ assert rust_ocr_enabled() is True++ litellm.use_litellm_rust(False)+ assert rust_ocr_enabled() is False+++def test_load_rust_amessages_returns_injected_impl():+ bridge = RecordingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)+ assert rust_messages.load_rust_amessages() is bridge+++def test_messages_wrapper_returns_none_when_bridge_absent(monkeypatch):+ monkeypatch.setattr(+ importlib.import_module("litellm.rust_bridge"),+ "get_native_bridge",+ lambda: None,+ )+ litellm.use_litellm_rust(True)+ assert rust_messages.load_rust_messages() is None+ result = rust_messages.messages(+ model="claude",+ body=REQUEST_BODY,+ api_key="k",+ api_base="b",+ custom_llm_provider="azure_ai",+ extra_headers={},+ timeout=30.0,+ )+ assert result is None+++def test_messages_wrapper_forwards_args_and_converts_timeout():+ bridge = RecordingMessages()+ litellm.use_litellm_rust(True, messages=bridge)++ response = rust_messages.messages(+ model="claude-sonnet-4-5",+ body=REQUEST_BODY,+ api_key="sk-azure",+ api_base="https://resource.services.ai.azure.com/anthropic",+ custom_llm_provider="azure_ai",+ extra_headers={"anthropic-beta": "token-efficient-tools-2025-02-19"},+ timeout=httpx.Timeout(600.0, read=42.0),+ )++ assert response == FAKE_MESSAGES_RESPONSE+ assert bridge.calls[0] == {+ "model": "claude-sonnet-4-5",+ "body": REQUEST_BODY,+ "api_key": "sk-azure",+ "api_base": "https://resource.services.ai.azure.com/anthropic",+ "custom_llm_provider": "azure_ai",+ "extra_headers": {"anthropic-beta": "token-efficient-tools-2025-02-19"},+ "timeout_seconds": 42.0,+ }+++async def test_amessages_wrapper_forwards_args():+ bridge = RecordingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ response = await rust_messages.amessages(+ model="claude-sonnet-4-5",+ body=REQUEST_BODY,+ api_key="sk-azure",+ api_base="https://resource.services.ai.azure.com/anthropic",+ custom_llm_provider="azure_ai",+ extra_headers=None,+ timeout=12.5,+ )++ assert response == FAKE_MESSAGES_RESPONSE+ assert bridge.calls[0]["model"] == "claude-sonnet-4-5"+ assert bridge.calls[0]["timeout_seconds"] == 12.5+++def _gate(**overrides):+ kwargs = {+ "custom_llm_provider": "azure_ai",+ "litellm_params": GenericLiteLLMParams(api_key="sk-azure", rust=True),+ "stream": False,+ "rust_stream_eligible": False,+ "model": "claude-sonnet-4-5",+ "api_key": "sk-azure",+ "api_base": "https://resource.services.ai.azure.com/anthropic",+ "headers": {"x-api-key": "sk-azure", "anthropic-version": "2023-06-01"},+ "request_body": dict(REQUEST_BODY),+ "timeout": 30.0,+ }+ kwargs.update(overrides)+ return BaseLLMHTTPHandler._maybe_rust_anthropic_messages(**kwargs)+++async def test_gate_invokes_rust_and_marks_response_header():+ bridge = RecordingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ response = await _gate()++ assert response is not None+ assert response["id"] == "msg_123"+ assert response["_hidden_params"]["additional_headers"] == {"x-litellm-rust": "true"}+ call = bridge.calls[0]+ assert call["model"] == "claude-sonnet-4-5"+ assert call["body"] == REQUEST_BODY+ assert call["api_key"] == "sk-azure"+ assert call["api_base"] == "https://resource.services.ai.azure.com/anthropic"+ assert call["extra_headers"] == {"x-api-key": "sk-azure", "anthropic-version": "2023-06-01"}+ assert call["timeout_seconds"] == 30.0+++async def test_gate_falls_back_to_python_when_bridge_raises():+ bridge = RaisingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ response = await _gate()++ assert response is None+ assert bridge.calls == 1+++async def test_gate_skips_rust_when_flag_absent():+ bridge = ExplodingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ response = await _gate(litellm_params=GenericLiteLLMParams(api_key="sk-azure"))++ assert response is None+ assert bridge.calls == 0+++async def test_gate_skips_rust_when_flag_false():+ bridge = ExplodingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ response = await _gate(litellm_params=GenericLiteLLMParams(api_key="sk-azure", rust=False))++ assert response is None+ assert bridge.calls == 0+++async def test_gate_skips_rust_for_non_azure_provider():+ bridge = ExplodingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ response = await _gate(custom_llm_provider="anthropic")++ assert response is None+ assert bridge.calls == 0+++async def test_gate_skips_rust_when_streaming_but_not_eligible():+ bridge = ExplodingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ response = await _gate(stream=True, rust_stream_eligible=False)++ assert response is None+ assert bridge.calls == 0+++async def test_gate_streams_through_rust_when_eligible_and_strips_stream_flag():+ bridge = RecordingAsyncMessages()+ litellm.use_litellm_rust(True, amessages=bridge)++ streaming_body = {**REQUEST_BODY, "stream": True}+ response = await _gate(+ stream=True,+ rust_stream_eligible=True,+ request_body=streaming_body,+ )++ assert response is not None+ assert response["_hidden_params"]["additional_headers"] == {"x-litellm-rust": "true"}+ assert "stream" not in bridge.calls[0]["body"]+ assert bridge.calls[0]["body"] == REQUEST_BODY+++async def test_fake_stream_wraps_rust_response_as_anthropic_sse():+ response = cast(AnthropicMessagesResponse, dict(FAKE_MESSAGES_RESPONSE))+ stream = BaseLLMHTTPHandler._rust_anthropic_messages_fake_stream(response)++ assert stream._hidden_params["additional_headers"] == {"x-litellm-rust": "true"}++ chunks = [chunk async for chunk in stream]+ joined = b"".join(chunks)++ assert b"event: message_start" in joined+ assert b"event: content_block_delta" in joined+ assert b"hello world" in joined+ assert b"event: message_stop" in joined+++async def test_gate_falls_back_when_bridge_unavailable(monkeypatch):+ monkeypatch.setattr(+ importlib.import_module("litellm.rust_bridge"),+ "get_native_bridge",+ lambda: None,+ )+ litellm.use_litellm_rust(True)++ response = await _gate()++ assert response is Nonediff --git a/type-discipline-budget.json b/type-discipline-budget.jsonindex 87b4c96e323..0482f47e5bc 100644--- a/type-discipline-budget.json+++ b/type-discipline-budget.json@@ -15,7 +15,7 @@"limit": 0},"LIT006": {- "limit": 1113+ "limit": 1111},"LIT007": {"limit": 0