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(batches): skip undecodable batch output lines when costing

mateo-berri5eeccf69b60b
diff --git a/litellm/batches/batch_utils.py b/litellm/batches/batch_utils.py
index 0f8acce3379..0cf22d82ca6 100644
--- a/litellm/batches/batch_utils.py
+++ b/litellm/batches/batch_utils.py
@@ -426,7 +426,7 @@ def _iter_batch_output_entries(file_content: bytes) -> Iterator[dict]:
def _parse_batch_output_line(line: bytes) -> dict | None:
try:
parsed: Final = json.loads(line)
- except json.JSONDecodeError as e:
+ except ValueError as e:
verbose_logger.warning("skipping malformed batch output line: %s", str(e))
return None
if isinstance(parsed, dict):
diff --git a/tests/test_litellm/batches/test_batch_utils.py b/tests/test_litellm/batches/test_batch_utils.py
index f3a7413bf03..ebe093c591c 100644
--- a/tests/test_litellm/batches/test_batch_utils.py
+++ b/tests/test_litellm/batches/test_batch_utils.py
@@ -186,6 +186,11 @@ def test_iter_output_entries_skips_malformed_and_non_object_lines():
assert list(bu._iter_batch_output_entries(content)) == [{"ok": 1}, {"ok": 2}]
+def test_iter_output_entries_skips_undecodable_line():
+ content = b'{"ok": 1}\n{"note": "\xff-bad"}\n{"ok": 2}\n'
+ assert list(bu._iter_batch_output_entries(content)) == [{"ok": 1}, {"ok": 2}]
+
+
# =========================================================================== #
# _estimate_batch_entry_tokens (regression: an uncountable/malformed row must
# never contribute zero tokens, or a crafted batch could evade the TPM limit)