From c1d5c3d9e8ff3eecc94c63a956c9d1cbe46b4a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E7=A9=BA?= <3103908461@qq.com> Date: Tue, 12 Aug 2025 02:41:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(stream):=20=E8=B7=B3=E8=BF=87=E7=A9=BA=20ch?= =?UTF-8?q?oices=20=E7=9A=84=20SSE=20=E5=B8=A7=E5=B9=B6=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=20usage=EF=BC=8C=E9=81=BF=E5=85=8D=E6=B5=81=E5=BC=8F=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E8=B6=8A=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/llm_models/model_client/openai_client.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/llm_models/model_client/openai_client.py b/src/llm_models/model_client/openai_client.py index 0b4f1e709..6902889c5 100644 --- a/src/llm_models/model_client/openai_client.py +++ b/src/llm_models/model_client/openai_client.py @@ -270,7 +270,15 @@ async def _default_stream_response_handler( # 如果中断量被设置,则抛出ReqAbortException _insure_buffer_closed() raise ReqAbortException("请求被外部信号中断") - + # 空 choices / usage-only 帧的防御 + if not getattr(event, "choices", None) or len(event.choices) == 0: + if getattr(event, "usage", None): + _usage_record = ( + event.usage.prompt_tokens or 0, + event.usage.completion_tokens or 0, + event.usage.total_tokens or 0, + ) + continue # 跳过本帧,避免访问 choices[0] delta = event.choices[0].delta # 获取当前块的delta内容 if hasattr(delta, "reasoning_content") and delta.reasoning_content: # type: ignore