litellm/litellm-rust/crates/llms/tests/messages_normalization.rs
yujonglee ee295aea68
refactor(rust): give Messages configs typed litellm params (#45611)
* refactor(rust): give Messages configs typed litellm params

* feat(rust): port _normalize_system_role_messages for Messages hosts

mid_conversation_system mirrors Python's module function for function: a
leading run of system turns is hoisted into the top-level system, a later
turn stays in place when the model map flags supports_mid_conversation_system
and otherwise becomes a user turn where it was, never between a tool_use and
its tool_result, and billing blocks are stripped from the hoisted system. The
capability joins MessagesModelCapabilities and the bridge projection. Azure AI
uses it in place of fold_system_role_messages, which hoisted every turn.

* fix(rust): keep the Bedrock runtime endpoint behind a blank api_base

* refactor(rust): import LitellmParams from its crate instead of a re-export

* refactor(rust): template the Messages request decode errors

Both decode failures go through ErrorDetail::invalid with the subject and the
serde error as source instead of a preformatted sentence.

* refactor(rust_bridge): read the module globals the param specs name

The Messages host no longer keeps its own list of litellm globals; a spec
whose spellings the call leaves out names the global to read, so the next
family that has one needs no host change.

* refactor(rust): give each route lookup Python's implicit None

ProviderConfigManager's per-route methods list only the providers the route
serves and fall through to None; the Rust lookups now do the same, so a new
LlmProviders variant touches only the route that serves it.
2026-10-09 16:29:45 -07:00

53 lines
2 KiB
Rust

use litellm_llms::base_llm::messages::normalization::normalize_system_role_messages;
use litellm_llms_types::formats::messages::MessagesRequest;
use rstest::rstest;
use serde_json::{Value, json};
#[rstest]
#[case::text_system(json!("existing"), json!([{"type": "text", "text": "existing"}]))]
#[case::block_system(json!([{ "type": "future", "payload": 7 }]), json!([{ "type": "future", "payload": 7 }]))]
#[case::no_system(Value::Null, json!([]))]
fn hoisting_preserves_block_fields_order_and_unrelated_request_fields(
#[case] system: Value,
#[case] initial_blocks: Value,
) {
let cache_control = json!({"type": "ephemeral", "scope": "global", "future": true});
let leading_block = json!({"type": "text", "text": "second", "cache_control": cache_control});
let user = json!({"role": "user", "content": "hello", "future_message": 42});
let later_turn = json!({"role": "system", "content": "mid-conversation reminder"});
let request: MessagesRequest = serde_json::from_value(json!({
"model": "test-model",
"max_tokens": 64,
"system": system,
"messages": [
{"role": "system", "content": "first"},
{"role": "system", "content": [leading_block]},
user,
later_turn
],
"future_request": {"nested": true}
}))
.unwrap();
let normalized = normalize_system_role_messages(request, true);
let expected_blocks: Vec<Value> = initial_blocks
.as_array()
.unwrap()
.iter()
.cloned()
.chain([json!({"type": "text", "text": "first"}), leading_block])
.collect();
assert_eq!(
serde_json::to_value(&normalized).unwrap(),
json!({
"model": "test-model",
"max_tokens": 64,
"system": expected_blocks,
"messages": [user, later_turn],
"future_request": {"nested": true}
})
);
assert_eq!(
normalize_system_role_messages(normalized.clone(), true),
normalized
);
}