3
3
from collections .abc import Iterable , Iterator , Mapping
4
4
from dataclasses import asdict , dataclass , field , fields , is_dataclass
5
5
from datetime import date
6
+ from enum import Enum
6
7
from typing import Any , Literal
7
8
from xml .etree import ElementTree
8
9
@@ -26,8 +27,8 @@ def format_as_xml(
26
27
This is useful since LLMs often find it easier to read semi-structured data (e.g. examples) as XML,
27
28
rather than JSON etc.
28
29
29
- Supports: `str`, `bytes`, `bytearray`, `bool`, `int`, `float`, `date`, `datetime`, `Mapping `,
30
- `Iterable`, `dataclass`, and `BaseModel`.
30
+ Supports: `str`, `bytes`, `bytearray`, `bool`, `int`, `float`, `date`, `datetime`, `Enum `,
31
+ `Mapping`, ` Iterable`, `dataclass`, and `BaseModel`.
31
32
32
33
Args:
33
34
obj: Python Object to serialize to XML.
@@ -101,7 +102,7 @@ def _to_xml(self, value: Any, path: str, tag: str | None = None) -> ElementTree.
101
102
element .text = value
102
103
elif isinstance (value , bytes | bytearray ):
103
104
element .text = value .decode (errors = 'ignore' )
104
- elif isinstance (value , bool | int | float ):
105
+ elif isinstance (value , bool | int | float | Enum ):
105
106
element .text = str (value )
106
107
elif isinstance (value , date ):
107
108
element .text = value .isoformat ()
0 commit comments