RetryWithErrorOutputParser Class — langchain Architecture
Architecture documentation for the RetryWithErrorOutputParser class in retry.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD d9504162_cd94_4db4_a3ec_7621a6287225["RetryWithErrorOutputParser"] 4d8d636f_d2d5_7fb9_287a_f135606f43b9["retry.py"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|defined in| 4d8d636f_d2d5_7fb9_287a_f135606f43b9 ab2db9cb_d383_bd44_2851_8e949fd4a85d["from_llm()"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|method| ab2db9cb_d383_bd44_2851_8e949fd4a85d 00b0c9ad_c753_b223_9358_1381afe521f7["parse_with_prompt()"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|method| 00b0c9ad_c753_b223_9358_1381afe521f7 95d8cdd7_ad78_0741_b125_a6ce998f7adf["aparse_with_prompt()"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|method| 95d8cdd7_ad78_0741_b125_a6ce998f7adf f11f8cb3_fdcb_f508_d145_d819a05030ba["parse()"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|method| f11f8cb3_fdcb_f508_d145_d819a05030ba bdd0a4ae_78bb_dc2d_ac6f_741429599873["get_format_instructions()"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|method| bdd0a4ae_78bb_dc2d_ac6f_741429599873 29ea006c_5b63_4bce_4600_e5b80eb2a313["_type()"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|method| 29ea006c_5b63_4bce_4600_e5b80eb2a313 58fc20e1_336a_07f5_656e_21660e6924a9["OutputType()"] d9504162_cd94_4db4_a3ec_7621a6287225 -->|method| 58fc20e1_336a_07f5_656e_21660e6924a9
Relationship Graph
Source Code
libs/langchain/langchain_classic/output_parsers/retry.py lines 187–315
class RetryWithErrorOutputParser(BaseOutputParser[T]):
"""Wrap a parser and try to fix parsing errors.
Does this by passing the original prompt, the completion, AND the error
that was raised to another language model and telling it that the completion
did not work, and raised the given error. Differs from RetryOutputParser
in that this implementation provides the error that was raised back to the
LLM, which in theory should give it more information on how to fix it.
"""
parser: Annotated[BaseOutputParser[T], SkipValidation()]
"""The parser to use to parse the output."""
# Should be an LLMChain but we want to avoid top-level imports from
# langchain_classic.chains
retry_chain: Annotated[
RunnableSerializable[RetryWithErrorOutputParserRetryChainInput, str] | Any,
SkipValidation(),
]
"""The RunnableSerializable to use to retry the completion (Legacy: LLMChain)."""
max_retries: int = 1
"""The maximum number of times to retry the parse."""
legacy: bool = True
"""Whether to use the run or arun method of the retry_chain."""
@classmethod
def from_llm(
cls,
llm: BaseLanguageModel,
parser: BaseOutputParser[T],
prompt: BasePromptTemplate = NAIVE_RETRY_WITH_ERROR_PROMPT,
max_retries: int = 1,
) -> RetryWithErrorOutputParser[T]:
"""Create a RetryWithErrorOutputParser from an LLM.
Args:
llm: The LLM to use to retry the completion.
parser: The parser to use to parse the output.
prompt: The prompt to use to retry the completion.
max_retries: The maximum number of times to retry the completion.
Returns:
A RetryWithErrorOutputParser.
"""
chain = prompt | llm | StrOutputParser()
return cls(parser=parser, retry_chain=chain, max_retries=max_retries)
@override
def parse_with_prompt(self, completion: str, prompt_value: PromptValue) -> T:
retries = 0
while retries <= self.max_retries:
try:
return self.parser.parse(completion)
except OutputParserException as e:
if retries == self.max_retries:
raise
retries += 1
if self.legacy and hasattr(self.retry_chain, "run"):
completion = self.retry_chain.run(
prompt=prompt_value.to_string(),
completion=completion,
error=repr(e),
)
else:
completion = self.retry_chain.invoke(
{
"completion": completion,
"prompt": prompt_value.to_string(),
"error": repr(e),
},
)
msg = "Failed to parse"
raise OutputParserException(msg)
async def aparse_with_prompt(self, completion: str, prompt_value: PromptValue) -> T:
"""Parse the output of an LLM call using a wrapped parser.
Args:
completion: The chain completion to parse.
prompt_value: The prompt to use to parse the completion.
Domain
Source
Frequently Asked Questions
What is the RetryWithErrorOutputParser class?
RetryWithErrorOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/output_parsers/retry.py.
Where is RetryWithErrorOutputParser defined?
RetryWithErrorOutputParser is defined in libs/langchain/langchain_classic/output_parsers/retry.py at line 187.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free