From ff28eca9ca17d7d56acef587ba4cde9cdcf72b9c Mon Sep 17 00:00:00 2001 From: NayukiChiba Date: Sat, 23 May 2026 23:17:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(openai):=20=E4=BF=AE=E5=A4=8D=E6=B5=81?= =?UTF-8?q?=E5=BC=8F=E5=93=8D=E5=BA=94=E6=9C=AB=E5=B0=BEusage=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=B8=A2=E5=A4=B1=E9=97=AE=E9=A2=98=20(#8306)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复在流式处理过程中,因跳过 delta=None 且 choices=[] 的 usage chunk 导致最终 completion 丢失 usage 数据的问题 - 在 handle_chunk 调用条件中增加 chunk.usage 判断,确保末尾 usage chunk 能被正常传递给 state 处理 - 更新相关注释,说明 usage chunk 的例外情况,保障流式响应的 usage 信息完整性 --- astrbot/core/provider/sources/openai_source.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/astrbot/core/provider/sources/openai_source.py b/astrbot/core/provider/sources/openai_source.py index 1ee9a3fda..117cfb492 100644 --- a/astrbot/core/provider/sources/openai_source.py +++ b/astrbot/core/provider/sources/openai_source.py @@ -672,7 +672,9 @@ class ProviderOpenAIOfficial(Provider): # 跳过 delta=None 的 chunk,避免 SDK 内部 _convert_initial_chunk_into_snapshot # 第 747 行 choice.delta.to_dict() 抛出 NoneType 错误。 # refs: AstrBot#6689 / openai-python#5069 / #5047 - if delta is not None: + # 例外:流末尾的 usage chunk(choices=[],delta=None 但有 usage 数据) + # 需要传给 state,否则最终 completion 会丢失 usage 信息 + if delta is not None or chunk.usage: try: state.handle_chunk(chunk) except Exception as e: