XMLAgentOutputParser Class — langchain Architecture
Architecture documentation for the XMLAgentOutputParser class in xml.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 647b2f55_255b_beae_316f_269d2e0e4bb5["XMLAgentOutputParser"] d9685a4b_26b0_bca9_f857_03bb2ffc9dd4["AgentOutputParser"] 647b2f55_255b_beae_316f_269d2e0e4bb5 -->|extends| d9685a4b_26b0_bca9_f857_03bb2ffc9dd4 f2ea7756_ae03_24f7_4d97_422e78f632d9["xml.py"] 647b2f55_255b_beae_316f_269d2e0e4bb5 -->|defined in| f2ea7756_ae03_24f7_4d97_422e78f632d9 e386861e_a7a7_9278_87ae_0d850022a200["parse()"] 647b2f55_255b_beae_316f_269d2e0e4bb5 -->|method| e386861e_a7a7_9278_87ae_0d850022a200 36b2a200_78a8_1292_4aa1_6a0fa119ef2c["get_format_instructions()"] 647b2f55_255b_beae_316f_269d2e0e4bb5 -->|method| 36b2a200_78a8_1292_4aa1_6a0fa119ef2c c80ae0c3_91e5_9078_7dad_70a1d86186f2["_type()"] 647b2f55_255b_beae_316f_269d2e0e4bb5 -->|method| c80ae0c3_91e5_9078_7dad_70a1d86186f2
Relationship Graph
Source Code
libs/langchain/langchain_classic/agents/output_parsers/xml.py lines 26–126
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")
"""The format to use for escaping XML characters.
minimal - uses custom delimiters to replace XML tags within content,
preventing parsing conflicts. This is the only supported format currently.
None - no escaping is applied, which may lead to parsing conflicts.
"""
@override
def parse(self, text: str) -> AgentAction | AgentFinish:
# Check for tool invocation first
tool_matches = re.findall(r"<tool>(.*?)</tool>", text, re.DOTALL)
if tool_matches:
if len(tool_matches) != 1:
msg = (
f"Malformed tool invocation: expected exactly one <tool> block, "
f"but found {len(tool_matches)}."
)
raise ValueError(msg)
_tool = tool_matches[0]
# Match optional tool input
input_matches = re.findall(
r"<tool_input>(.*?)</tool_input>", text, re.DOTALL
)
if len(input_matches) > 1:
msg = (
f"Malformed tool invocation: expected at most one <tool_input> "
f"block, but found {len(input_matches)}."
)
raise ValueError(msg)
_tool_input = input_matches[0] if input_matches else ""
# Unescape if minimal escape format is used
if self.escape_format == "minimal":
_tool = _unescape(_tool)
_tool_input = _unescape(_tool_input)
return AgentAction(tool=_tool, tool_input=_tool_input, log=text)
# Check for final answer
if "<final_answer>" in text and "</final_answer>" in text:
matches = re.findall(r"<final_answer>(.*?)</final_answer>", text, re.DOTALL)
if len(matches) != 1:
msg = (
"Malformed output: expected exactly one "
"<final_answer>...</final_answer> block."
Extends
Source
Frequently Asked Questions
What is the XMLAgentOutputParser class?
XMLAgentOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/output_parsers/xml.py.
Where is XMLAgentOutputParser defined?
XMLAgentOutputParser is defined in libs/langchain/langchain_classic/agents/output_parsers/xml.py at line 26.
What does XMLAgentOutputParser extend?
XMLAgentOutputParser extends AgentOutputParser.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free