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

LangChain升级至0.0.295+后Vertex AI模型抛出AttributeError求助

问题解决:LangChain升级后Vertex AI的AttributeError错误

错误根源

LangChain 0.0.295及以上版本调整了Vertex AI聊天模型(ChatVertexAI)的内部处理逻辑,同时若依赖的google-cloud-aiplatform库版本过低,会导致响应结构不匹配,触发'TextGenerationResponse' object has no attribute 'candidates'错误。

解决方案

方案1:升级依赖的Google云AI平台库

确保google-cloud-aiplatform版本与LangChain新版本兼容:

pip install --upgrade google-cloud-aiplatform

方案2:适配Chat模型的ConversationChain配置

ConversationChain默认prompt针对普通LLM模型,需改用ChatPromptTemplate适配ChatVertexAI:

from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.chains import ConversationChain

def __initialize_basic_chain(self, llm, memory):
    # 构建适配Chat模型的prompt
    prompt = ChatPromptTemplate.from_messages([
        ("system", "以下是人类与AI的友好对话。AI善于交流,会结合上下文给出具体细节;若不知道答案,会如实说明。"),
        MessagesPlaceholder(variable_name="history"),
        ("human", "{input}")
    ])
    return ConversationChain(llm=llm, memory=memory, prompt=prompt)

方案3:改用普通LLM模型(若业务允许)

如果无需使用聊天模型chat-bison,可切换为文本模型text-bison并使用VertexAI类:

from langchain.llms import VertexAI

case 'public_general_palm':
llm = VertexAI(model_name="text-bison", temperature=0)
chain = self.__initialize_basic_chain(llm, self.chain_memory)
return chain(request.msg, return_only_outputs=True)

方案4:临时降级LangChain版本

若上述方案无法快速生效,可降级到之前稳定运行的版本:

pip install langchain==0.0.294

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 20:02:58