fix(backend): guard against non-finite cost_usd in persist_and_record_usage

float('inf') and float('nan') do not raise ValueError/TypeError so they
bypass the existing try/except. Passing them to usd_to_microdollars causes
OverflowError at round(inf * 1_000_000). Add math.isfinite(val) and val >= 0
check (matching the same pattern used in baseline/service.py and llm.py)
before assigning cost_float.
This commit is contained in:
Zamil Majdy
2026-04-07 22:56:20 +07:00
parent e7ca81ed89
commit 91af007c18

View File

@@ -11,6 +11,7 @@ This module extracts that common logic so both paths stay in sync.
import asyncio
import logging
import math
import re
import threading
@@ -174,7 +175,9 @@ async def persist_and_record_usage(
cost_float = None
if cost_usd is not None:
try:
cost_float = float(cost_usd)
val = float(cost_usd)
if math.isfinite(val) and val >= 0:
cost_float = val
except (ValueError, TypeError):
pass