Home / File/ xml.py — langchain Source File

xml.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  b9bf24e2_44fd_b9c4_1e4a_a936f78c742f["xml.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  b9bf24e2_44fd_b9c4_1e4a_a936f78c742f --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"]
  b9bf24e2_44fd_b9c4_1e4a_a936f78c742f --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b
  style b9bf24e2_44fd_b9c4_1e4a_a936f78c742f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Literal

from langchain_core.agents import AgentAction


def _escape(xml: str) -> str:
    """Replace XML tags with custom safe delimiters."""
    replacements = {
        "<tool>": "[[tool]]",
        "</tool>": "[[/tool]]",
        "<tool_input>": "[[tool_input]]",
        "</tool_input>": "[[/tool_input]]",
        "<observation>": "[[observation]]",
        "</observation>": "[[/observation]]",
    }
    for orig, repl in replacements.items():
        xml = xml.replace(orig, repl)
    return xml


def format_xml(
    intermediate_steps: list[tuple[AgentAction, str]],
    *,
    escape_format: Literal["minimal"] | None = "minimal",
) -> str:
    """Format the intermediate steps as XML.

    Args:
        intermediate_steps: The intermediate steps.
        escape_format: The escaping format to use. Currently only 'minimal' is
            supported, which replaces XML tags with custom delimiters to prevent
            conflicts.

    Returns:
        The intermediate steps as XML.
    """
    log = ""
    for action, observation in intermediate_steps:
        if escape_format == "minimal":
            # Escape XML tags in tool names and inputs using custom delimiters
            tool = _escape(action.tool)
            tool_input = _escape(str(action.tool_input))
            observation_ = _escape(str(observation))
        else:
            tool = action.tool
            tool_input = str(action.tool_input)
            observation_ = str(observation)
        log += (
            f"<tool>{tool}</tool><tool_input>{tool_input}"
            f"</tool_input><observation>{observation_}</observation>"
        )
    return log

Subdomains

Dependencies

  • langchain_core.agents
  • typing

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, ToolInterface subdomain.
What functions are defined in xml.py?
xml.py defines 2 function(s): _escape, format_xml.
What does xml.py depend on?
xml.py imports 2 module(s): langchain_core.agents, typing.
Where is xml.py in the architecture?
xml.py is located at libs/langchain/langchain_classic/agents/format_scratchpad/xml.py (domain: AgentOrchestration, subdomain: ToolInterface, directory: libs/langchain/langchain_classic/agents/format_scratchpad).

Analyze Your Own Codebase

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

Try Supermodel Free