Home / Class/ RetryOutputParser Class — langchain Architecture

RetryOutputParser Class — langchain Architecture

Architecture documentation for the RetryOutputParser class in retry.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  056612cb_8291_0c46_ab1b_ac10284862d6["RetryOutputParser"]
  4d8d636f_d2d5_7fb9_287a_f135606f43b9["retry.py"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|defined in| 4d8d636f_d2d5_7fb9_287a_f135606f43b9
  e7c33adb_98e6_26c5_4783_69ac0fffe3d6["from_llm()"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|method| e7c33adb_98e6_26c5_4783_69ac0fffe3d6
  ea8d4ab5_903d_16fa_398f_656ae160bfd8["parse_with_prompt()"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|method| ea8d4ab5_903d_16fa_398f_656ae160bfd8
  af9365c1_7554_045e_da47_85d942fb6e5c["aparse_with_prompt()"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|method| af9365c1_7554_045e_da47_85d942fb6e5c
  b6353c05_1a1b_3d84_4db8_e8697219d234["parse()"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|method| b6353c05_1a1b_3d84_4db8_e8697219d234
  936f21df_34ef_a030_869d_6fea4c3b8983["get_format_instructions()"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|method| 936f21df_34ef_a030_869d_6fea4c3b8983
  98b781e7_c526_55dd_6d73_de318cdb0ea1["_type()"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|method| 98b781e7_c526_55dd_6d73_de318cdb0ea1
  ed5eb88b_9d00_a27b_bd26_96b74c90013c["OutputType()"]
  056612cb_8291_0c46_ab1b_ac10284862d6 -->|method| ed5eb88b_9d00_a27b_bd26_96b74c90013c

Relationship Graph

Source Code

libs/langchain/langchain_classic/output_parsers/retry.py lines 54–184

class RetryOutputParser(BaseOutputParser[T]):
    """Wrap a parser and try to fix parsing errors.

    Does this by passing the original prompt and the completion to another
    LLM, and telling it the completion did not satisfy criteria in the prompt.
    """

    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[RetryOutputParserRetryChainInput, 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_PROMPT,
        max_retries: int = 1,
    ) -> RetryOutputParser[T]:
        """Create an RetryOutputParser from a language model and a parser.

        Args:
            llm: llm to use for fixing
            parser: parser to use for parsing
            prompt: prompt to use for fixing
            max_retries: Maximum number of retries to parse.

        Returns:
            RetryOutputParser
        """
        chain = prompt | llm | StrOutputParser()
        return cls(parser=parser, retry_chain=chain, max_retries=max_retries)

    def parse_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.

        Returns:
            The parsed completion.
        """
        retries = 0

        while retries <= self.max_retries:
            try:
                return self.parser.parse(completion)
            except OutputParserException:
                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,
                    )
                else:
                    completion = self.retry_chain.invoke(
                        {
                            "prompt": prompt_value.to_string(),
                            "completion": completion,
                        },
                    )

        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.

Domain

Frequently Asked Questions

What is the RetryOutputParser class?
RetryOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/output_parsers/retry.py.
Where is RetryOutputParser defined?
RetryOutputParser is defined in libs/langchain/langchain_classic/output_parsers/retry.py at line 54.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free