Home / Class/ ChatOutputParser Class — langchain Architecture

ChatOutputParser Class — langchain Architecture

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

Entity Profile

Dependency Diagram

graph TD
  e700deb7_c623_0cec_a453_408791c8e2fe["ChatOutputParser"]
  6701f111_459b_a5ab_36de_5950dae06e0e["AgentOutputParser"]
  e700deb7_c623_0cec_a453_408791c8e2fe -->|extends| 6701f111_459b_a5ab_36de_5950dae06e0e
  ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220["output_parser.py"]
  e700deb7_c623_0cec_a453_408791c8e2fe -->|defined in| ddc27fdd_9d1e_cd56_1c6a_9d0e974eb220
  08d5a45f_2058_abb5_200a_af70d9fcaeb7["get_format_instructions()"]
  e700deb7_c623_0cec_a453_408791c8e2fe -->|method| 08d5a45f_2058_abb5_200a_af70d9fcaeb7
  88ebf526_9913_4409_4ebc_dec61c9330fb["parse()"]
  e700deb7_c623_0cec_a453_408791c8e2fe -->|method| 88ebf526_9913_4409_4ebc_dec61c9330fb
  4bff1be6_1001_88fb_48df_7e1a39b91092["_type()"]
  e700deb7_c623_0cec_a453_408791c8e2fe -->|method| 4bff1be6_1001_88fb_48df_7e1a39b91092

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/chat/output_parser.py lines 14–71

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

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

    pattern: Pattern = re.compile(r"^.*?`{3}(?:json)?\n(.*?)`{3}.*?$", re.DOTALL)
    """Regex pattern to parse the output."""

    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.
            ValueError: If the action could not be found.
        """
        includes_answer = FINAL_ANSWER_ACTION in text
        try:
            found = self.pattern.search(text)
            if not found:
                # Fast fail to parse Final Answer.
                msg = "action not found"
                raise ValueError(msg)
            action = found.group(1)
            response = json.loads(action.strip())
            includes_action = "action" in response
            if includes_answer and includes_action:
                msg = (
                    "Parsing LLM output produced a final answer "
                    f"and a parse-able action: {text}"
                )
                raise OutputParserException(msg)
            return AgentAction(
                response["action"],
                response.get("action_input", {}),
                text,
            )

        except Exception as exc:
            if not includes_answer:
                msg = f"Could not parse LLM output: {text}"
                raise OutputParserException(msg) from exc
            output = text.rsplit(FINAL_ANSWER_ACTION, maxsplit=1)[-1].strip()
            return AgentFinish({"output": output}, text)

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

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free