fix(transcription): reject nonfinite provider durations

This commit is contained in:
Emerson Gomes 2026-09-23 04:49:54 -05:00
parent 9e1d36489e
commit 6a3c36fcc3
No known key found for this signature in database
GPG key ID: D3DF28AB5D1B5E17
2 changed files with 10 additions and 3 deletions

View file

@ -1038,9 +1038,7 @@ def _get_transcription_usage_duration(completion_response: object) -> float | No
seconds: Final = (
usage_object.get("seconds") if isinstance(usage_object, dict) else getattr(usage_object, "seconds", None)
)
if isinstance(seconds, bool) or not isinstance(seconds, (int, float)) or seconds < 0:
return None
return float(seconds)
return normalized_audio_duration_seconds(seconds)
def _is_known_usage_objects(usage_obj):

View file

@ -334,6 +334,15 @@ def test_transcription_usage_cost_returns_zero_for_unknown_type():
assert _transcription_usage_cost({}, {}) == 0.0
@pytest.mark.parametrize("malformed_seconds", [-1.0, float("nan"), float("inf"), 10**1000, True])
def test_transcription_ignores_invalid_provider_duration(malformed_seconds: float | int | bool) -> None:
from litellm.cost_calculator import _get_transcription_usage_duration
response: Final = SimpleNamespace(usage={"type": "duration", "seconds": malformed_seconds})
assert _get_transcription_usage_duration(response) is None
def test_get_transcription_model_falls_back_to_session_model(monkeypatch):
"""session.model is used when transcription-specific model fields are absent."""
monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")