Home / Class/ MRKLOutputParser Class — langchain Architecture

MRKLOutputParser Class — langchain Architecture

Architecture documentation for the MRKLOutputParser class in output_parser.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  08bb1b5b_dfcb_98d0_741f_e0ad60fa4965["MRKLOutputParser"]
  d9685a4b_26b0_bca9_f857_03bb2ffc9dd4["AgentOutputParser"]
  08bb1b5b_dfcb_98d0_741f_e0ad60fa4965 -->|extends| d9685a4b_26b0_bca9_f857_03bb2ffc9dd4
  765c8570_f257_beb4_6fbe_fe754d8e485a["output_parser.py"]
  08bb1b5b_dfcb_98d0_741f_e0ad60fa4965 -->|defined in| 765c8570_f257_beb4_6fbe_fe754d8e485a
  6c5397dd_ce34_093d_d6e6_5211cffe4121["get_format_instructions()"]
  08bb1b5b_dfcb_98d0_741f_e0ad60fa4965 -->|method| 6c5397dd_ce34_093d_d6e6_5211cffe4121
  f09ccc25_3c92_f035_5332_d2cdd3fbe6eb["parse()"]
  08bb1b5b_dfcb_98d0_741f_e0ad60fa4965 -->|method| f09ccc25_3c92_f035_5332_d2cdd3fbe6eb
  d94dc313_bc2c_5b1c_341d_a6666757d2b0["_type()"]
  08bb1b5b_dfcb_98d0_741f_e0ad60fa4965 -->|method| d94dc313_bc2c_5b1c_341d_a6666757d2b0

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/mrkl/output_parser.py lines 21–101

class MRKLOutputParser(AgentOutputParser):
    """MRKL Output parser for the chat agent."""

    format_instructions: str = FORMAT_INSTRUCTIONS
    """Default formatting instructions"""

    def get_format_instructions(self) -> str:
        """Returns formatting instructions for the given output parser."""
        return self.format_instructions

    def parse(self, text: str) -> AgentAction | AgentFinish:
        """Parse the output from the agent into an AgentAction or AgentFinish object.

        Args:
            text: The text to parse.

        Returns:
            An AgentAction or AgentFinish object.

        Raises:
            OutputParserException: If the output could not be parsed.
        """
        includes_answer = FINAL_ANSWER_ACTION in text
        regex = (
            r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
        )
        action_match = re.search(regex, text, re.DOTALL)
        if action_match and includes_answer:
            if text.find(FINAL_ANSWER_ACTION) < text.find(action_match.group(0)):
                # if final answer is before the hallucination, return final answer
                start_index = text.find(FINAL_ANSWER_ACTION) + len(FINAL_ANSWER_ACTION)
                end_index = text.find("\n\n", start_index)
                return AgentFinish(
                    {"output": text[start_index:end_index].strip()},
                    text[:end_index],
                )
            msg = f"{FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: {text}"
            raise OutputParserException(msg)

        if action_match:
            action = action_match.group(1).strip()
            action_input = action_match.group(2)
            tool_input = action_input.strip(" ")
            # ensure if its a well formed SQL query we don't remove any trailing " chars
            if tool_input.startswith("SELECT ") is False:
                tool_input = tool_input.strip('"')

            return AgentAction(action, tool_input, text)

        if includes_answer:
            return AgentFinish(
                {"output": text.rsplit(FINAL_ANSWER_ACTION, maxsplit=1)[-1].strip()},
                text,
            )

        if not re.search(r"Action\s*\d*\s*:[\s]*(.*?)", text, re.DOTALL):
            msg = f"Could not parse LLM output: `{text}`"
            raise OutputParserException(
                msg,
                observation=MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE,
                llm_output=text,
                send_to_llm=True,
            )
        if not re.search(
            r"[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)",
            text,
            re.DOTALL,
        ):
            msg = f"Could not parse LLM output: `{text}`"
            raise OutputParserException(
                msg,
                observation=MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE,
                llm_output=text,
                send_to_llm=True,
            )
        msg = f"Could not parse LLM output: `{text}`"
        raise OutputParserException(msg)

    @property
    def _type(self) -> str:
        return "mrkl"

Frequently Asked Questions

What is the MRKLOutputParser class?
MRKLOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/mrkl/output_parser.py.
Where is MRKLOutputParser defined?
MRKLOutputParser is defined in libs/langchain/langchain_classic/agents/mrkl/output_parser.py at line 21.
What does MRKLOutputParser extend?
MRKLOutputParser extends AgentOutputParser.

Analyze Your Own Codebase

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

Try Supermodel Free