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 <noreply@anthropic.com>
This commit is contained in:
Yujong Lee 2026-09-23 20:07:08 -07:00
parent 091dc10d3c
commit c8716673db
4 changed files with 40 additions and 17 deletions

View file

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

View file

@ -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")))

View file

@ -96,6 +96,12 @@ pub fn py_float(value: &Value) -> Option<f64> {
number::float(&python_value(value)).ok()
}
pub fn py_real(value: &Value) -> Option<f64> {
(value.is_number() || value.is_boolean())
.then(|| py_float(value))
.flatten()
}
pub fn py_float_unless_bool(value: &Value) -> Option<f64> {
(!value.is_boolean()).then(|| py_float(value)).flatten()
}

View file

@ -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, &params),
(expected_rate, 0.0)
);
}