[Bug]: LiteLLM’s streaming handler should skip chunks with an empty choices array instead of indexing choices[0]

用户使用 LiteLLM 对接 Databricks 托管的 meta-llama/llama-4-maverick-17b-128e-instruct-fp8 端点,开启流式输出(streaming)并进行原生函数调用(native function calling)。在多个工具调用成功执行并生成

[Bug]: LiteLLM's streaming handler should skip chunks with an empty choices array instead of indexing choices[0]

[Bug]: LiteLLM’s streaming handler should skip chunks with an empty choices array instead of indexing choices[0]

快速结论:该报错通常发生在 LiteLLM 通过 Databricks 端点流式调用 Meta-Llama / Llama-4-Maverick 模型进行原生函数调用时。优先排查 LiteLLM 版本是否 >= 1.89.4,并检查 Databricks streaming_utils.py 中是否对空 choices 数组做了跳过处理。

问题场景

用户使用 LiteLLM 对接 Databricks 托管的 meta-llama/llama-4-maverick-17b-128e-instruct-fp8 端点,开启流式输出(streaming)并进行原生函数调用(native function calling)。在多个工具调用成功执行并生成最终回复后,流式传输立即失败,抛出数组索引异常。该异常在 Databricks 流式解析器将 chunk 发送到 OpenWebUI 之前发生。

报错原文

IndexError: list index out of range

litellm/llms/databricks/streaming_utils.py
processed_chunk.choices[0].delta.content

原因分析

可能原因:在流式传输过程中,Databricks 端点返回了某个 chunk,其 choices 数组为空(processed_chunk.choices[])。LiteLLM 的流式处理器在未检查数组是否为空的情况下直接索引 choices[0],导致 IndexError。该问题可能与 #30870 是同一类情况,也可能是 Databricks 流式解析器特有的回归问题。

环境排查

  • LiteLLM 版本:v1.89.4(被报告版本)
  • 模型:meta-llama/llama-4-maverick-17b-128e-instruct-fp8(Databricks 托管)
  • 端到端组件:LiteLLM → Databricks 端点 → OpenWebUI
  • 触发条件:流式模式 + 原生函数调用(多个 tool_calls 后)

解决步骤

  1. 检查 LiteLLM 当前版本是否已包含针对空 choices 数组的跳过逻辑。若无,建议升级到包含此修复的版本。
  2. 若无法升级,可临时在 litellm/llms/databricks/streaming_utils.py 中找到 processed_chunk.choices[0].delta.content 所在行,在访问前增加判断:
    if not processed_chunk.choices: continueif not processed_chunk.choices: return None(具体取决于函数上下文)。
  3. 关闭流式模式,改用非流式调用验证函数调用是否会正常完成,以确认问题仅出现在流式解析阶段。
  4. 如果上述方法有效,可向 LiteLLM 仓库提交补丁或 Issue 说明 Databricks 流式解析器需要与主逻辑一致的过滤处理。

验证方法

重新运行引发报错的流式函数调用请求,观察是否不再抛出 IndexError: list index out of range,且所有 streamed chunk 能被正确消费(包括模型生成的最终回复)。

参考来源

BerriAI/litellm #30870

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 13307

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注