Skip to content

pydantic_ai.format_prompt

format_as_xml

def format_as_xml(
    obj: Any,
    root_tag: str | None = None,
    item_tag: str = 'item',
    none_str: str = 'null',
    indent: str | None = '  ',
    include_field_info: Literal['once'] | bool = False,
) -> str

Format a Python object as XML.

This is useful since LLMs often find it easier to read semi-structured data (e.g. examples) as XML, rather than JSON etc.

Supports: str, bytes, bytearray, bool, int, float, Decimal, date, datetime, time, timedelta, UUID, Enum, Mapping, Iterable, dataclass, and BaseModel.

Mapping keys are used as element names when they are valid XML names without a colon (an XML NCName, e.g. name or first_name). Any other key, like 'my key', '1abc' or an int, is instead written to the key attribute of an element named after item_tag, e.g. <item key="my key">1</item>, so it is escaped like any other attribute value and can’t inject tags or break the element structure.

Example:

format_as_xml_example.py
from pydantic_ai import format_as_xml

print(format_as_xml({'name': 'John', 'height': 6, 'weight': 200}, root_tag='user'))
'''
<user>
  <name>John</name>
  <height>6</height>
  <weight>200</weight>
</user>
'''

Returns

str — XML representation of the object.

Parameters

obj : Any

Python Object to serialize to XML.

root_tag : str | None Default: None

Outer tag to wrap the XML in, use None to omit the outer tag.

item_tag : str Default: 'item'

Tag to use for each item in an iterable (e.g. list), this is overridden by the class name for dataclasses and Pydantic models. It’s also used for mapping entries whose key isn’t a valid XML element name, see above.

none_str : str Default: 'null'

String to use for None values.

indent : str | None Default: ' '

Indentation string to use for pretty printing.

include_field_info : Literal[‘once’] | bool Default: False

Whether to include attributes like Pydantic Field attributes and dataclasses field() metadata as XML attributes. In both cases the allowed Field attributes and field() metadata keys are title and description. If a field is repeated in the data (e.g. in a list) by setting once the attributes are included only in the first occurrence of an XML element relative to the same field.