From c8716673db9f5ad51b45da9d9516273f888b30ea Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Wed, 23 Sep 2026 20:07:08 -0700 Subject: [PATCH] fix(cost): compare search result tiers as Python numbers Float max_results and float tier bounds select a tier as they do in Python, and wire::py_real is the one home for Python's isinstance(value, (int, float)) Co-Authored-By: Claude Opus 5.5 --- .../crates/cost/src/completion_cost.rs | 12 +++-------- litellm-rust/crates/cost/src/search_cost.rs | 18 +++++++++------- litellm-rust/crates/cost/src/wire.rs | 6 ++++++ .../crates/cost/tests/python_search_cost.rs | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/litellm-rust/crates/cost/src/completion_cost.rs b/litellm-rust/crates/cost/src/completion_cost.rs index 7114da02a69..0c8a2780a44 100644 --- a/litellm-rust/crates/cost/src/completion_cost.rs +++ b/litellm-rust/crates/cost/src/completion_cost.rs @@ -1,13 +1,7 @@ use crate::error::CostError; use serde_json::Value; -use crate::wire::py_float; - -fn python_real(value: &Value) -> Option { - (value.is_number() || value.is_boolean()) - .then(|| py_float(value)) - .flatten() -} +use crate::wire::{py_float, py_real}; #[derive(Clone, Copy, Debug, PartialEq)] pub struct CompletionCost { @@ -33,7 +27,7 @@ pub fn apply_cost_discount( let percent = provider .filter(|provider| !provider.is_empty()) .and_then(|provider| config.get(provider)) - .and_then(python_real) + .and_then(py_real) .unwrap_or(0.0); let amount = base_cost * percent; (base_cost - amount, percent, amount) @@ -53,7 +47,7 @@ pub fn apply_cost_margin( config.get("percentage").and_then(py_float).unwrap_or(0.0), config.get("fixed_amount").and_then(py_float).unwrap_or(0.0), ), - Some(value) => (python_real(value).unwrap_or(0.0), 0.0), + Some(value) => (py_real(value).unwrap_or(0.0), 0.0), None => (0.0, 0.0), }; let total = base_cost * percent + fixed; diff --git a/litellm-rust/crates/cost/src/search_cost.rs b/litellm-rust/crates/cost/src/search_cost.rs index a8e642a5513..b872f204e7f 100644 --- a/litellm-rust/crates/cost/src/search_cost.rs +++ b/litellm-rust/crates/cost/src/search_cost.rs @@ -2,7 +2,7 @@ use serde_json::Value; use crate::catalog::ModelInfoCatalog; use crate::error::CostError; -use crate::wire::py_float; +use crate::wire::{py_float, py_real}; #[derive(Clone, Copy, Debug, PartialEq)] pub struct ParallelAiPricing { @@ -82,16 +82,18 @@ pub fn search_provider_cost_from_model_info( |tiers| { let max_results = optional_params .get("max_results") - .and_then(Value::as_i64) - .unwrap_or(10); + .map_or(Some(10.0), py_real); let tier = tiers .iter() .find(|tier| { - tier.get("max_results_range") - .and_then(Value::as_array) - .is_some_and(|bounds| { - matches!(bounds.as_slice(), [min, max] if min.as_i64().is_some_and(|min| min <= max_results) && max.as_i64().is_some_and(|max| max_results <= max)) - }) + let bounds = tier.get("max_results_range").and_then(Value::as_array); + match (bounds.map(Vec::as_slice), max_results) { + (Some([min, max]), Some(max_results)) => { + py_real(min).is_some_and(|min| min <= max_results) + && py_real(max).is_some_and(|max| max_results <= max) + } + _ => false, + } }) .or_else(|| tiers.last()); rate(tier.and_then(|tier| tier.get("input_cost_per_query"))) diff --git a/litellm-rust/crates/cost/src/wire.rs b/litellm-rust/crates/cost/src/wire.rs index 01ee620d1f2..8f88a8936ec 100644 --- a/litellm-rust/crates/cost/src/wire.rs +++ b/litellm-rust/crates/cost/src/wire.rs @@ -96,6 +96,12 @@ pub fn py_float(value: &Value) -> Option { number::float(&python_value(value)).ok() } +pub fn py_real(value: &Value) -> Option { + (value.is_number() || value.is_boolean()) + .then(|| py_float(value)) + .flatten() +} + pub fn py_float_unless_bool(value: &Value) -> Option { (!value.is_boolean()).then(|| py_float(value)).flatten() } diff --git a/litellm-rust/crates/cost/tests/python_search_cost.rs b/litellm-rust/crates/cost/tests/python_search_cost.rs index f015ad7f12f..8e0746b4358 100644 --- a/litellm-rust/crates/cost/tests/python_search_cost.rs +++ b/litellm-rust/crates/cost/tests/python_search_cost.rs @@ -142,3 +142,24 @@ fn parallel_ai_invalid_usage_falls_back_to_requested_results(#[case] params: Val 0.02 ); } + +#[rstest] +#[case::float_max_results(json!({"max_results": 50.0}), 0.025)] +#[case::float_tier_bounds_match_integer_request(json!({"max_results": 30}), 0.025)] +#[case::default_ten_results(json!({}), 0.005)] +#[case::boolean_is_one_result(json!({"max_results": true}), 0.005)] +fn search_provider_cost_compares_result_tiers_as_python_numbers( + #[case] params: Value, + #[case] expected_rate: f64, +) { + let model_info = json!({ + "tiered_pricing": [ + {"max_results_range": [0, 25], "input_cost_per_query": 0.005}, + {"max_results_range": [26.0, 100.0], "input_cost_per_query": 0.025} + ] + }); + assert_eq!( + search_provider_cost_from_model_info(&model_info, 1, ¶ms), + (expected_rate, 0.0) + ); +}