Langchain RunnableEach invoke error: response is not valid JSON
题意 :Langchain RunnableEach 调用错误:响应不是有效的 JSON。
问题背景:
I am trying to use RunnableEach to make parallel requests to OpenAI. The responses are supposed to be "yes/no" plus a motivation. I want the response to be in JSON format, my code is like the following:
我正在尝试使用 RunnableEach 向 OpenAI 发起并行请求。响应应该是“是/否”以及一个理由。我希望响应是 JSON 格式,下面是我的代码:
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables.base import RunnableEach
from langchain_openai import ChatOpenAI
class Classification(BaseModel):
flag: bool = Field(description="my descr",enum=[True, False])
answer: str = Field(description="my descr")
llm = ChatOpenAI(temperature=0, model="gpt-4o-mini", api_key=model_key).with_structured_output(
Classification)
prompt = my_prompt
tagging_chain = prompt | llm
runnable_each = RunnableEach(bound=tagging_chain)
input_list = [{"input": val} for val in mydata]
res = runnable_each.invoke(input_list)
I have around 25k elements in input list, that is 25k requests have to be processed. This has worked fine until today, it is failing because of an error in one request:
我在输入列表中大约有 25k 个元素,也就是说需要处理 25k 个请求。这一直运行得很好,直到今天,由于一个请求中的错误,导致它失败:
Function Classification arguments:
{"flag":false,"motivation":"The passage does not contain any information relevant to products
5. are not valid JSON. Received JSONDecodeError Unterminated string starting at: line 1 column 34 (char 33)
I understand that the call to the llm model returned a malformed string that when parsed as JSON (since I have constrained output), generates an error. I would like to know whether this kind of error can be handled in a way such that the entire process does not fail but only that request. Thanks in advance for your help.
我理解对 LLM 模型的调用返回了一个格式错误的字符串,当它被解析为 JSON 时(因为我限制了输出),会生成一个错误。我想知道这种类型的错误是否可以以某种方式处理,使得整个过程不会失败,而只是那个请求失败。提前感谢您的帮助。
Update I saw in the langchain documentation that a retry logic is implemented in the library:
更新:我在 langchain 文档中看到库中实现了重试逻辑:RunnableRetry — 🦜🔗 LangChain documentation By defining
tagging_chain = prompt | llm.with_retry()
runnable_each = RunnableEach(bound=tagging_chain)
Would it retry the single input that is failing or the entire input sequence?
它会重试单个失败的输入还是整个输入序列?
问题解决:
I found the below in the documentation
我在文档中找到了如下内容:
return_exceptions (bool) – Whether to return exceptions instead of raising them. Defaults to False.
So basically you just have to pass return_exceptions as True to the RunnableEach and it will just return it and not break the whole thing.
所以基本上你只需要将 return_exceptions 设置为 True 传递给 RunnableEach,它就会返回异常而不会中断整个过程。

