Skip to content

Commit ff28eca

Browse files
authored
fix(openai): 修复流式响应末尾usage信息丢失问题 (AstrBotDevs#8306)
- 修复在流式处理过程中,因跳过 delta=None 且 choices=[] 的 usage chunk 导致最终 completion 丢失 usage 数据的问题 - 在 handle_chunk 调用条件中增加 chunk.usage 判断,确保末尾 usage chunk 能被正常传递给 state 处理 - 更新相关注释,说明 usage chunk 的例外情况,保障流式响应的 usage 信息完整性
1 parent dcc99e6 commit ff28eca

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

astrbot/core/provider/sources/openai_source.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,9 @@ async def _query_stream(
672672
# 跳过 delta=None 的 chunk,避免 SDK 内部 _convert_initial_chunk_into_snapshot
673673
# 第 747 行 choice.delta.to_dict() 抛出 NoneType 错误。
674674
# refs: AstrBot#6689 / openai-python#5069 / #5047
675-
if delta is not None:
675+
# 例外:流末尾的 usage chunk(choices=[],delta=None 但有 usage 数据)
676+
# 需要传给 state,否则最终 completion 会丢失 usage 信息
677+
if delta is not None or chunk.usage:
676678
try:
677679
state.handle_chunk(chunk)
678680
except Exception as e:

0 commit comments

Comments
 (0)