From 9aae08baa11541a49a5c33a936661371907fc4f1 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 23 Jul 2026 12:53:22 -0400 Subject: [PATCH] refac --- cptr/utils/ai.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cptr/utils/ai.py b/cptr/utils/ai.py index e461f20..60d80a7 100644 --- a/cptr/utils/ai.py +++ b/cptr/utils/ai.py @@ -554,9 +554,14 @@ async def stream_openai_completions( return item async for line in resp.aiter_lines(): - if not line.startswith("data: ") or line == "data: [DONE]": + if not line.startswith("data:"): continue - chunk = json.loads(line[6:]) + raw = line.removeprefix("data:").strip() + if raw == "[DONE]": + break + if not raw: + continue + chunk = json.loads(raw) choices = chunk.get("choices", []) delta = choices[0].get("delta", {}) if choices else {}