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 defusedxml import #26718

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Changes from all 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
9 changes: 4 additions & 5 deletions libs/core/langchain_core/output_parsers/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ def parse(self, text: str) -> dict[str, Union[str, list[Any]]]:
# likely if you're reading this you can move them to the top of the file
if self.parser == "defusedxml":
try:
import defusedxml # type: ignore
from defusedxml import ElementTree # type: ignore
except ImportError as e:
raise ImportError(
"defusedxml is not installed. "
"Please install it to use the defusedxml parser."
"You can install it with `pip install defusedxml`"
"See https://github.com/tiran/defusedxml for more details"
) from e
_et = defusedxml.ElementTree # Use the defusedxml parser
_et = ElementTree # Use the defusedxml parser
else:
_et = ET # Use the standard library parser

Expand All @@ -211,10 +211,9 @@ def parse(self, text: str) -> dict[str, Union[str, list[Any]]]:

text = text.strip()
try:
root = ET.fromstring(text)
root = _et.fromstring(text)
return self._root_to_dict(root)

except ET.ParseError as e:
except _et.ParseError as e:
msg = f"Failed to parse XML format from completion {text}. Got: {e}"
raise OutputParserException(msg, llm_output=text) from e

Expand Down
Loading