mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(messages): clamp bedrock effort before the drop_params effort gate (#51972)
This commit is contained in:
parent
58d86c2148
commit
d034fb681c
5 changed files with 86 additions and 6 deletions
|
|
@ -540,9 +540,20 @@ fn requested_optional_params(
|
|||
provider: MessagesProvider,
|
||||
context: &MessagesTransformContext,
|
||||
) -> Result<MessagesRequest, Error> {
|
||||
let request = litellm_llms::anthropic::messages::transformation::drop_unsupported_params(
|
||||
request, context,
|
||||
let requested_effort = request.params.reasoning_effort.clone();
|
||||
let clamped = provider
|
||||
.config()
|
||||
.clamp_requested_effort(request, &context.thinking.capabilities)?;
|
||||
let gated = litellm_llms::anthropic::messages::transformation::drop_unsupported_params(
|
||||
clamped, context,
|
||||
)?;
|
||||
let request = MessagesRequest {
|
||||
params: MessagesOptionalParams {
|
||||
reasoning_effort: gated.params.reasoning_effort.and(requested_effort),
|
||||
..gated.params
|
||||
},
|
||||
..gated
|
||||
};
|
||||
if provider.accepts_service_tier() {
|
||||
return Ok(request);
|
||||
}
|
||||
|
|
@ -1260,6 +1271,58 @@ mod tests {
|
|||
MessagesShaping::default()
|
||||
}
|
||||
|
||||
fn opus_4_6_on_bedrock() -> MessagesTransformContext {
|
||||
use litellm_llms::base_llm::messages::context::{
|
||||
BedrockCapabilities, MessagesModelCapabilities, SupportedEffortTiers,
|
||||
};
|
||||
MessagesTransformContext::new(
|
||||
MessagesModelCapabilities {
|
||||
supports_reasoning: true,
|
||||
supports_adaptive_thinking: true,
|
||||
supports_output_config: true,
|
||||
effort_tiers: SupportedEffortTiers {
|
||||
low: true,
|
||||
medium: true,
|
||||
high: true,
|
||||
max: true,
|
||||
..SupportedEffortTiers::default()
|
||||
},
|
||||
bedrock: BedrockCapabilities {
|
||||
output_config_effort_ceiling: Some(
|
||||
litellm_llms_types::formats::messages::EffortLevel::Max,
|
||||
),
|
||||
..BedrockCapabilities::default()
|
||||
},
|
||||
..MessagesModelCapabilities::default()
|
||||
},
|
||||
true,
|
||||
)
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::bedrock_clamps_before_the_gate("bedrock/anthropic.claude-opus-4-6-v1", Some(json!("xhigh")))]
|
||||
#[case::anthropic_has_no_clamp("anthropic/claude-opus-4-6", None)]
|
||||
fn drop_params_keeps_an_effort_the_bedrock_ceiling_clamps(
|
||||
#[case] model: &str,
|
||||
#[case] kept: Option<Value>,
|
||||
) {
|
||||
let resolved = resolve_llm_provider(model, None, "messages").unwrap();
|
||||
let provider = messages_provider(resolved.provider, resolved.model).unwrap();
|
||||
let requested = requested_optional_params(
|
||||
body(json!({"model": resolved.model, "max_tokens": 4096, "messages": [{"role": "user", "content": "hi"}], "reasoning_effort": "xhigh"})),
|
||||
provider,
|
||||
&opus_4_6_on_bedrock(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::to_value(&requested.params)
|
||||
.unwrap()
|
||||
.get("reasoning_effort")
|
||||
.cloned(),
|
||||
kept
|
||||
);
|
||||
}
|
||||
|
||||
fn body(value: Value) -> MessagesRequest {
|
||||
serde_json::from_value(value).unwrap()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
- Caller credentials go out exactly as Python's dict and client send them (owner ruling, 2026-11-13). A caller `api-key` spelled exactly so replaces the deployment key and the Entra ID path. Otherwise the deployment key is added as `api-key`, or the Entra ID token as `Authorization` (replacing only that exact key), beside the caller's headers. `api-key` is renamed to `x-api-key` unless `x-api-key` is spelled exactly so, and names that differ only in case are joined into one header with `, `
|
||||
- `anthropic-beta` goes through the `azure_ai` row of `anthropic_beta_headers_config.json` as Python's handler filters it: unknown and unsupported betas are dropped, the caller's and the ones the request needs alike
|
||||
- The beta-gated system field strip, system-role normalization and the `cache_control.scope` strip run in `finalize_request`, after the betas are picked from the request as sent, in the order Python's transform runs them
|
||||
- This config alone reads messages and system prompts Rust could not type as Python does (`UnparsedMessages::Read`): the sanitizer, advisor and encrypted-reasoning strips rewrite them and their beta triggers count. Every other provider, `anthropic` included by the owner's ruling, keeps main's behaviour: such messages go as sent, and a system prompt that does not parse is main's request error (`messages_body`)
|
||||
- Every native Messages provider (anthropic, azure_ai, bedrock invoke) reads messages and system prompts Rust could not type as Python does: the sanitizer, advisor and encrypted-reasoning strips rewrite them, their beta triggers count, and a system prompt that does not parse is kept. There is no per-provider switch for this
|
||||
|
||||
# references
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use litellm_secret_value::SecretValue;
|
|||
use serde_json::Value;
|
||||
|
||||
use super::{
|
||||
context::{MessagesShapeOptions, MessagesTransformContext},
|
||||
context::{MessagesModelCapabilities, MessagesShapeOptions, MessagesTransformContext},
|
||||
tools::ToolCustomField,
|
||||
};
|
||||
|
||||
|
|
@ -49,6 +49,14 @@ pub trait BaseMessagesConfig: Sync {
|
|||
Ok(request)
|
||||
}
|
||||
|
||||
fn clamp_requested_effort(
|
||||
&self,
|
||||
request: MessagesRequest,
|
||||
_capabilities: &MessagesModelCapabilities,
|
||||
) -> Result<MessagesRequest, Error> {
|
||||
Ok(request)
|
||||
}
|
||||
|
||||
fn native_output_fallback(
|
||||
&self,
|
||||
_request: &MessagesRequest,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ use litellm_host::interceptors::RetriedRequest;
|
|||
use crate::{
|
||||
anthropic::messages::handler::shape_anthropic_messages_request,
|
||||
base_llm::messages::context::{
|
||||
MessagesShapeOptions, MessagesTransformContext, NativeOutputSupport,
|
||||
MessagesModelCapabilities, MessagesShapeOptions, MessagesTransformContext,
|
||||
NativeOutputSupport,
|
||||
},
|
||||
};
|
||||
use litellm_auth::CredentialPlacement;
|
||||
|
|
@ -232,6 +233,14 @@ impl BaseMessagesConfig for AmazonAnthropicClaudeMessagesConfig {
|
|||
shape_anthropic_messages_request(request, options)
|
||||
}
|
||||
|
||||
fn clamp_requested_effort(
|
||||
&self,
|
||||
request: MessagesRequest,
|
||||
capabilities: &MessagesModelCapabilities,
|
||||
) -> Result<MessagesRequest, Error> {
|
||||
request::clamp_adaptive_reasoning_effort_for_bedrock(request, capabilities)
|
||||
}
|
||||
|
||||
fn get_complete_url(
|
||||
&self,
|
||||
api_base: Option<&str>,
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ fn get_bedrock_output_config_effort_ceiling(
|
|||
}
|
||||
|
||||
/// Port of Python's `_clamp_adaptive_reasoning_effort_for_bedrock`.
|
||||
fn clamp_adaptive_reasoning_effort_for_bedrock(
|
||||
pub(super) fn clamp_adaptive_reasoning_effort_for_bedrock(
|
||||
request: MessagesRequest,
|
||||
capabilities: &MessagesModelCapabilities,
|
||||
) -> Result<MessagesRequest, Error> {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue