The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
core[patch]: fix xml output parser transform (#19530)
Eugene Yurtsev56f4c5459b6b
diff --git a/libs/core/langchain_core/output_parsers/xml.py b/libs/core/langchain_core/output_parsers/xml.pyindex 9871ecee91..5a89c5f763 100644--- a/libs/core/langchain_core/output_parsers/xml.py+++ b/libs/core/langchain_core/output_parsers/xml.py@@ -1,7 +1,6 @@import refrom typing import Any, AsyncIterator, Dict, Iterator, List, Optional, Unionfrom xml.etree import ElementTree as ET-from xml.etree.ElementTree import TreeBuilderfrom langchain_core.exceptions import OutputParserExceptionfrom langchain_core.messages import BaseMessage@@ -61,13 +60,7 @@ class XMLOutputParser(BaseTransformOutputParser):def _transform(self, input: Iterator[Union[str, BaseMessage]]) -> Iterator[AddableDict]:- # Imports are temporarily placed here to avoid issue with caching on CI- # likely if you're reading this you can move them to the top of the file- from defusedxml.ElementTree import DefusedXMLParser # type: ignore[import]-- parser = ET.XMLPullParser(- ["start", "end"], _parser=DefusedXMLParser(target=TreeBuilder())- )+ parser = ET.XMLPullParser(["start", "end"])xml_start_re = re.compile(r"<[a-zA-Z:_]")xml_started = Falsecurrent_path: List[str] = []@@ -117,12 +110,7 @@ class XMLOutputParser(BaseTransformOutputParser):async def _atransform(self, input: AsyncIterator[Union[str, BaseMessage]]) -> AsyncIterator[AddableDict]:- # Imports are temporarily placed here to avoid issue with caching on CI- # likely if you're reading this you can move them to the top of the file- from defusedxml.ElementTree import DefusedXMLParser # type: ignore[import]-- _parser = DefusedXMLParser(target=TreeBuilder())- parser = ET.XMLPullParser(["start", "end"], _parser=_parser)+ parser = ET.XMLPullParser(["start", "end"])xml_start_re = re.compile(r"<[a-zA-Z:_]")xml_started = Falsecurrent_path: List[str] = []diff --git a/libs/core/tests/unit_tests/output_parsers/test_xml_parser.py b/libs/core/tests/unit_tests/output_parsers/test_xml_parser.pyindex 17ef1a558e..7ba68f42a4 100644--- a/libs/core/tests/unit_tests/output_parsers/test_xml_parser.py+++ b/libs/core/tests/unit_tests/output_parsers/test_xml_parser.py@@ -1,6 +1,5 @@"""Test XMLOutputParser"""from typing import AsyncIterator-from xml.etree.ElementTree import ParseErrorimport pytest@@ -100,17 +99,3 @@ async def tests_billion_laughs_attack() -> None:with pytest.raises(OutputParserException):await parser.aparse(MALICIOUS_XML)-- with pytest.raises(ParseError):- # Right now raises undefined entity error- assert list(parser.transform(iter(MALICIOUS_XML))) == [- {"foo": [{"bar": [{"baz": None}]}]}- ]-- async def _as_iter(string: str) -> AsyncIterator[str]:- for c in string:- yield c-- with pytest.raises(ParseError):- chunks = [chunk async for chunk in parser.atransform(_as_iter(MALICIOUS_XML))]- assert chunks == [{"foo": [{"bar": [{"baz": None}]}]}]