Home / Function/ parse() — langchain Function Reference

parse() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  13888524_294f_7757_e851_426d567cce7f["parse()"]
  a021acf8_977f_5883_b651_b38a87dbf99d["XMLAgentOutputParser"]
  13888524_294f_7757_e851_426d567cce7f -->|defined in| a021acf8_977f_5883_b651_b38a87dbf99d
  1a2cbbc1_432e_c96e_372c_414265410793["_unescape()"]
  13888524_294f_7757_e851_426d567cce7f -->|calls| 1a2cbbc1_432e_c96e_372c_414265410793
  style 13888524_294f_7757_e851_426d567cce7f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/output_parsers/xml.py lines 70–118

    def parse(self, text: str) -> AgentAction | AgentFinish:
        # Check for tool invocation first
        tool_matches = re.findall(r"<tool>(.*?)</tool>", text, re.DOTALL)
        if tool_matches:
            if len(tool_matches) != 1:
                msg = (
                    f"Malformed tool invocation: expected exactly one <tool> block, "
                    f"but found {len(tool_matches)}."
                )
                raise ValueError(msg)
            _tool = tool_matches[0]

            # Match optional tool input
            input_matches = re.findall(
                r"<tool_input>(.*?)</tool_input>", text, re.DOTALL
            )
            if len(input_matches) > 1:
                msg = (
                    f"Malformed tool invocation: expected at most one <tool_input> "
                    f"block, but found {len(input_matches)}."
                )
                raise ValueError(msg)
            _tool_input = input_matches[0] if input_matches else ""

            # Unescape if minimal escape format is used
            if self.escape_format == "minimal":
                _tool = _unescape(_tool)
                _tool_input = _unescape(_tool_input)

            return AgentAction(tool=_tool, tool_input=_tool_input, log=text)
        # Check for final answer
        if "<final_answer>" in text and "</final_answer>" in text:
            matches = re.findall(r"<final_answer>(.*?)</final_answer>", text, re.DOTALL)
            if len(matches) != 1:
                msg = (
                    "Malformed output: expected exactly one "
                    "<final_answer>...</final_answer> block."
                )
                raise ValueError(msg)
            answer = matches[0]
            # Unescape custom delimiters in final answer
            if self.escape_format == "minimal":
                answer = _unescape(answer)
            return AgentFinish(return_values={"output": answer}, log=text)
        msg = (
            "Malformed output: expected either a tool invocation "
            "or a final answer in XML format."
        )
        raise ValueError(msg)

Subdomains

Calls

Frequently Asked Questions

What does parse() do?
parse() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/output_parsers/xml.py.
Where is parse() defined?
parse() is defined in libs/langchain/langchain_classic/agents/output_parsers/xml.py at line 70.
What does parse() call?
parse() calls 1 function(s): _unescape.

Analyze Your Own Codebase

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

Try Supermodel Free