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 <noreply@anthropic.com>
This commit is contained in:
Yujong Lee 2026-09-23 19:53:10 -07:00
parent 04bffb26d7
commit b98af38a49
14 changed files with 60 additions and 63 deletions

View file

@ -1,12 +1,9 @@
use crate::error::CostError;
use crate::wire::py_float;
use serde_json::Value;
fn amount(value: &Value) -> Result<f64, CostError> {
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(

View file

@ -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<f64> {
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<ChatUsage, CostError> {

View file

@ -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 {

View file

@ -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<f64> {
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 {

View file

@ -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<f64> {
let rate = |value: &Value| {
value
.as_f64()
.or_else(|| value.as_str().and_then(|value| value.parse::<f64>().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}")))

View file

@ -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 {

View file

@ -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<f64> {
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<Value> {

View file

@ -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(

View file

@ -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 {

View file

@ -96,6 +96,10 @@ pub fn py_float(value: &Value) -> Option<f64> {
number::float(&python_value(value)).ok()
}
pub fn py_float_unless_bool(value: &Value) -> Option<f64> {
(!value.is_boolean()).then(|| py_float(value)).flatten()
}
pub fn lax_int(value: &Value) -> Option<i64> {
i64::try_from(&pydantic::lax_int(&python_value(value)).ok()?).ok()
}

View file

@ -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<f64> {
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<u64> {
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(

View file

@ -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<f64>,
) {
assert_eq!(
video_output_cost_per_second(&model_info, Some("720p")),
expected
);
}

View file

@ -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,

View file

@ -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
);
}