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
相关产品推荐
相关产品推荐

