fix(batches): skip undecodable batch output lines when costing

This commit is contained in:
mateo-berri 2026-08-19 14:35:01 -07:00
parent 51cafe4365
commit 5eeccf69b6
2 changed files with 6 additions and 1 deletions

View file

@ -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):

View file

@ -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)