Skip to main content
Beyond basic types like str, int, and float, FastAPI supports many specialized data types from Python’s standard library and Pydantic.

Common Extra Data Types

FastAPI/Pydantic support these additional types with automatic validation:
  • UUID: Universal Unique Identifier
  • datetime: Date and time
  • date: Date only
  • time: Time only
  • timedelta: Time duration
  • frozenset: Immutable set
  • bytes: Binary data
  • Decimal: Precise decimal numbers

Complete Example

All these types are automatically:
  • Validated from the request
  • Serialized in the response
  • Documented in OpenAPI

UUID Type

UUIDs are commonly used as unique identifiers:
UUIDs are represented as strings in JSON but validated as proper UUID format.

DateTime Types

datetime - Date and Time

Example JSON:

date - Date Only

Example JSON:

time - Time Only

Example JSON:

timedelta - Duration

Example JSON:
timedelta is represented as seconds (float) in JSON.

Numeric Types

Decimal - Precise Numbers

Use Decimal for financial calculations where precision matters:
Use Decimal instead of float for money to avoid floating-point precision errors.

Binary Data Types

bytes - Binary Data

bytes are represented as base64-encoded strings in JSON.

Collection Types

frozenset - Immutable Set

Example JSON:
frozenset ensures unique values and is immutable, but is represented as a list in JSON.

URL Types

HttpUrl - URL Validation

This validates that the URL:
  • Has a valid scheme (http/https)
  • Has a valid domain
  • Is properly formatted

Email and Other String Types

EmailStr - Email Validation

EmailStr requires the email-validator package:

Path and File Types

FilePath and DirectoryPath

Color Type

Accepts formats:
  • "red"
  • "#ff0000"
  • "rgb(255, 0, 0)"
  • "rgba(255, 0, 0, 0.5)"

All Supported Types Summary

1

Standard Python

UUID, datetime, date, time, timedelta, Decimal, bytes, frozenset
2

Pydantic Types

EmailStr, HttpUrl, FilePath, DirectoryPath, ColorStr, Json
3

Network Types

IPvAnyAddress, IPvAnyInterface, IPvAnyNetwork
4

Secret Types

SecretStr, SecretBytes (not logged/dumped by default)

Type Conversion and Validation

All these types provide:
  1. Automatic Conversion: String → Type
  2. Validation: Ensures format is correct
  3. Serialization: Type → JSON
  4. Documentation: Correct OpenAPI schema