From cd06169b2fb2ba60804705f7ad82405d365b2f48 Mon Sep 17 00:00:00 2001 From: fawney19 Date: Fri, 19 Dec 2025 01:44:35 +0800 Subject: [PATCH] fix: detect OpenAI format stream completion via finish_reason - Add detection of finish_reason in OpenAI API responses to mark stream completion - Ensures OpenAI API streams are properly marked as complete even without explicit completion events - Complements existing completion event detection for other API formats --- src/api/handlers/base/stream_processor.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/api/handlers/base/stream_processor.py b/src/api/handlers/base/stream_processor.py index 5429d1e..a47c2c7 100644 --- a/src/api/handlers/base/stream_processor.py +++ b/src/api/handlers/base/stream_processor.py @@ -127,6 +127,13 @@ class StreamProcessor: if event_type in ("response.completed", "message_stop"): ctx.has_completion = True + # 检查 OpenAI 格式的 finish_reason + choices = data.get("choices", []) + if choices and isinstance(choices, list) and len(choices) > 0: + finish_reason = choices[0].get("finish_reason") + if finish_reason is not None: + ctx.has_completion = True + async def prefetch_and_check_error( self, byte_iterator: Any,