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
  88ebf526_9913_4409_4ebc_dec61c9330fb["parse()"]
  e700deb7_c623_0cec_a453_408791c8e2fe["ChatOutputParser"]
  88ebf526_9913_4409_4ebc_dec61c9330fb -->|defined in| e700deb7_c623_0cec_a453_408791c8e2fe
  style 88ebf526_9913_4409_4ebc_dec61c9330fb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/chat/output_parser.py lines 27–67

    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)

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free