Home / Class/ OutputFixingParser Class — langchain Architecture

OutputFixingParser Class — langchain Architecture

Architecture documentation for the OutputFixingParser class in fix.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  cbd1bd7d_786a_2287_38a2_3e4d067733c2["OutputFixingParser"]
  59284af4_d3b8_96bf_ceb4_0d2c90a20c87["fix.py"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|defined in| 59284af4_d3b8_96bf_ceb4_0d2c90a20c87
  2d46047d_64d3_9c57_b6a5_4558a1003399["is_lc_serializable()"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|method| 2d46047d_64d3_9c57_b6a5_4558a1003399
  cbca486d_c43c_8e2d_f76c_6e8fd649036b["from_llm()"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|method| cbca486d_c43c_8e2d_f76c_6e8fd649036b
  27861849_4f69_a2a4_017c_db20da14705d["parse()"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|method| 27861849_4f69_a2a4_017c_db20da14705d
  c06537ac_0eca_0592_087a_610e09bf6218["aparse()"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|method| c06537ac_0eca_0592_087a_610e09bf6218
  c5c0daae_f6e5_68d9_5565_5e2f022b06fc["get_format_instructions()"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|method| c5c0daae_f6e5_68d9_5565_5e2f022b06fc
  16dfb15b_4628_fa0b_7806_037ddf395b4b["_type()"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|method| 16dfb15b_4628_fa0b_7806_037ddf395b4b
  cc2547d4_7b44_bc61_3d5b_f00d07831aed["OutputType()"]
  cbd1bd7d_786a_2287_38a2_3e4d067733c2 -->|method| cc2547d4_7b44_bc61_3d5b_f00d07831aed

Relationship Graph

Source Code

libs/langchain/langchain_classic/output_parsers/fix.py lines 25–156

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

    @classmethod
    @override
    def is_lc_serializable(cls) -> bool:
        return True

    parser: Annotated[Any, 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[OutputFixingParserRetryChainInput, 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: Runnable,
        parser: BaseOutputParser[T],
        prompt: BasePromptTemplate = NAIVE_FIX_PROMPT,
        max_retries: int = 1,
    ) -> OutputFixingParser[T]:
        """Create an OutputFixingParser 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:
            OutputFixingParser
        """
        chain = prompt | llm | StrOutputParser()
        return cls(parser=parser, retry_chain=chain, max_retries=max_retries)

    @override
    def parse(self, completion: str) -> 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(
                        instructions=self.parser.get_format_instructions(),
                        completion=completion,
                        error=repr(e),
                    )
                else:
                    try:
                        completion = self.retry_chain.invoke(
                            {
                                "instructions": self.parser.get_format_instructions(),
                                "completion": completion,
                                "error": repr(e),
                            },
                        )
                    except (NotImplementedError, AttributeError):
                        # Case: self.parser does not have get_format_instructions
                        completion = self.retry_chain.invoke(
                            {
                                "completion": completion,
                                "error": repr(e),
                            },
                        )

        msg = "Failed to parse"
        raise OutputParserException(msg)

Domain

Frequently Asked Questions

What is the OutputFixingParser class?
OutputFixingParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/output_parsers/fix.py.
Where is OutputFixingParser defined?
OutputFixingParser is defined in libs/langchain/langchain_classic/output_parsers/fix.py at line 25.

Analyze Your Own Codebase

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

Try Supermodel Free