CS504070 - FastAPI Tutorials
  • 🧩CS504070 - FastAPI Tutorials
  • 🔎Unit 1: Python Type Hint
  • ⚙️Unit 2: Pydantic
  • 🔃Unit 3: Concurrency
  • 💾Unit 4: Install FastAPI
  • 🍉Unit 5: Hello World!
  • 🍌Unit 6: Path Parameters
  • 🍋Unit 7: Query Parameters
  • 🍊Unit 8: Request Body
  • 🍐Unit 9: Query Parameters and Validations
  • 🍎Unit 10: Path Parameters and Validations
  • 🍏Unit 11: Multiple Parameters
  • 🍇Unit 12: Request Body - List Fields and Nested Models
  • 🍓Unit 13: Data Types
  • 🍪Unit 14: Cookie Parameters
  • 🫐Unit 15: Header Parameters
  • 🍈Unit 16: Response Model - Return Type
  • 🍒Unit 17: Additional Models
  • 🥑Unit 18: Implementing JWT Authentication with FastAPI
  • ⚙️Appendix A
  • 🍭Appendix B
Powered by GitBook
On this page
  • Installation
  • Sample Program
  • Key Features
  • Data Validation and Parsing
  • Type Annotations
  • Error Handling
  • ORM Mode
  • Settings Management
  • JSON Schema Support
  • FastAPI Integration
  • Extensibility
  • Simple Syntax

Unit 2: Pydantic

PreviousUnit 1: Python Type HintNextUnit 3: Concurrency

Last updated 1 year ago

is a Python library used for data parsing and validation using Python type annotations. Its main goal is to provide an easy way to define how data should be in pure, human-readable Python code, and to ensure that the data you work with matches these specifications.

Installation

Installation is as simple as:

pip install pydantic

Pydantic has a few dependencies:

  • pydantic-core: Core validation logic for pydantic written in rust.

  • typing-extensions: Backport of the standard library typing module.

  • annotated-types: Reusable constraint types to use with typing.Annotated.

Sample Program

from datetime import datetime
from pydantic import BaseModel


class User(BaseModel):
    id: int
    name: str = "John Doe"
    signup_ts: datetime | None = None
    friends: list[int] = []


external_data = {
    "id": "123",
    "signup_ts": "2017-06-01 12:22",
    "friends": [1, "2", b"3"],
}
user = User(**external_data)
print(user)
print(user.id)

Save the file as Example.py, then execute the program using the command below:

python Example.py

And the output will look like that

id=123 name='John Doe' signup_ts=datetime.datetime(2023, 1, 3, 18, 4) friends=[1, 2, 3]
123

The Python program above demonstrates the use of the Pydantic library, which is designed for data validation and settings management using Python type annotations. Let's break down some important code:

  • The User class is defined as a subclass of Pydantic's BaseModel. This enables automatic validation of data based on the type annotations provided in the class.

  • The User class has four fields where signup_ts is a datetime field for the user's sign-up timestamp. It can also be None, as indicated by datetime | None. The | None is a way to specify that the field is optional and can be None.

  • external_data is a dictionary containing data that will be used to create a User object. The keys in this dictionary correspond to the fields in the User class.

  • user = User(**external_data): This line creates an instance of the User class. The **external_data syntax unpacks the dictionary into keyword arguments. Pydantic automatically handles the conversion of data types (e.g., converting the string "123" to an integer for the id field) and validates the data.

  • If any field is missing in external_data (except those with default values), or if there's a type mismatch, Pydantic will raise an error.

Key Features

Data Validation and Parsing

Pydantic ensures that the data you receive, whether from an external source like a JSON file or an API call, fits your expected structure and type. It validates the data against a predefined schema and can automatically convert types when possible.

Type Annotations

Pydantic heavily relies on Python's type annotations. This allows you to define data models using standard Python classes and type hints, making your code clear and easy to understand.

Error Handling

When data doesn't conform to the defined schema, Pydantic raises informative errors. These errors can tell you exactly where and how the data is incorrect, which is incredibly useful for debugging and data cleaning.

ORM Mode

Pydantic can also work with ORM (Object-Relational Mapping) models, allowing you to parse and validate data from ORM objects. This makes it compatible with databases using ORMs like SQLAlchemy.

Settings Management

Pydantic is often used for managing application settings, where it can validate environment variables and other configuration data. This ensures that your application starts with the correct and valid configuration.

JSON Schema Support

It can generate JSON Schemas from your Python models. This is particularly useful for API documentation and validation.

FastAPI Integration

Pydantic is integral to FastAPI, a modern web framework for building APIs. FastAPI uses Pydantic models to validate incoming request data and to serialize outgoing response data.

Extensibility

Pydantic can be extended and customized to handle almost any type of data validation. This includes creating custom validators and defining complex data types.

Simple Syntax

Despite its power, Pydantic remains syntactically simple, leveraging Python's native type hints.


Pydantic
⚙️
Page cover image