--- id: pydantic-xml version: "2.21.1" license: Unlicense license_treatment: permissive maintenance: active --- # pydantic-xml — pydantic xml extension License: permissive · Maintenance: active · Downloads: 2.5M/mo ## 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 above — 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 pip install pydantic-xml uv add pydantic-xml 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 = 'Starship' 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_current - Install friction: low - Maintenance: active - Downloads: 2.5M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags pydantic xml serialization, xml to pydantic model, xml deserialization python, pydantic xml binding, xml element attribute mapping, convert xml to python objects, xml-serialization, pydantic-extension [View on SkillFed](https://skillfed.io/packages/pydantic-xml) · [View on PyPI](https://pypi.org/project/pydantic-xml/)