Home / File/ xml.py — langchain Source File

xml.py — langchain Source File

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

File python AgentOrchestration ActionLogic 6 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  bfb402fc_eefe_a5f3_61eb_f705b8cd26d1["xml.py"]
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  bfb402fc_eefe_a5f3_61eb_f705b8cd26d1 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  bfb402fc_eefe_a5f3_61eb_f705b8cd26d1 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"]
  bfb402fc_eefe_a5f3_61eb_f705b8cd26d1 --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  bfb402fc_eefe_a5f3_61eb_f705b8cd26d1 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  bfb402fc_eefe_a5f3_61eb_f705b8cd26d1 --> 91721f45_4909_e489_8c1f_084f8bd87145
  37814a67_ff56_3f91_8aa4_794e5ef7a906["langchain_classic.agents"]
  bfb402fc_eefe_a5f3_61eb_f705b8cd26d1 --> 37814a67_ff56_3f91_8aa4_794e5ef7a906
  style bfb402fc_eefe_a5f3_61eb_f705b8cd26d1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import re
from typing import Literal

from langchain_core.agents import AgentAction, AgentFinish
from pydantic import Field
from typing_extensions import override

from langchain_classic.agents import AgentOutputParser


def _unescape(text: str) -> str:
    """Convert custom tag delimiters back into XML tags."""
    replacements = {
        "[[tool]]": "<tool>",
        "[[/tool]]": "</tool>",
        "[[tool_input]]": "<tool_input>",
        "[[/tool_input]]": "</tool_input>",
        "[[observation]]": "<observation>",
        "[[/observation]]": "</observation>",
    }
    for repl, orig in replacements.items():
        text = text.replace(repl, orig)
    return text


class XMLAgentOutputParser(AgentOutputParser):
    """Parses tool invocations and final answers from XML-formatted agent output.

    This parser extracts structured information from XML tags to determine whether
    an agent should perform a tool action or provide a final answer. It includes
    built-in escaping support to safely handle tool names and inputs
    containing XML special characters.

    Args:
        escape_format: The escaping format to use when parsing XML content.
            Supports 'minimal' which uses custom delimiters like [[tool]] to replace
            XML tags within content, preventing parsing conflicts.
            Use 'minimal' if using a corresponding encoding format that uses
            the _escape function when formatting the output (e.g., with format_xml).

    Expected formats:
        Tool invocation (returns AgentAction):
            <tool>search</tool>
            <tool_input>what is 2 + 2</tool_input>

        Final answer (returns AgentFinish):
            <final_answer>The answer is 4</final_answer>

    !!! note
        Minimal escaping allows tool names containing XML tags to be safely represented.
        For example, a tool named `search<tool>nested</tool>` would be escaped as
        `search[[tool]]nested[[/tool]]` in the XML and automatically unescaped during
        parsing.

    Raises:
        ValueError: If the input doesn't match either expected XML format or
            contains malformed XML structure.
    """

    escape_format: Literal["minimal"] | None = Field(default="minimal")
// ... (67 more lines)

Subdomains

Functions

Dependencies

  • langchain_classic.agents
  • langchain_core.agents
  • pydantic
  • re
  • typing
  • typing_extensions

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 AgentOrchestration domain, ActionLogic subdomain.
What functions are defined in xml.py?
xml.py defines 1 function(s): _unescape.
What does xml.py depend on?
xml.py imports 6 module(s): langchain_classic.agents, langchain_core.agents, pydantic, re, typing, typing_extensions.
Where is xml.py in the architecture?
xml.py is located at libs/langchain/langchain_classic/agents/output_parsers/xml.py (domain: AgentOrchestration, subdomain: ActionLogic, directory: libs/langchain/langchain_classic/agents/output_parsers).

Analyze Your Own Codebase

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

Try Supermodel Free