Skip to main content
Pydantic Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Phone Numbers

The pydantic_extra_types.phone_numbers module provides the PhoneNumber data type.

This class depends on the phonenumbers package, which is a Python port of Google’s libphonenumber.

Bases: str

A wrapper around the phonenumbers.PhoneNumber object.

It provides class-level configuration points you can change by subclassing:

Python
from pydantic import BaseModel
from pydantic_extra_types.phone_numbers import PhoneNumber

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

c = Contact(name='Alice', phone='+1 650-253-0000')
print(c.phone)
# > tel:+1-650-253-0000 (formatted using RFC3966 by default)
Python
from pydantic_extra_types.phone_numbers import PhoneNumber

class USPhone(PhoneNumber):
    default_region_code = 'US'
    supported_regions = ['US']
    phone_format = 'NATIONAL'

# Now parsing will accept national numbers for the US
p = USPhone('650-253-0000')
print(p)
# > 650-253-0000

Changing defaults by using the provided validator annotation:

Section titled “Changing defaults by using the provided validator annotation:”
Python
from typing import Annotated, Union
import phonenumbers
from pydantic import BaseModel
from pydantic_extra_types.phone_numbers import PhoneNumberValidator

E164NumberType = Annotated[Union[str, phonenumbers.PhoneNumber], PhoneNumberValidator(number_format='E164')]

class Model(BaseModel):
    phone: E164NumberType

m = Model(phone='+1 650-253-0000')
print(m.phone)
# > +16502530000

The default region code to use when parsing phone numbers without an international prefix.

Type: str | None Default: None

The supported regions. If empty, all regions are supported.

Type: list[str] Default: []

The format of the phone number.

Type: str Default: 'RFC3966'

An annotation to validate phonenumbers.PhoneNumber objects.

The default region code to use when parsing phone numbers without an international prefix.

If None (the default), the region must be supplied in the phone number as an international prefix.

Type: str | None Default: None

The format of the phone number to return. See phonenumbers.PhoneNumberFormat for valid values.

Type: str Default: 'RFC3966'

The supported regions. If empty (the default), all regions are supported.

Type: Sequence[str] | None Default: None

Was this page helpful?

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu