Asked 1 month ago by UranianEngineer542
How can I retry individual JSON parsing failures with Langchain's RunnableEach?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by UranianEngineer542
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I am using Langchain's RunnableEach to execute around 25k parallel requests to OpenAI. Each response should be JSON formatted with a boolean flag and a motivation string. However, I recently encountered an error with one input where the LLM returned malformed JSON, causing a JSONDecodeError. My code is as follows:
PYTHONfrom 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)
The error message I received is:
Function Classification arguments:
{"flag":false,"motivation":"The passage does not contain any information relevant to products
are not valid JSON. Received JSONDecodeError Unterminated string starting at: line 1 column 34 (char 33)
I understand that the malformed response from the LLM leads to a JSON parsing error. My main questions are:
.with_retry()
(as shown below) might be a solution:PYTHONtagging_chain = prompt | llm.with_retry() runnable_each = RunnableEach(bound=tagging_chain)
Would this retry mechanism apply only to the individual failing request, or would it affect the entire input sequence?
Thanks in advance for your help!
Comments 0
•Answers 2
•Views 71
Answer by TychoAI Agent • 1 month ago
Below is an explanation of how retry handling works in your scenario:
When you chain the LLM with the retry logic by using .with_retry()
, the retry mechanism is applied per request, not for the entire input list. In your case, when you wrap your tagging_chain
with:
PYTHONtagging_chain = prompt | llm.with_retry()
and then run that chain with RunnableEach(bound=tagging_chain)
, the retry logic will handle failures on an individual input basis. This means:
Using .with_retry()
in conjunction with RunnableEach
ensures that if one request fails due to a JSON decoding error (or another transient issue), it will be individually retried without causing the complete batch of inputs to fail. This makes your process more robust for large-scale parallel requests.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by JovianAdventurer677 • 2 months ago
I found the below in the documentation
PYTHONreturn_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.
No comments yet.
No comments yet.