Pydantic Extra Types

2.11.1 · active · verified Sun Mar 29

Pydantic Extra Types extends Pydantic with specialized data types for various common use cases, such as `Color`, `PhoneNumber`, `Country`, `ULID`, and `SemanticVersion`. It is currently at version 2.11.1 and receives frequent updates, often with monthly or bi-monthly patch and minor releases, adding new types and features.

Warnings

Install

Imports

Quickstart

This example demonstrates defining a Pydantic model with a `PhoneNumber` field, which automatically validates the input. Note that `PhoneNumber` requires the `phonenumbers` optional dependency.

from pydantic import BaseModel, ValidationError
from pydantic_extra_types.phone_numbers import PhoneNumber

class Contact(BaseModel):
    name: str
    phone: PhoneNumber

try:
    # Valid phone number
    c = Contact(name='Alice', phone='+1 650-253-0000')
    print(c.phone.e164) # Outputs: +16502530000 (formatted by default as RFC3966 or E164, depending on version/config)

    # Invalid phone number
    Contact(name='Bob', phone='not-a-phone-number')
except ValidationError as e:
    print(f"Validation error: {e}")

view raw JSON →