You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Llama-Index导入报错:无法从langchain.schema导入FunctionMessage

解决无法从langchain.schema导入FunctionMessage的问题

报错详情

ImportError
cannot import name 'FunctionMessage' from 'langchain.schema' (/Users/janiobonfim/Projetos-VSCode/env/lib/python3.11/site-packages/langchain/schema.py)
  File "/Users/janiobonfim/Projetos-VSCode/main.py", line 9, in <module>
    from llama_index import GPTVectorStoreIndex, download_loader, MockLLMPredictor, ServiceContext
ImportError: cannot import name 'FunctionMessage' from 'langchain.schema' (/Users/janiobonfim/Projetos-VSCode/env/lib/python3.11/site-packages/langchain/schema.py)

代码用于从Google Docs提取文本,通过Llama-Index构建索引并对接OpenAI ChatGPT,已尝试更新Llama-Index和LangChain版本,问题未解决。

问题原因

核心是Llama-Index与LangChain版本不兼容:Llama-Index的当前版本依赖LangChain中langchain.schema路径下的FunctionMessage类,但你安装的LangChain版本中该类不存在、被移除或迁移到了其他模块。

解决步骤

  • 安装兼容版本的LangChain
    卸载现有LangChain,安装与Llama-Index匹配的稳定版本,执行以下命令(确保在项目虚拟环境中操作):

    # 激活虚拟环境(macOS/Linux)
    source /Users/janiobonfim/Projetos-VSCode/env/bin/activate
    # 卸载当前LangChain
    pip uninstall -y langchain
    # 安装兼容版本(示例版本,可根据Llama-Index版本调整)
    pip install langchain==0.0.286
    
  • 验证虚拟环境依赖
    执行pip list查看已安装包版本,确认LangChain版本为指定的兼容版本,同时Llama-Index版本也处于稳定兼容区间。

  • 降级Llama-Index到稳定版本(可选)
    若上述操作无效,尝试安装Llama-Index的稳定兼容版本:

    pip uninstall -y llama-index
    pip install llama-index==0.8.35
    
  • 检查导入路径(不推荐手动修改)
    若仍报错,可查看/Users/janiobonfim/Projetos-VSCode/env/lib/python3.11/site-packages/langchain/schema.py文件,确认FunctionMessage是否存在。若该类已迁移到langchain.schema.messages,优先通过版本匹配解决,而非手动修改第三方库代码。

内容的提问来源于stack exchange,提问作者Jan Ferrer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 00:04:58