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

langchain及langchain_community报错:缺少recursive_guard关键字参数

解决TypeError: ForwardRef._evaluate() missing 'recursive_guard'参数问题

问题背景

突然langchain_community和langchain包抛出以下错误:

TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

错误源于pydantic组件,已尝试升级langchain、langchain_community、pydantic、langsmith等包,但未解决。当前环境版本:

Python 3.12.4

langchain: 0.2.3
langchain_community: 0.2.4
langsmith: 0.1.75
pydantic: 2.7.3
typing_extensions: 4.11.0

pip check未检测到冲突,完整错误栈见用户提供内容。

原因分析

从错误栈可见,实际调用的是pydantic/v1版本的代码(路径pydantic\v1\main.py),而Python 3.12对ForwardRef._evaluate()方法新增了recursive_guard必填参数。旧版本的pydantic v1不兼容这个Python 3.12的API变化——即使你安装了pydantic 2.x,langsmith等依赖仍在使用pydantic v1,最终导致参数缺失错误。

解决方案

方案1:升级pydantic v1到兼容Python 3.12的版本

卸载现有pydantic相关包,安装支持Python 3.12的pydantic v1版本:

pip uninstall -y pydantic pydantic-core
pip install "pydantic<2,>=1.10.12"

pydantic 1.10.12及以后的v1版本已适配Python 3.12的ForwardRef参数变化。

方案2:升级langsmith到最新版本

部分较新的langsmith版本已切换到pydantic v2,尝试更新langsmith:

pip install --upgrade langsmith

更新后检查是否仍依赖pydantic v1,若问题解决则无需调整pydantic版本。

方案3:创建虚拟环境重新安装依赖

如果上述方法无效,建议创建全新虚拟环境,重新安装所有依赖,避免环境中残留的旧版本包干扰:

# Windows系统创建并激活虚拟环境
python -m venv langchain_env
.\langchain_env\Scripts\activate

# 安装指定版本依赖
pip install langchain==0.2.3 langchain_community==0.2.4 langsmith==0.1.75 "pydantic<2,>=1.10.12" typing_extensions==4.11.0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 16:47:10