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
This commit is contained in:
fawney19
2025-12-19 01:44:35 +08:00
parent 50ffd47546
commit cd06169b2f

View File

@@ -127,6 +127,13 @@ class StreamProcessor:
if event_type in ("response.completed", "message_stop"): if event_type in ("response.completed", "message_stop"):
ctx.has_completion = True 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( async def prefetch_and_check_error(
self, self,
byte_iterator: Any, byte_iterator: Any,