fix: use is-not-None guards for per-second cost extraction

Greptile review (confidence 3/5) correctly identified that the
input_cost_per_second / output_cost_per_second fallback used the Python
 operator, which treats 0.0 as falsy — the same bug explicitly
fixed for token-based pricing in this PR.

Replace  with explicit  checks so a configured
input_cost_per_second of 0.0 (free input) is honoured rather than
silently falling back to output_cost_per_second.
This commit is contained in:
Dor Amir 2026-03-06 12:52:54 -05:00
parent a5a69b08c1
commit af8b79eb5b

View file

@ -1139,9 +1139,14 @@ def completion_cost( # noqa: PLR0915
_metadata = _litellm_params.get("metadata", {}) or {}
_model_info = _metadata.get("model_info", {}) or {}
# Prefer input_cost_per_second; fall back to output_cost_per_second
_cost_per_second = _model_info.get(
"input_cost_per_second"
) or _model_info.get("output_cost_per_second")
# Use `is not None` guards to correctly handle explicit 0.0 costs
_input_cost_per_second = _model_info.get("input_cost_per_second")
_output_cost_per_second = _model_info.get("output_cost_per_second")
_cost_per_second = (
_input_cost_per_second
if _input_cost_per_second is not None
else _output_cost_per_second
)
if _cost_per_second is not None:
custom_cost_per_second = _cost_per_second