Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[patch]: fix xml output parser transform #19530

Merged
merged 6 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 0 additions & 111 deletions libs/core/langchain_core/output_parsers/xml.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import re
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional, Union

Check failure on line 2 in libs/core/langchain_core/output_parsers/xml.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (F401)

langchain_core/output_parsers/xml.py:2:25: F401 `typing.AsyncIterator` imported but unused

Check failure on line 2 in libs/core/langchain_core/output_parsers/xml.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (F401)

langchain_core/output_parsers/xml.py:2:46: F401 `typing.Iterator` imported but unused

Check failure on line 2 in libs/core/langchain_core/output_parsers/xml.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (F401)

langchain_core/output_parsers/xml.py:2:72: F401 `typing.Union` imported but unused
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import TreeBuilder

from langchain_core.exceptions import OutputParserException
from langchain_core.messages import BaseMessage

Check failure on line 6 in libs/core/langchain_core/output_parsers/xml.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (F401)

langchain_core/output_parsers/xml.py:6:37: F401 `langchain_core.messages.BaseMessage` imported but unused
from langchain_core.output_parsers.transform import BaseTransformOutputParser
from langchain_core.runnables.utils import AddableDict

Expand Down Expand Up @@ -58,116 +57,6 @@
msg = f"Failed to parse XML format from completion {text}. Got: {e}"
raise OutputParserException(msg, llm_output=text) from e

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())
)
xml_start_re = re.compile(r"<[a-zA-Z:_]")
xml_started = False
current_path: List[str] = []
current_path_has_children = False
buffer = ""
for chunk in input:
if isinstance(chunk, BaseMessage):
# extract text
chunk_content = chunk.content
if not isinstance(chunk_content, str):
continue
chunk = chunk_content
# add chunk to buffer of unprocessed text
buffer += chunk
# if xml string hasn't started yet, continue to next chunk
if not xml_started:
if match := xml_start_re.search(buffer):
# if xml string has started, remove all text before it
buffer = buffer[match.start() :]
xml_started = True
else:
continue
# feed buffer to parser
parser.feed(buffer)
buffer = ""
# yield all events

for event, elem in parser.read_events():
if event == "start":
# update current path
current_path.append(elem.tag)
current_path_has_children = False
elif event == "end":
# remove last element from current path
current_path.pop()
# yield element
if not current_path_has_children:
yield nested_element(current_path, elem)
# prevent yielding of parent element
if current_path:
current_path_has_children = True
else:
xml_started = False
# close parser
parser.close()

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)
xml_start_re = re.compile(r"<[a-zA-Z:_]")
xml_started = False
current_path: List[str] = []
current_path_has_children = False
buffer = ""
async for chunk in input:
if isinstance(chunk, BaseMessage):
# extract text
chunk_content = chunk.content
if not isinstance(chunk_content, str):
continue
chunk = chunk_content
# add chunk to buffer of unprocessed text
buffer += chunk
# if xml string hasn't started yet, continue to next chunk
if not xml_started:
if match := xml_start_re.search(buffer):
# if xml string has started, remove all text before it
buffer = buffer[match.start() :]
xml_started = True
else:
continue
# feed buffer to parser
parser.feed(buffer)
buffer = ""
# yield all events
for event, elem in parser.read_events():
if event == "start":
# update current path
current_path.append(elem.tag)
current_path_has_children = False
elif event == "end":
# remove last element from current path
current_path.pop()
# yield element
if not current_path_has_children:
yield nested_element(current_path, elem)
# prevent yielding of parent element
if current_path:
current_path_has_children = True
else:
xml_started = False
# close parser
parser.close()

def _root_to_dict(self, root: ET.Element) -> Dict[str, List[Any]]:
"""Converts xml tree to python dictionary."""
result: Dict[str, List[Any]] = {root.tag: []}
Expand Down
30 changes: 0 additions & 30 deletions libs/core/tests/unit_tests/output_parsers/test_xml_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test XMLOutputParser"""
from typing import AsyncIterator

Check failure on line 2 in libs/core/tests/unit_tests/output_parsers/test_xml_parser.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (F401)

tests/unit_tests/output_parsers/test_xml_parser.py:2:20: F401 `typing.AsyncIterator` imported but unused
from xml.etree.ElementTree import ParseError

Check failure on line 3 in libs/core/tests/unit_tests/output_parsers/test_xml_parser.py

View workflow job for this annotation

GitHub Actions / cd libs/core / make lint #3.11

Ruff (F401)

tests/unit_tests/output_parsers/test_xml_parser.py:3:35: F401 `xml.etree.ElementTree.ParseError` imported but unused

import pytest

Expand Down Expand Up @@ -49,22 +49,6 @@
xml_parser = XMLOutputParser()
assert DEF_RESULT_EXPECTED == xml_parser.parse(result)
assert DEF_RESULT_EXPECTED == (await xml_parser.aparse(result))
assert list(xml_parser.transform(iter(result))) == [
{"foo": [{"bar": [{"baz": None}]}]},
{"foo": [{"bar": [{"baz": "slim.shady"}]}]},
{"foo": [{"baz": "tag"}]},
]

async def _as_iter(string: str) -> AsyncIterator[str]:
for c in string:
yield c

chunks = [chunk async for chunk in xml_parser.atransform(_as_iter(result))]
assert chunks == [
{"foo": [{"bar": [{"baz": None}]}]},
{"foo": [{"bar": [{"baz": "slim.shady"}]}]},
{"foo": [{"baz": "tag"}]},
]


@pytest.mark.parametrize("result", ["foo></foo>", "<foo></foo", "foo></foo", "foofoo"])
Expand Down Expand Up @@ -100,17 +84,3 @@

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}]}]}]
Loading