From fa2b38b2bc7ab58142f143bfc051d20675111807 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 1 Aug 2026 01:09:48 -0400 Subject: [PATCH] refac --- cptr/utils/ai.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cptr/utils/ai.py b/cptr/utils/ai.py index f40e69b..91d614b 100644 --- a/cptr/utils/ai.py +++ b/cptr/utils/ai.py @@ -779,6 +779,12 @@ async def stream_openai_completions( emitted = True yield {"type": "output", "item": item} raw = chunk["usage"] + usage_tokens = sum( + raw.get(key, 0) + for key in ("prompt_tokens", "completion_tokens", "total_tokens") + ) + if not emitted and usage_tokens <= 0: + continue emitted = True yield { "type": "usage", @@ -792,6 +798,11 @@ async def stream_openai_completions( if item is not None: emitted = True yield {"type": "output", "item": item} + if not emitted: + raise RuntimeError( + "Upstream provider returned an empty completion with no text, " + "output items, tool calls, or usage tokens." + ) emitted = True yield {"type": "done"} return