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
  698aa558_20db_aa12_47eb_de459e25e934["parse()"]
  b51fd3dc_a25e_9ad9_8893_3725f5d436f0["XMLOutputParser"]
  698aa558_20db_aa12_47eb_de459e25e934 -->|defined in| b51fd3dc_a25e_9ad9_8893_3725f5d436f0
  e9426db7_ce58_59db_1f0b_befb5eb1d7ae["parse()"]
  e9426db7_ce58_59db_1f0b_befb5eb1d7ae -->|calls| 698aa558_20db_aa12_47eb_de459e25e934
  bc7e3795_e9e2_9111_e066_976d85188623["_root_to_dict()"]
  698aa558_20db_aa12_47eb_de459e25e934 -->|calls| bc7e3795_e9e2_9111_e066_976d85188623
  e9426db7_ce58_59db_1f0b_befb5eb1d7ae["parse()"]
  698aa558_20db_aa12_47eb_de459e25e934 -->|calls| e9426db7_ce58_59db_1f0b_befb5eb1d7ae
  style 698aa558_20db_aa12_47eb_de459e25e934 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/output_parsers/xml.py lines 206–250

    def parse(self, text: str) -> dict[str, str | list[Any]]:
        """Parse the output of an LLM call.

        Args:
            text: The output of an LLM call.

        Returns:
            A `dict` representing the parsed XML.

        Raises:
            OutputParserException: If the XML is not well-formed.
            ImportError: If defus`edxml is not installed and the `defusedxml` parser is
                requested.
        """
        # Try to find XML string within triple backticks
        # Imports are temporarily placed here to avoid issue with caching on CI
        # likely if you're reading this you can move them to the top of the file
        if self.parser == "defusedxml":
            if not _HAS_DEFUSEDXML:
                msg = (
                    "defusedxml is not installed. "
                    "Please install it to use the defusedxml parser."
                    "You can install it with `pip install defusedxml`"
                    "See https://github.com/tiran/defusedxml for more details"
                )
                raise ImportError(msg)
            et = ElementTree  # Use the defusedxml parser
        else:
            et = ET  # Use the standard library parser

        match = re.search(r"```(xml)?(.*)```", text, re.DOTALL)
        if match is not None:
            # If match found, use the content within the backticks
            text = match.group(2)
        encoding_match = self.encoding_matcher.search(text)
        if encoding_match:
            text = encoding_match.group(2)

        text = text.strip()
        try:
            root = et.fromstring(text)
            return self._root_to_dict(root)
        except et.ParseError as e:
            msg = f"Failed to parse XML format from completion {text}. Got: {e}"
            raise OutputParserException(msg, llm_output=text) from e

Domain

Subdomains

Called By

Frequently Asked Questions

What does parse() do?
parse() is a function in the langchain codebase, defined in libs/core/langchain_core/output_parsers/xml.py.
Where is parse() defined?
parse() is defined in libs/core/langchain_core/output_parsers/xml.py at line 206.
What does parse() call?
parse() calls 2 function(s): _root_to_dict, parse.
What calls parse()?
parse() is called by 1 function(s): parse.

Analyze Your Own Codebase

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

Try Supermodel Free