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

LCEL链中调用Parent Document Retriever报错expected string or buffer

问题分析与解决

你遇到的expected string or buffer错误,核心原因是LCEL链中检索器的输入类型不匹配:

  • 单独调用retriever_2.invoke("justice breyer")时,传入的是字符串类型的查询词,符合检索器的输入要求;
  • 集成到RunnableParallel后,检索器默认接收整个输入字典{"user_input": "justice breyer"},而不是字典里的字符串值,导致类型不兼容报错。

修复方案

调整RunnableParallel的结构,让检索器只接收user_input对应的字符串值,以下是两种规范写法:

写法1:使用Lambda函数提取字段

full_chain = RunnableParallel(
    {"user_input": RunnablePassthrough(), 
     "retriever": lambda input_data: retriever_2.invoke(input_data["user_input"])}
) | llm

写法2:使用RunnablePick(更符合LCEL风格)

from langchain_core.runnables import RunnablePick

full_chain = RunnableParallel(
    user_input=RunnablePassthrough(),
    retriever=RunnablePick("user_input") | retriever_2
) | llm

修改后再调用full_chain.invoke({"user_input": "justice breyer"}, config=cfg),检索器就能拿到正确的字符串查询词,避免类型错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:43:22