[Bug]: Using DeepSeek with LlamaIndex causes a 400 Bad Request because LlamaIndex calls the deprecated /v1/completions text endpoint instead

当 LlamaIndex 配合 DeepSeek 模型使用 OpenAILike 时,会因默认调用已废弃的 /v1/completions 文本补全端点而返回 400 Bad Request。优先排查是否显式设置了 is_chat_model=True ,并确认 model 与 api_base 参

快速结论:当 LlamaIndex 配合 DeepSeek 模型使用 OpenAILike 时,会因默认调用已废弃的 /v1/completions 文本补全端点而返回 400 Bad Request。优先排查是否显式设置了 is_chat_model=True,并确认 modelapi_base 参数没有写反。

适用环境:LlamaIndex(版本范围 >=0.14.24,<0.15.0),llama-index-llms-openai-like 集成包,DeepSeek API(主域名 https://api.deepseek.com)。Issue 中未涉及 CUDA、显卡、Python 版本或操作系统信息。

最快修复方案:OpenAILike 初始化时显式添加 is_chat_model=True,让请求走 /v1/chat/completions 端点,这是 Issue 中明确验证过的解决方法。

注意事项:同时建议把 model 参数改为实际模型名(如 deepseek-chat)而不是 URL,并把 api_base 设为 API 地址;原始代码中两个参数值写反了,这属于已知的人为配置错误。

问题场景

用户在 LlamaIndex 中通过 OpenAILike 类接入 DeepSeek 模型,调用 complete() 方法时触发 400 Bad Request。框架实际请求的是 https://api.deepseek.com/v1/completions,而 DeepSeek 在主域名上已废弃该文本补全端点。

报错原文

Exception has occurred: BadRequestError
Error code: 400 - {'error': {'message': 'completions api is only available when using beta api (set base_url="https://api.deepseek.com/beta")', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}
httpx.HTTPStatusError: Client error '400 Bad Request' for url 'https://api.deepseek.com/v1/completions'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

openai.BadRequestError: Error code: 400 - {'error': {'message': 'completions api is only available when using beta api (set base_url="https://api.deepseek.com/beta")', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

原因分析

根本原因是 OpenAILikeis_chat_model 参数默认值为 False。当它为 False 时,无论调用 complete() 还是 chat(),都会统一路由到 /v1/completions 文本端点。DeepSeek 已在主域名上废弃该端点,因此返回 400。
另外原始代码中 modelapi_base 的值被写反了:model 应传模型名(deepseek-chat),api_base 才传 URL,这可能是导致行为异常的又一因素。

环境排查

  • 确认 LlamaIndex 版本是否在 >=0.14.24,<0.15.0 范围内。
  • 确认已安装 llama-index-llms-openai-like 且与主 LlamaIndex 版本匹配。
  • 确认 model 参数传的是 DeepSeek 模型名(如 deepseek-chatdeepseek-reasoner),而不是 URL。
  • 确认 api_base 参数传的是 https://api.deepseek.com/v1 这类 API 地址。

解决步骤

  1. 打开调用 OpenAILike 的代码文件,检查 modelapi_base 参数是否写反,修正为模型名与 API 地址各归其位。
  2. 在初始化参数中显式加入 is_chat_model=True,必要时一并设置 is_function_calling_model=True 以启用 DeepSeek 的函数调用能力。
  3. 参考修正后的参数写法重新初始化:
    online_llm = OpenAILike(
        model="deepseek-chat",
        api_base="https://api.deepseek.com/v1",
        api_key="sk-...",
        temperature=0.1,
        context_window=16384,
        is_chat_model=True,
        is_function_calling_model=True,
        system_prompt="You are a senior backend developer",
    )
    response = online_llm.complete("can you explain distributed systems like I'm 5")
  4. 重新运行代码,确认不再请求 /v1/completions 端点。

验证方法

重新执行 complete() 调用,若不再出现 400 Bad Request 且能正常返回文本,即可确认问题解决。也可以观察请求日志,确认目标 URL 已变为 https://api.deepseek.com/v1/chat/completions

参考来源

run-llama/llama_index #22846

GamsGo AI

AI 工具推荐

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

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

了解 GamsGo AI

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

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 20556

发表回复

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