Home / File/ test_xml.py — langchain Source File

test_xml.py — langchain Source File

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

File python LangChainCore ApiManagement 3 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  972f46cf_6d67_3d50_b55a_eec185ef4348["test_xml.py"]
  59e0d3b0_0f8e_4b79_d442_e9b4821561c7["langchain_core.agents"]
  972f46cf_6d67_3d50_b55a_eec185ef4348 --> 59e0d3b0_0f8e_4b79_d442_e9b4821561c7
  d2a4b55f_58f4_c7e6_e45c_0db7de51a64c["langchain_classic.agents.output_parsers.xml"]
  972f46cf_6d67_3d50_b55a_eec185ef4348 --> d2a4b55f_58f4_c7e6_e45c_0db7de51a64c
  e535c700_de11_1533_ee31_c11dc9bf51b7["langchain_classic.agents.format_scratchpad.xml"]
  972f46cf_6d67_3d50_b55a_eec185ef4348 --> e535c700_de11_1533_ee31_c11dc9bf51b7
  style 972f46cf_6d67_3d50_b55a_eec185ef4348 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from langchain_core.agents import AgentAction, AgentFinish

from langchain_classic.agents.output_parsers.xml import XMLAgentOutputParser


def test_tool_usage() -> None:
    parser = XMLAgentOutputParser()
    # Test when final closing </tool_input> is included
    _input = """<tool>search</tool><tool_input>foo</tool_input>"""
    output = parser.invoke(_input)
    expected_output = AgentAction(tool="search", tool_input="foo", log=_input)
    assert output == expected_output
    # Test when final closing </tool_input> is NOT included
    # This happens when it's used as a stop token
    _input = """<tool>search</tool><tool_input>foo</tool_input>"""
    output = parser.invoke(_input)
    expected_output = AgentAction(tool="search", tool_input="foo", log=_input)
    assert output == expected_output


def test_finish() -> None:
    parser = XMLAgentOutputParser()
    # Test when final closing <final_answer> is included
    _input = """<final_answer>bar</final_answer>"""
    output = parser.invoke(_input)
    expected_output = AgentFinish(return_values={"output": "bar"}, log=_input)
    assert output == expected_output

    # Test when final closing <final_answer> is NOT included
    # This happens when it's used as a stop token
    _input = """<final_answer>bar</final_answer>"""
    output = parser.invoke(_input)
    expected_output = AgentFinish(return_values={"output": "bar"}, log=_input)
    assert output == expected_output


def test_malformed_xml_with_nested_tags() -> None:
    """Test handling of tool names with XML tags via format_xml minimal escaping."""
    from langchain_classic.agents.format_scratchpad.xml import format_xml

    # Create an AgentAction with XML tags in the tool name
    action = AgentAction(tool="search<tool>nested</tool>", tool_input="query", log="")

    # The format_xml function should escape the XML tags using custom delimiters
    formatted_xml = format_xml([(action, "observation")])

    # Extract just the tool part for parsing
    tool_part = formatted_xml.split("<observation>")[0]  # Remove observation part

    # Now test that the parser can handle the escaped XML
    parser = XMLAgentOutputParser(escape_format="minimal")
    output = parser.invoke(tool_part)

    # The parser should unescape and extract the original tool name
    expected_output = AgentAction(
        tool="search<tool>nested</tool>", tool_input="query", log=tool_part
    )
    assert output == expected_output


def test_no_escaping() -> None:
    """Test parser with escaping disabled."""
    parser = XMLAgentOutputParser(escape_format=None)

    # Test with regular tool name (no XML tags)
    _input = """<tool>search</tool><tool_input>foo</tool_input>"""
    output = parser.invoke(_input)
    expected_output = AgentAction(tool="search", tool_input="foo", log=_input)
    assert output == expected_output

Domain

Subdomains

Dependencies

  • langchain_classic.agents.format_scratchpad.xml
  • langchain_classic.agents.output_parsers.xml
  • langchain_core.agents

Frequently Asked Questions

What does test_xml.py do?
test_xml.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, ApiManagement subdomain.
What functions are defined in test_xml.py?
test_xml.py defines 4 function(s): test_finish, test_malformed_xml_with_nested_tags, test_no_escaping, test_tool_usage.
What does test_xml.py depend on?
test_xml.py imports 3 module(s): langchain_classic.agents.format_scratchpad.xml, langchain_classic.agents.output_parsers.xml, langchain_core.agents.
Where is test_xml.py in the architecture?
test_xml.py is located at libs/langchain/tests/unit_tests/agents/output_parsers/test_xml.py (domain: LangChainCore, subdomain: ApiManagement, directory: libs/langchain/tests/unit_tests/agents/output_parsers).

Analyze Your Own Codebase

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

Try Supermodel Free