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

LangChain V0.2多部分构建ChatPromptTemplate报错技术问询

问题描述

使用LangChain V0.2版本构建包含固定系统提示、可变人类备注提示、可变人类用户提示的ChatPromptTemplate,尝试通过链调用LLM获取结果时触发TypeError,提示期望Runnable、可调用对象或字典,却得到str类型。

相关代码:

from langchain_huggingface import HuggingFaceEndpoint

from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.prompts.chat import HumanMessagePromptTemplate, SystemMessagePromptTemplate

# 省略部分代码

system_template = "ABC"
system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)

notes_template = "EFG"
notes_message_prompt = HumanMessagePromptTemplate.from_template(notes_template)

user_template = "YXZ {this}"
user_message_prompt = HumanMessagePromptTemplate.from_template(user_template)

chat_prompt = ChatPromptTemplate.from_messages([
    system_message_prompt,
    notes_message_prompt,
    user_message_prompt
])

filled_chat_prompt = chat_prompt.format(this = 'some data')

# 省略部分代码

llm_chain = filled_chat_prompt | myllmFromHuggingFace

错误信息:

TypeError
Traceback (most recent call last)
<ipython-input-17-7697f8d4a2c1> in <cell line: 10>()
      8 )
      9 
---> 10 llm_chain = filled_chat_prompt | myllmFromHuggingFace

1 frames
/usr/local/lib/python3.10/dist-packages/langchain_core/runnables/base.py in coerce_to_runnable(thing)
   5513         return cast(Runnable[Input, Output], RunnableParallel(thing))
   5514     else:
-> 5515         raise TypeError(
   5516             f"Expected a Runnable, callable or dict."
   5517             f"Instead got an unsupported type: {type(thing)}"

TypeError: Expected a Runnable, callable or dict.Instead got an unsupported type: <class 'str'>
解决方案

问题原因

chat_prompt.format()方法返回的是格式化后的字符串,而LangChain V0.2的链式调用(|操作符)要求左侧必须是Runnable类型(比如ChatPromptTemplate本身就是Runnable),不能直接传入字符串,因此触发类型错误。

修复步骤

  1. 移除提前格式化字符串的代码filled_chat_prompt = chat_prompt.format(this = 'some data')
  2. 直接将ChatPromptTemplate实例与LLM链接,构建可运行的链
  3. 调用链时通过invoke()方法传入变量参数,链式调用会自动完成模板格式化

修复后的代码示例:

# 保留原有模板构建代码
system_template = "ABC"
system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)

notes_template = "EFG"
notes_message_prompt = HumanMessagePromptTemplate.from_template(notes_template)

user_template = "YXZ {this}"
user_message_prompt = HumanMessagePromptTemplate.from_template(user_template)

chat_prompt = ChatPromptTemplate.from_messages([
    system_message_prompt,
    notes_message_prompt,
    user_message_prompt
])

# 直接构建链,无需提前格式化
llm_chain = chat_prompt | myllmFromHuggingFace

# 调用链并传入变量参数
result = llm_chain.invoke({"this": "some data"})
print(result)

补充说明

如果需要单独查看格式化后的提示内容,可以使用chat_prompt.format_prompt(this='some data')方法,它返回的是PromptValue对象,而非字符串,也可以兼容链式调用,但通常直接使用模板实例链接LLM更符合LangChain V0.2的设计逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 14:12:36