bug(core): XMLOutputParser loses parents and siblings after self-closing tags
Description
XMLOutputParser loses parent and sibling elements when valid XML contains a self-closing tag. For example, the nested document below returns only { c: "y" }. Replacing <a/> with <a></a> preserves the full tree.
Example Code
import { XMLOutputParser } from "@langchain/core/output_parsers";
const parser = new XMLOutputParser();
const result = await parser.parse(
"<root><group><a/><b>x</b></group><c>y</c></root>"
);
console.dir(result, { depth: null });Actual:
{ c: "y" }Expected:
{ root: [{ group: [{ a: "" }, { b: "x" }] }, { c: "y" }] }There is no exception: the result silently loses data. The same problem occurs with Markdown-wrapped XML and streamed output.
Cause
The bundled SAX parser emits both an open event and a close event for a self-closing element. parseXMLMarkdown() skips pushing that element onto its stack, so its close event pops the parent instead. Later elements can then replace the root result.
Checked other resources
Searched GitHub issues and open PRs for XMLOutputParser and XML self-closing tags. The CDATA report #6666 concerns a different SAX event and is unrelated to this stack imbalance.
System Info
- Reproduced on main at
5d509ad4b8a5ed9dd4b83020b4c103ac8a69859c(@langchain/corepackage version1.2.13). - Node.js
24.18.0, pnpm10.14.0, macOS. - No model provider, credentials, or network requests are needed.
Source: langchain-ai/langchainjs