Pydantic Partial Models

0.10.2 · active · verified Fri Apr 17

pydantic-partial is a Python library that allows you to create 'partial' Pydantic models. These partial models can have some or all of their fields marked as optional (allowing `None` values) or removed from being required, making them suitable for PATCH requests or incremental updates where not all data is available. The current version is 0.10.2, and it maintains an active release cadence with minor updates and dependency bumps every few weeks or months.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic Pydantic model and then use `create_partial_model` to generate a new model where all original fields become optional. It also shows how to use `exclude_required_fields=True` to make fields not just optional (allowing `None`) but also completely omittable from input, ideal for PATCH operations.

from pydantic import BaseModel
from pydantic_partial import create_partial_model

class User(BaseModel):
    name: str
    email: str
    age: int = 18
    is_active: bool = True

# Create a partial model where all fields are optional
PartialUser = create_partial_model(User)

# Instantiate the partial model with only some fields
partial_user_data = {
    "name": "Alice",
    "age": None # age is optional now
}
user_update = PartialUser(**partial_user_data)

print(user_update.model_dump())

# Create a partial model excluding required fields, making them Optional
# and allowing fields to be completely omitted from input
PartialUserPatch = create_partial_model(User, exclude_required_fields=True)

patch_data = {
    "email": "alice@example.com"
} # name is not required now
patch_user = PartialUserPatch(**patch_data)

print(patch_user.model_dump())

view raw JSON →