mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-20 05:43:55 +00:00
core[patch]: Fix defusedxml import (#26718)
Fix defusedxml import. Haven't investigated what's actually going on under the hood -- defusedxml probably does some weird things in __init__
This commit is contained in:
parent
79b224f6f3
commit
4fc69d61ad
@ -189,7 +189,7 @@ class XMLOutputParser(BaseTransformOutputParser):
|
|||||||
# likely if you're reading this you can move them to the top of the file
|
# likely if you're reading this you can move them to the top of the file
|
||||||
if self.parser == "defusedxml":
|
if self.parser == "defusedxml":
|
||||||
try:
|
try:
|
||||||
import defusedxml # type: ignore
|
from defusedxml import ElementTree # type: ignore
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
raise ImportError(
|
raise ImportError(
|
||||||
"defusedxml is not installed. "
|
"defusedxml is not installed. "
|
||||||
@ -197,7 +197,7 @@ class XMLOutputParser(BaseTransformOutputParser):
|
|||||||
"You can install it with `pip install defusedxml`"
|
"You can install it with `pip install defusedxml`"
|
||||||
"See https://github.com/tiran/defusedxml for more details"
|
"See https://github.com/tiran/defusedxml for more details"
|
||||||
) from e
|
) from e
|
||||||
_et = defusedxml.ElementTree # Use the defusedxml parser
|
_et = ElementTree # Use the defusedxml parser
|
||||||
else:
|
else:
|
||||||
_et = ET # Use the standard library parser
|
_et = ET # Use the standard library parser
|
||||||
|
|
||||||
@ -211,10 +211,9 @@ class XMLOutputParser(BaseTransformOutputParser):
|
|||||||
|
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
try:
|
try:
|
||||||
root = ET.fromstring(text)
|
root = _et.fromstring(text)
|
||||||
return self._root_to_dict(root)
|
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}"
|
msg = f"Failed to parse XML format from completion {text}. Got: {e}"
|
||||||
raise OutputParserException(msg, llm_output=text) from e
|
raise OutputParserException(msg, llm_output=text) from e
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user