fix(dashscope): treat an explicit 0.0 tier cost as a real price, not missing - #30749
Conversation
…missing _calculate_tiered_cost resolved a tier's per-token rate with `tier.get(cost_key) or tier.get(fallback_cost_key, 0)`. The `or` short-circuits on a falsy 0.0, so a tier that legitimately prices cached reads (or reasoning tokens) at 0.0 was silently billed at the full fallback rate, at both the in-range and overflow sites. Add _resolve_tier_cost_per_token, which only falls back when the primary key is absent (is None), mirroring the flat-pricing path that already guards correctly. Uses the X | None annotation style to stay within the ruff strict-rule budget. Re-submit of BerriAI#30653, which was reverted from litellm_internal_staging because the previous Optional[str] annotation pushed the UP045 count over the ruff-strict-budget.json ceiling.
Greptile SummaryThis PR fixes a billing correctness bug in the Dashscope tiered cost calculator where a tier's explicit
Confidence Score: 5/5The change is a narrowly scoped fix to a single billing helper with clear before/after semantics and no effect on other providers or the hot request path. The logic switch from No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/dashscope/cost_calculator.py | Adds _resolve_tier_cost_per_token helper that correctly treats an explicit 0.0 as a real price by using an is None guard instead of the falsy or short-circuit; both the in-range and overflow billing sites are updated to use it. |
| tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py | Adds two pure unit tests (no network calls) directly exercising _calculate_tiered_cost for the zero-cost in-range and overflow paths; removes an unused json import. |
Reviews (1): Last reviewed commit: "fix(dashscope): treat an explicit 0.0 ti..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
aed763b
into
BerriAI:litellm_oss_staging_230626
…missing (#30749) _calculate_tiered_cost resolved a tier's per-token rate with `tier.get(cost_key) or tier.get(fallback_cost_key, 0)`. The `or` short-circuits on a falsy 0.0, so a tier that legitimately prices cached reads (or reasoning tokens) at 0.0 was silently billed at the full fallback rate, at both the in-range and overflow sites. Add _resolve_tier_cost_per_token, which only falls back when the primary key is absent (is None), mirroring the flat-pricing path that already guards correctly. Uses the X | None annotation style to stay within the ruff strict-rule budget. Re-submit of #30653, which was reverted from litellm_internal_staging because the previous Optional[str] annotation pushed the UP045 count over the ruff-strict-budget.json ceiling.
…missing (#30749) _calculate_tiered_cost resolved a tier's per-token rate with `tier.get(cost_key) or tier.get(fallback_cost_key, 0)`. The `or` short-circuits on a falsy 0.0, so a tier that legitimately prices cached reads (or reasoning tokens) at 0.0 was silently billed at the full fallback rate, at both the in-range and overflow sites. Add _resolve_tier_cost_per_token, which only falls back when the primary key is absent (is None), mirroring the flat-pricing path that already guards correctly. Uses the X | None annotation style to stay within the ruff strict-rule budget. Re-submit of #30653, which was reverted from litellm_internal_staging because the previous Optional[str] annotation pushed the UP045 count over the ruff-strict-budget.json ceiling.
Re-submit of #30653, which was merged then reverted from
litellm_internal_stagingbecause the previousOptional[str]annotation pushed the UP045 count over the ceiling inruff-strict-budget.json. This version uses theX | Nonestyle so it adds no new UP045 violations (count on the file stays at the pre-existing 3).Bug
_calculate_tiered_costresolved a tier's per-token rate with:The
orshort-circuits on a falsy0.0, so a tier that legitimately prices cached reads (or reasoning tokens) at0.0(e.g. a free-cache-read tier) is silently billed at the full fallback rate. This happens at both the in-range and the overflow (beyond-highest-tier) sites.Fix
Add
_resolve_tier_cost_per_token, which only falls back when the primary key is absent (is None), treating an explicit0.0as a real price. This mirrors the flat-pricing path in_calculate_prompt_cost/_calculate_completion_cost, which already guards withis None.Tests
Two new tests cover the in-range and overflow paths with a
0.0cache-read tier, asserting the cost is0.0and not the input rate. Full dashscope cost-calculator suite passes locally and ruff is clean.