pydantic-xml
pydantic xml extension
What it is and what it does
Pydantic-xml is a pydantic extension that bridges Python dataclass-like models and XML documents. It lets you declare a pydantic model with fields that map to XML attributes (via `attr()`), child elements (via `element()`), or text content, then automatically serialize instances to XML or deserialize XML back into typed Python objects. The binding is declarative and integrates with pydantic's type system, so you get validation, optional fields, unions, generics, and computed fields all working with XML.
The package supports both pydantic v1 and v2, works with standard library `xml.etree.ElementTree` or the faster lxml parser, and handles Python collection types like List, Dict, Set, and Tuple. It does not support pydantic dataclasses or callable discriminators in unions. Most use cases involve APIs or configuration files where XML is the wire format but you want to work with typed Python objects.
Use it for:
- Parse XML configuration files into typed pydantic models with validation, then serialize changes back to XML.
- Consume SOAP or XML-based REST APIs by mapping XML responses to pydantic models automatically.
- Build XML document generators where you define the structure as a pydantic model and serialize instances to XML.
- Migrate legacy XML-based systems to Python by treating XML as a serialization format for modern typed models.
- Validate and transform XML data pipelines using pydantic's rich validation and computed field features.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Pydantic-xml adds XML serialization and deserialization to pydantic models, letting you bind model fields to XML attributes, elements, and text content, then convert between Python objects and XML documents.
Yes. Low install friction, active maintenance, no known vulnerabilities, permissive license, and solid popularity (top 5000 PyPI packages). Install if you need to work with XML in a pydantic-native way; skip only if you have no XML in your project or prefer a different serialization format.
Install
pydantic-xml on PyPI
pip
pip install pydantic-xmluv
uv add pydantic-xmlpoetry
poetry add pydantic-xmlInstalling pydantic-xml
Before you install
Low friction: pure Python wheel with only pydantic and pydantic-core as runtime dependencies. Active maintenance with a release 6 days ago; repository shows steady activity and 255 stars.
License in practice
Licensed under Unlicense (permissive public domain), so you can use, modify, and distribute freely without restriction or attribution requirement.
Quickstart
pip install pydantic-xml
from pydantic_xml import BaseXmlModel, attr, element
from typing import List
class Product(BaseXmlModel):
status: str = attr()
title: str
class Company(BaseXmlModel):
trade_name: str = attr(name='trade-name')
products: List[Product] = element(tag='product')
xml_str = '<Company trade-name="SpaceX"><product status="active">Starship</product></Company>'
company = Company.from_xml(xml_str)
xml_out = company.to_xml()
Verify before relying
- Whether lxml is required or optional; fact sheet lists it as a supported parser but does not clarify if it's a hard dependency.
- Performance characteristics when handling large XML documents or deeply nested structures.
Package facts
| License | Unlicense (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — pydantic, pydantic-core |
| Maintenance | actively maintained — 6 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,507,149/month — #3,035 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pydantic_xml-2.21.1-py3-none-any.whl
Keywords: pydantic, xml, serialization, deserialization, lxml
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
xsdata-pydanticGenerates pydantic models from XML and JSON…
permissive · top 15,000 on PyPI
py-serializableSerializes and deserializes Python classes to…
permissive · top 1,000 on PyPI
xml-pythonConverts XML documents into Python objects,…
copyleft · top 5,000 on PyPI
PyXB-XPyXB-X generates Python classes from XMLSchema…
permissive · top 15,000 on PyPI
et-xmlfileet_xmlfile writes large XML files with minimal…
permissive · top 1,000 on PyPI
py-avro-schemaGenerates Apache Avro schemas from Python…
permissive · top 15,000 on PyPI
maecParse, manipulate, and generate MAEC (Malware…
permissive · top 15,000 on PyPI
pyangbindPyangBind converts YANG data models into Python…
permissive · top 15,000 on PyPI
xsdataxsdata generates Python dataclasses from XML…
permissive · top 5,000 on PyPI
pydantic-scimProvides Pydantic data models for SCIM (System…
unclear · top 15,000 on PyPI