skillfed

pydantic-xml

pydantic xml extension

pydantic-xml v2.21.1 2.5M downloads/30d#3,035 on PyPI255
Permissive license Unlicense Active released

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-xml

uv

uv add pydantic-xml

poetry

poetry add pydantic-xml

Installing 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

Development Status :: 5 - Production/StableFramework :: PydanticFramework :: Pydantic :: 1Framework :: Pydantic :: 2Intended Audience :: DevelopersLicense :: OSI ApprovedLicense :: Public DomainNatural Language :: EnglishOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Software Development :: LibrariesTopic :: Text Processing :: Markup :: XMLTyping :: Typed

Tags

pydantic xml serializationxml to pydantic modelxml deserialization pythonpydantic xml bindingxml element attribute mappingconvert xml to python objects
xml-serializationpydantic-extension

More Libraries packages