Home / Function/ parse() — langchain Function Reference

parse() — langchain Function Reference

Architecture documentation for the parse() function in output_parser.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e36f5f6f_eeb5_37b1_0b7c_182b69a3517c["parse()"]
  9160decd_83bc_a33b_26b8_0257282d7855["ConvoOutputParser"]
  e36f5f6f_eeb5_37b1_0b7c_182b69a3517c -->|defined in| 9160decd_83bc_a33b_26b8_0257282d7855
  style e36f5f6f_eeb5_37b1_0b7c_182b69a3517c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/conversational_chat/output_parser.py lines 22–51

    def parse(self, text: str) -> AgentAction | AgentFinish:
        """Attempts to parse the given text into an AgentAction or AgentFinish.

        Raises:
             OutputParserException if parsing fails.
        """
        try:
            # Attempt to parse the text into a structured format (assumed to be JSON
            # stored as markdown)
            response = parse_json_markdown(text)

            # If the response contains an 'action' and 'action_input'
            if "action" in response and "action_input" in response:
                action, action_input = response["action"], response["action_input"]

                # If the action indicates a final answer, return an AgentFinish
                if action == "Final Answer":
                    return AgentFinish({"output": action_input}, text)
                # Otherwise, return an AgentAction with the specified action and
                # input
                return AgentAction(action, action_input, text)
            # If the necessary keys aren't present in the response, raise an
            # exception
            msg = f"Missing 'action' or 'action_input' in LLM output: {text}"
            raise OutputParserException(msg)
        except Exception as e:
            # If any other exception is raised during parsing, also raise an
            # OutputParserException
            msg = f"Could not parse LLM output: {text}"
            raise OutputParserException(msg) from e

Subdomains

Frequently Asked Questions

What does parse() do?
parse() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/conversational_chat/output_parser.py.
Where is parse() defined?
parse() is defined in libs/langchain/langchain_classic/agents/conversational_chat/output_parser.py at line 22.

Analyze Your Own Codebase

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

Try Supermodel Free