Home / File/ xml.py — langchain Source File

xml.py — langchain Source File

Architecture documentation for xml.py, a python file in the langchain codebase. 13 imports, 1 dependents.

File python OutputParsing StreamingParsers 13 imports 1 dependents 3 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22["xml.py"]
  69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56["contextlib"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22["xml.py"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> f90cc11a_ee3a_6a74_4781_be7b69a7ed22
  48f0d1a6_a77f_f3fd_8b10_3568d79864d3["xml.etree.ElementTree"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 48f0d1a6_a77f_f3fd_8b10_3568d79864d3
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 91721f45_4909_e489_8c1f_084f8bd87145
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> d758344f_537f_649e_f467_b9d7442e86df
  5d37c56a_542f_e309_4416_d58ad5f08a28["langchain_core.output_parsers.transform"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 5d37c56a_542f_e309_4416_d58ad5f08a28
  81c04601_d095_a27d_4af1_55e771bb2b6b["langchain_core.runnables.utils"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> 81c04601_d095_a27d_4af1_55e771bb2b6b
  e6e1927d_e69c_6e10_6585_ed0fd5f715f5["defusedxml"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> e6e1927d_e69c_6e10_6585_ed0fd5f715f5
  a117e950_d477_a333_1fdd_952fe73219ea["defusedxml.ElementTree"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> a117e950_d477_a333_1fdd_952fe73219ea
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22["xml.py"]
  f90cc11a_ee3a_6a74_4781_be7b69a7ed22 --> f90cc11a_ee3a_6a74_4781_be7b69a7ed22
  style f90cc11a_ee3a_6a74_4781_be7b69a7ed22 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Output parser for XML format."""

import contextlib
import re
import xml
import xml.etree.ElementTree as ET
from collections.abc import AsyncIterator, Iterator
from typing import Any, Literal
from xml.etree.ElementTree import TreeBuilder

from typing_extensions import override

from langchain_core.exceptions import OutputParserException
from langchain_core.messages import BaseMessage
from langchain_core.output_parsers.transform import BaseTransformOutputParser
from langchain_core.runnables.utils import AddableDict

try:
    from defusedxml import ElementTree  # type: ignore[import-untyped]
    from defusedxml.ElementTree import XMLParser  # type: ignore[import-untyped]

    _HAS_DEFUSEDXML = True
except ImportError:
    _HAS_DEFUSEDXML = False

XML_FORMAT_INSTRUCTIONS = """The output should be formatted as a XML file.
1. Output should conform to the tags below.
2. If tags are not given, make them on your own.
3. Remember to always open and close all the tags.

As an example, for the tags ["foo", "bar", "baz"]:
1. String "<foo>\n   <bar>\n      <baz></baz>\n   </bar>\n</foo>" is a well-formatted instance of the schema.
2. String "<foo>\n   <bar>\n   </foo>" is a badly-formatted instance.
3. String "<foo>\n   <tag>\n   </tag>\n</foo>" is a badly-formatted instance.

Here are the output tags:
```
{tags}
```"""  # noqa: E501


class _StreamingParser:
    """Streaming parser for XML.

    This implementation is pulled into a class to avoid implementation drift between
    `transform` and `atransform` of the `XMLOutputParser`.
    """

    def __init__(self, parser: Literal["defusedxml", "xml"]) -> None:
        """Initialize the streaming parser.

        Args:
            parser: Parser to use for XML parsing.

                Can be either `'defusedxml'` or `'xml'`. See documentation in
                `XMLOutputParser` for more information.

        Raises:
            ImportError: If `defusedxml` is not installed and the `defusedxml` parser is
                requested.
// ... (241 more lines)

Domain

Subdomains

Dependencies

  • collections.abc
  • contextlib
  • defusedxml
  • defusedxml.ElementTree
  • langchain_core.exceptions
  • langchain_core.messages
  • langchain_core.output_parsers.transform
  • langchain_core.runnables.utils
  • re
  • typing
  • typing_extensions
  • xml.etree.ElementTree
  • xml.py

Frequently Asked Questions

What does xml.py do?
xml.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StreamingParsers subdomain.
What functions are defined in xml.py?
xml.py defines 3 function(s): _HAS_DEFUSEDXML, defusedxml, nested_element.
What does xml.py depend on?
xml.py imports 13 module(s): collections.abc, contextlib, defusedxml, defusedxml.ElementTree, langchain_core.exceptions, langchain_core.messages, langchain_core.output_parsers.transform, langchain_core.runnables.utils, and 5 more.
What files import xml.py?
xml.py is imported by 1 file(s): xml.py.
Where is xml.py in the architecture?
xml.py is located at libs/core/langchain_core/output_parsers/xml.py (domain: OutputParsing, subdomain: StreamingParsers, directory: libs/core/langchain_core/output_parsers).

Analyze Your Own Codebase

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

Try Supermodel Free