快速结论:该报错发生在 LiteLLM 代理通过 /v1/messages 接口(route_type=anthropic_messages)转发非 Anthropic 后端(如 GLM-5.1)的流式响应时,后端在流末尾发送了一个仅含 usage、choices 为空的 chunk,导致 _should_start_new_content_block 在访问 chunk.choices[0] 时抛出 IndexError。优先排查你的 LiteLLM 版本是否已包含修复(#35314)。
适用环境:LiteLLM Proxy v1.83.14;GLM-5.1 模型后端;/v1/messages + stream=true;route_type=anthropic_messages。该问题为模型后端相关,MiniMax-M2.5 在同一代理上不会触发。
最快修复方案:升级 LiteLLM 到包含 PR #35314 的版本(该 PR 已修复此问题并合入 main 分支),当前 Issue 已确认在 main 上不再复现。
注意事项:若无法立即升级,可先尝试在代理层面拦截空的 choices chunk 或使用不发送 usage-only chunk 的后端模型规避;但该方案未被 Issue 验证。
问题场景
使用 LiteLLM Proxy 1.83.14,通过 /v1/messages 接口(route_type=anthropic_messages)路由 GLM-5.1 模型的流式请求(stream=true)。当后端在流末尾发送仅含 usage 信息、choices 为空的 chunk 时,LiteLLM 的流式适配器崩溃,导致客户端收到错误的流结束事件。
报错原文
IndexError: list index out of range
File ".../litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py", line 262, in __anext__
should_start_new_block = self._should_start_new_content_block(chunk)
File ".../litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py", line 474, in _should_start_new_content_block
if chunk.choices[0].finish_reason is not None:
~~~~~~~~~~~~~^^^
IndexError: list index out of range
原因分析
可能原因:streaming_iterator.py 中的 _should_start_new_content_block 方法无条件访问 chunk.choices[0],但部分 OpenAI 格式的后端会在流末尾发送仅含 usage、choices 为空的 chunk。该 chunk 在 LiteLLM 的适配器中触发 IndexError: list index out of range。此问题与后端实现相关——GLM-5.1 会发送这种空 choices chunk,而 MiniMax-M2.5 不会。类似的无保护 choices[0] 访问模式也出现在 streaming_handler.py::raise_on_model_repetition(#27928)和 Vertex Gemini(#28884)中。
环境排查
- 确认 LiteLLM Proxy 版本:v1.83.14(Issue 报告版本),或升级后版本
- 确认后端模型:GLM-5.1(触发问题),MiniMax-M2.5(不触发)
- 确认路由配置:
route_type=anthropic_messages,接口/v1/messages,stream=true - 检查
streaming_iterator.py中_should_start_new_content_block和__next__/__anext__的is_final_chunk判断是否有choices长度检查
解决步骤
- 升级 LiteLLM 版本,确保包含 PR #35314 的修复(该 PR 已合入 main,并带有回归测试,覆盖此问题)。
- 若无法立即升级,可在代理层或后端层面拦截空的
choiceschunk,或换用不发送 usage-only chunk 的后端模型。 - 对于开发者:在
_should_start_new_content_block及is_final_chunk判断处增加choices长度检查,空choices时返回should_start_new_block=False/不视为最终 chunk,让usage通过现有will_merge_into_held路径正常处理。
验证方法
升级后使用原始复现命令(curl -N 'http://<litellm-host>:4000/v1/messages' ...,模型设为 glm-5.1)重新发送流式请求,观察 SSE 流末尾是否正常收到 message_delta/message_stop,而无 typeless 错误事件。也可手动构造仅含 usage、choices: [] 的 chunk 进行单元测试,确认不再抛出 IndexError。
参考来源
AI 工具推荐
想把多个 AI 模型放在一个入口?
GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。
推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。
这个方案解决了吗?
可以继续搜索完整报错,或查看同一工具的其他排查指南。
![[Question]: error when attaching file in chat ----AttributeError("'Request' object has no attribute 'file'")](https://www.chat-gpts.plus/wp-content/uploads/2026/08/11805-40332ec3-768x403.jpg)
![[Question]: No keyword or question was found in dataSet afer files loaded by customized ingestion pipeline](https://www.chat-gpts.plus/wp-content/uploads/2026/08/11474-a36958b1-768x403.jpg)
