Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(deepgram): ignore zero-duration Metadata frames when billing streamed audio

yassin9f6e5242853a
diff --git a/litellm/llms/deepgram/common_utils.py b/litellm/llms/deepgram/common_utils.py
index 4f18918fa71..ede5b4f157d 100644
--- a/litellm/llms/deepgram/common_utils.py
+++ b/litellm/llms/deepgram/common_utils.py
@@ -152,7 +152,7 @@ def deepgram_listen_audio_seconds(websocket_messages: Sequence[Mapping[str, obje
duration
for frame in websocket_messages
if frame.get("type") == "Metadata"
- if (duration := _seconds(frame.get("duration"))) is not None
+ if (duration := _seconds(frame.get("duration"))) is not None and duration > 0
)
if metadata_durations:
return metadata_durations[-1]
diff --git a/tests/test_litellm/llms/deepgram/test_deepgram_common_utils.py b/tests/test_litellm/llms/deepgram/test_deepgram_common_utils.py
index 802a3795ffe..8b61d5bffa0 100644
--- a/tests/test_litellm/llms/deepgram/test_deepgram_common_utils.py
+++ b/tests/test_litellm/llms/deepgram/test_deepgram_common_utils.py
@@ -105,6 +105,12 @@ def test_deepgram_listen_callback_params(query_string: str, expected: tuple[str,
pytest.param((_metadata(4.0), _results(0.0, 9.0), _metadata(5.5)), 5.5, id="last metadata wins"),
pytest.param((_results(0.0, 2.0), _results(2.0, 3.5), _results(1.0, 1.0)), 5.5, id="furthest results end"),
pytest.param((_results(0.0, 0.0), _metadata(0.0)), 0.0, id="zero metadata is a real zero"),
+ pytest.param(
+ (_metadata(0.0), _results(0.0, 2.0), _results(2.0, 3.5)),
+ 5.5,
+ id="handshake metadata zero does not hide streamed results",
+ ),
+ pytest.param((_metadata(0.0), _results(0.0, 2.0), _metadata(0.0)), 2.0, id="only zero metadata frames"),
pytest.param((_results(0.0, 1.5), _metadata("6.25")), 1.5, id="string metadata is ignored"),
pytest.param((_results(0.0, 1.5), _metadata(True)), 1.5, id="boolean metadata is ignored"),
pytest.param((_results(0.0, 1.5), _metadata(-3.0)), 1.5, id="negative metadata is ignored"),