From b98af38a492a136bedb6950f43d7fb86118705c8 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Wed, 23 Sep 2026 19:53:10 -0700 Subject: [PATCH] refactor(cost): convert provider rates through python-compat's float() and int() Dashscope, Perplexity, xAI, search, tiered, video, A2A, and video usage rates now use wire::py_float or py_int, and the Fireworks cache default and off-peak rates share py_float_unless_bool, matching Python's float() on padded strings, booleans, and digit separators Co-Authored-By: Claude Opus 5.5 --- litellm-rust/crates/cost/src/a2a_cost.rs | 7 ++---- .../crates/cost/src/completion_response.rs | 7 ++---- .../crates/cost/src/dashscope_cost.rs | 8 ++----- litellm-rust/crates/cost/src/off_peak.rs | 7 ++---- litellm-rust/crates/cost/src/openai_cost.rs | 7 ++---- .../crates/cost/src/perplexity_cost.rs | 9 ++------ .../crates/cost/src/provider_cache.rs | 7 ++---- litellm-rust/crates/cost/src/search_cost.rs | 7 ++---- .../crates/cost/src/tiered_pricing.rs | 8 ++----- litellm-rust/crates/cost/src/wire.rs | 4 ++++ litellm-rust/crates/cost/src/xai_cost.rs | 22 +++++++------------ .../crates/cost/tests/python_openai_cost.rs | 14 ++++++++++++ .../cost/tests/python_tiered_pricing.rs | 2 ++ .../crates/cost/tests/python_xai_cost.rs | 14 ++++++++++++ 14 files changed, 60 insertions(+), 63 deletions(-) diff --git a/litellm-rust/crates/cost/src/a2a_cost.rs b/litellm-rust/crates/cost/src/a2a_cost.rs index 4f33045fe89..5b78804084f 100644 --- a/litellm-rust/crates/cost/src/a2a_cost.rs +++ b/litellm-rust/crates/cost/src/a2a_cost.rs @@ -1,12 +1,9 @@ use crate::error::CostError; +use crate::wire::py_float; use serde_json::Value; fn amount(value: &Value) -> Result { - match value { - Value::Number(number) => number.as_f64().ok_or(CostError::InvalidCost), - Value::String(number) => number.parse().map_err(|_| CostError::InvalidCost), - _ => Err(CostError::InvalidCost), - } + py_float(value).ok_or(CostError::InvalidCost) } pub fn calculate_token_based_cost( diff --git a/litellm-rust/crates/cost/src/completion_response.rs b/litellm-rust/crates/cost/src/completion_response.rs index 3f55e6c457c..1d6bc6d3ab2 100644 --- a/litellm-rust/crates/cost/src/completion_response.rs +++ b/litellm-rust/crates/cost/src/completion_response.rs @@ -34,6 +34,7 @@ use crate::speech_cost::count_characters; use crate::together_cost::together_pricing_model; use crate::tool_call_cost_tracking::{DefaultToolRates, ResponseKind as ToolResponseKind}; use crate::tool_cost_dispatch::{BuiltInToolCostRequest, get_cost_for_built_in_tools}; +use crate::wire::py_float; #[derive(Clone, Copy, Debug)] pub struct BuiltInToolCostConfig<'a> { @@ -159,11 +160,7 @@ fn flat_priced( } fn number(value: Option<&Value>) -> Option { - match value? { - Value::Number(value) => value.as_f64(), - Value::String(value) => value.parse().ok(), - _ => None, - } + value.and_then(py_float) } fn count_text_usage(input: CompletionTextInput<'_>) -> Result { diff --git a/litellm-rust/crates/cost/src/dashscope_cost.rs b/litellm-rust/crates/cost/src/dashscope_cost.rs index ce3e69f3d10..d8eb4efbfb9 100644 --- a/litellm-rust/crates/cost/src/dashscope_cost.rs +++ b/litellm-rust/crates/cost/src/dashscope_cost.rs @@ -5,6 +5,7 @@ use crate::generic_usage::{parse_completion_tokens_details, parse_prompt_tokens_ use crate::off_peak::{open_off_peak_block, parse_off_peak_rate}; use crate::responses_usage::ChatUsage; use crate::tiered_pricing::{select_tier_for_input, tier_rate}; +use crate::wire::py_float; #[derive(Clone, Copy, Debug, PartialEq)] pub struct TokenBreakdown { @@ -48,12 +49,7 @@ pub fn extract_token_breakdown(usage: &ChatUsage) -> TokenBreakdown { } fn rate(value: Option<&Value>) -> f64 { - match value { - Some(Value::Number(value)) => value.as_f64().unwrap_or(0.0), - Some(Value::String(value)) => value.parse().unwrap_or(0.0), - Some(Value::Bool(value)) => f64::from(*value as u8), - _ => 0.0, - } + value.and_then(py_float).unwrap_or(0.0) } fn flat_rate(model_info: &Value, key: &str, fallback: &str) -> f64 { diff --git a/litellm-rust/crates/cost/src/off_peak.rs b/litellm-rust/crates/cost/src/off_peak.rs index 1a30a6054da..4ba950c4319 100644 --- a/litellm-rust/crates/cost/src/off_peak.rs +++ b/litellm-rust/crates/cost/src/off_peak.rs @@ -1,3 +1,4 @@ +use crate::wire::py_float_unless_bool; use jiff::{Timestamp, tz::TimeZone}; use serde_json::Value; @@ -115,11 +116,7 @@ pub fn open_off_peak_block(model_info: &Value, at: Timestamp) -> Option<&Value> } pub fn parse_off_peak_rate(value: Option<&Value>) -> Option { - match value? { - Value::Number(number) => number.as_f64(), - Value::String(number) => number.trim().parse().ok(), - _ => None, - } + value.and_then(py_float_unless_bool) } pub fn apply_off_peak_pricing(model_info: &Value, at: Timestamp, rates: TokenRates) -> TokenRates { diff --git a/litellm-rust/crates/cost/src/openai_cost.rs b/litellm-rust/crates/cost/src/openai_cost.rs index 468774c134c..829b595120a 100644 --- a/litellm-rust/crates/cost/src/openai_cost.rs +++ b/litellm-rust/crates/cost/src/openai_cost.rs @@ -2,6 +2,7 @@ use crate::error::CostError; use serde_json::Value; use crate::non_token::{Charge, Unit, calculate}; +use crate::wire::py_float; pub use crate::non_token::video_resolution_to_cost_field_suffix; @@ -17,11 +18,7 @@ pub fn video_output_cost_per_second( model_info: &Value, video_resolution: Option<&str>, ) -> Option { - let rate = |value: &Value| { - value - .as_f64() - .or_else(|| value.as_str().and_then(|value| value.parse::().ok())) - }; + let rate = |value: &Value| py_float(value); let tier_rate = video_resolution .and_then(video_resolution_to_cost_field_suffix) .and_then(|suffix| model_info.get(format!("output_cost_per_second_{suffix}"))) diff --git a/litellm-rust/crates/cost/src/perplexity_cost.rs b/litellm-rust/crates/cost/src/perplexity_cost.rs index 24a74b9803a..d2906eef968 100644 --- a/litellm-rust/crates/cost/src/perplexity_cost.rs +++ b/litellm-rust/crates/cost/src/perplexity_cost.rs @@ -3,15 +3,10 @@ use serde_json::Value; use crate::off_peak::{open_off_peak_block, parse_off_peak_rate}; use crate::responses_usage::ChatUsage; -use crate::wire::is_truthy; +use crate::wire::{is_truthy, py_float}; fn rate(value: Option<&Value>) -> f64 { - match value { - Some(Value::Number(value)) => value.as_f64().unwrap_or(0.0), - Some(Value::String(value)) => value.parse().unwrap_or(0.0), - Some(Value::Bool(value)) => f64::from(*value as u8), - _ => 0.0, - } + value.and_then(py_float).unwrap_or(0.0) } fn cost_per_query(model_info: &Value) -> f64 { diff --git a/litellm-rust/crates/cost/src/provider_cache.rs b/litellm-rust/crates/cost/src/provider_cache.rs index c55762d2a25..fd4ebe66e3c 100644 --- a/litellm-rust/crates/cost/src/provider_cache.rs +++ b/litellm-rust/crates/cost/src/provider_cache.rs @@ -1,13 +1,10 @@ use serde_json::{Number, Value}; use crate::provider::LlmProviders; +use crate::wire::py_float_unless_bool; fn rate(value: Option<&Value>) -> Option { - match value? { - Value::Number(value) => value.as_f64(), - Value::String(value) => value.trim().parse().ok(), - _ => None, - } + value.and_then(py_float_unless_bool) } fn number(value: f64) -> Option { diff --git a/litellm-rust/crates/cost/src/search_cost.rs b/litellm-rust/crates/cost/src/search_cost.rs index f8582f41085..a8e642a5513 100644 --- a/litellm-rust/crates/cost/src/search_cost.rs +++ b/litellm-rust/crates/cost/src/search_cost.rs @@ -2,6 +2,7 @@ use serde_json::Value; use crate::catalog::ModelInfoCatalog; use crate::error::CostError; +use crate::wire::py_float; #[derive(Clone, Copy, Debug, PartialEq)] pub struct ParallelAiPricing { @@ -64,11 +65,7 @@ pub fn parallel_ai_search_cost( } fn rate(value: Option<&Value>) -> f64 { - match value { - Some(Value::Number(value)) => value.as_f64().unwrap_or(0.0), - Some(Value::String(value)) => value.parse().unwrap_or(0.0), - _ => 0.0, - } + value.and_then(py_float).unwrap_or(0.0) } pub fn search_provider_cost_from_model_info( diff --git a/litellm-rust/crates/cost/src/tiered_pricing.rs b/litellm-rust/crates/cost/src/tiered_pricing.rs index f7d7e6b9c18..e1b591c25dd 100644 --- a/litellm-rust/crates/cost/src/tiered_pricing.rs +++ b/litellm-rust/crates/cost/src/tiered_pricing.rs @@ -1,3 +1,4 @@ +use crate::wire::py_float; use serde_json::Value; fn range(tier: &Value) -> Option<(f64, f64)> { @@ -31,12 +32,7 @@ pub fn select_tier_for_input(tiers: &[Value], input_tokens: i64) -> Option<&Valu } fn coerce(value: &Value) -> f64 { - match value { - Value::Number(value) => value.as_f64().unwrap_or(0.0), - Value::String(value) => value.trim().parse().unwrap_or(0.0), - Value::Bool(value) => f64::from(*value), - _ => 0.0, - } + py_float(value).unwrap_or(0.0) } pub fn tier_rate(tier: &Value, cost_key: &str, fallback_cost_key: Option<&str>) -> f64 { diff --git a/litellm-rust/crates/cost/src/wire.rs b/litellm-rust/crates/cost/src/wire.rs index 37a4df8b5ca..adfd031bbca 100644 --- a/litellm-rust/crates/cost/src/wire.rs +++ b/litellm-rust/crates/cost/src/wire.rs @@ -96,6 +96,10 @@ pub fn py_float(value: &Value) -> Option { number::float(&python_value(value)).ok() } +pub fn py_float_unless_bool(value: &Value) -> Option { + (!value.is_boolean()).then(|| py_float(value)).flatten() +} + pub fn lax_int(value: &Value) -> Option { i64::try_from(&pydantic::lax_int(&python_value(value)).ok()?).ok() } diff --git a/litellm-rust/crates/cost/src/xai_cost.rs b/litellm-rust/crates/cost/src/xai_cost.rs index b87d898e08a..261242b23ad 100644 --- a/litellm-rust/crates/cost/src/xai_cost.rs +++ b/litellm-rust/crates/cost/src/xai_cost.rs @@ -5,6 +5,7 @@ use serde_json::Value; use crate::generic_cost::calculate_generic_cost_from_model_info_with_region; use crate::responses_usage::{ChatUsage, PromptTokenDetails}; +use crate::wire::{py_float, py_int}; pub fn reported_cost(usage: &ChatUsage) -> Option { usage.cost.filter(|cost| cost.is_finite() && *cost >= 0.0) @@ -18,12 +19,10 @@ pub fn web_search_cost_per_call_from_model_info(model_info: &Value, default_rate ] .into_iter() .filter_map(|key| { - let value = model_info.get("search_context_cost_per_query")?.get(key)?; - match value { - Value::Number(value) => value.as_f64(), - Value::String(value) => value.parse().ok(), - _ => None, - } + model_info + .get("search_context_cost_per_query")? + .get(key) + .and_then(py_float) }) .find(|rate| *rate > 0.0) .unwrap_or(default_rate) @@ -47,14 +46,9 @@ pub fn cost_per_web_search_request( } fn count(value: Option<&Value>) -> Option { - match value? { - Value::Number(value) => value - .as_u64() - .or_else(|| value.as_f64().map(|value| value as u64)), - Value::String(value) => value.parse().ok(), - Value::Bool(value) => Some(u64::from(*value)), - _ => None, - } + value + .and_then(py_int) + .and_then(|count| u64::try_from(count).ok()) } pub fn apply_server_side_tool_usage_details_to_usage( diff --git a/litellm-rust/crates/cost/tests/python_openai_cost.rs b/litellm-rust/crates/cost/tests/python_openai_cost.rs index 9bef5d8020d..8b15b2c7852 100644 --- a/litellm-rust/crates/cost/tests/python_openai_cost.rs +++ b/litellm-rust/crates/cost/tests/python_openai_cost.rs @@ -77,3 +77,17 @@ fn video_generation_cost_rejects_invalid_duration() { Err(CostError::InvalidQuantity) ); } + +#[rstest] +#[case::padded_string(json!({"output_cost_per_second": " 0.05 "}), Some(0.05))] +#[case::boolean(json!({"output_cost_per_second": true}), Some(1.0))] +#[case::resolution_tier_first(json!({"output_cost_per_second": 0.05, "output_cost_per_second_720p": "0.1"}), Some(0.1))] +fn video_output_cost_per_second_converts_rates_like_python_float( + #[case] model_info: serde_json::Value, + #[case] expected: Option, +) { + assert_eq!( + video_output_cost_per_second(&model_info, Some("720p")), + expected + ); +} diff --git a/litellm-rust/crates/cost/tests/python_tiered_pricing.rs b/litellm-rust/crates/cost/tests/python_tiered_pricing.rs index e7df7388667..fffca77962e 100644 --- a/litellm-rust/crates/cost/tests/python_tiered_pricing.rs +++ b/litellm-rust/crates/cost/tests/python_tiered_pricing.rs @@ -46,6 +46,8 @@ fn select_tier_for_input_skips_invalid_ranges_and_chooses_first_matching_tier() #[case(json!({"output_cost_per_token": " 4e-07 "}), "output_cost_per_token", Some("input_cost_per_token"), 4e-7)] #[case(json!({"output_cost_per_token": "bad", "input_cost_per_token": "4e-07"}), "output_cost_per_token", Some("input_cost_per_token"), 0.0)] #[case(json!({"output_cost_per_token": ["bad"], "input_cost_per_token": "4e-07"}), "output_cost_per_token", Some("input_cost_per_token"), 0.0)] +#[case(json!({"input_cost_per_token": true}), "input_cost_per_token", None, 1.0)] +#[case(json!({"input_cost_per_token": "4_0e-9"}), "input_cost_per_token", None, 4e-8)] fn tier_rate_preserves_zero_and_coerces_yaml_strings( #[case] tier: Value, #[case] key: &str, diff --git a/litellm-rust/crates/cost/tests/python_xai_cost.rs b/litellm-rust/crates/cost/tests/python_xai_cost.rs index f05907ea81d..f2744199776 100644 --- a/litellm-rust/crates/cost/tests/python_xai_cost.rs +++ b/litellm-rust/crates/cost/tests/python_xai_cost.rs @@ -203,3 +203,17 @@ fn apply_server_side_tool_usage_details_preserves_existing_cache_usage() { assert_eq!(prompt.cached_tokens, 7); assert_eq!(prompt.web_search_requests, Some(4)); } + +#[rstest] +#[case::padded_string(json!({"search_context_cost_per_query": {"search_context_size_medium": " 0.02 "}}), 0.02)] +#[case::skips_unparseable_and_zero(json!({"search_context_cost_per_query": {"search_context_size_medium": "n/a", "search_context_size_low": 0, "search_context_size_high": 0.03}}), 0.03)] +#[case::default_when_nothing_prices(json!({}), 0.005)] +fn web_search_cost_per_call_takes_the_first_positive_float( + #[case] model_info: Value, + #[case] expected: f64, +) { + assert_eq!( + litellm_cost::xai_cost::web_search_cost_per_call_from_model_info(&model_info, 0.005), + expected + ); +}