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 Identifierdatetime: Date and timedate: Date onlytime: Time onlytimedelta: Time durationfrozenset: Immutable setbytes: Binary dataDecimal: 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:DateTime Types
datetime - Date and Time
date - Date Only
time - Time Only
timedelta - Duration
timedelta is represented as seconds (float) in JSON.Numeric Types
Decimal - Precise Numbers
UseDecimal for financial calculations where precision matters:
Binary Data Types
bytes - Binary Data
bytes are represented as base64-encoded strings in JSON.Collection Types
frozenset - Immutable Set
frozenset ensures unique values and is immutable, but is represented as a list in JSON.URL Types
HttpUrl - URL Validation
- Has a valid scheme (http/https)
- Has a valid domain
- Is properly formatted
Email and Other String Types
EmailStr - Email Validation
Path and File Types
FilePath and DirectoryPath
Color Type
"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, frozenset2
Pydantic Types
EmailStr, HttpUrl, FilePath, DirectoryPath, ColorStr, Json3
Network Types
IPvAnyAddress, IPvAnyInterface, IPvAnyNetwork4
Secret Types
SecretStr, SecretBytes (not logged/dumped by default)Type Conversion and Validation
All these types provide:- Automatic Conversion: String → Type
- Validation: Ensures format is correct
- Serialization: Type → JSON
- Documentation: Correct OpenAPI schema
Related Topics
- Request Body - Use these types in request models
- Response Model - Use these types in responses
- Path Parameters - Use UUID and other types in URLs