Create partial models from your pydantic models. Partial models may allow None for certain or all fields.
Maintained. Last release 5 months ago, commits in 9 of the last 26 weeks. Weaker on release cadence and contributor spread.
pip install pydantic-partialpydantic-partial
Installation
Just use pip install pydantic-partial to install the library.
Note: pydantic-partial is compatible with pydantic version 2.x on Python 3.10, 3.11, 3.12,
3.13 and 3.14. This is also ensured running all tests on all those versions using tox.
About
Create partial models from your normal pydantic models. Partial models will allow some or all fields to be optional and thus not be required when creating the model instance.
Partial models can be used to support PATCH HTTP requests where the user only wants
to update some fields of the model and normal validation for required fields is not
required. It may also be used to have partial response DTOs where you want to skip
certain fields, this can be useful in combination with exclude_none. It is - like
shown in these examples - intended to be used with API use cases, so when using
pydantic with for example FastAPI.
Disclaimer: This is still an early release of pydantic-partial. Things might
change in the future. PR welcome. ;-)
Usage example
pydantic-partial provides a mixin to generate partial model classes. The mixin can
be used like this:
import pydantic
from pydantic_partial import PartialModelMixin
# Something model, then can be used as a partial, too:
class Something(PartialModelMixin, pydantic.BaseModel):
name: str
age: int
# Create a full partial model
FullSomethingPartial = Something.model_as_partial()
FullSomethingPartial() # Same as FullSomethingPartial(name=None, age=None)
Without using the mixin
You also may create partial models without using the mixin:
import pydantic
from pydantic_partial import create_partial_model
# Something model, without the mixin:
class Something(pydantic.BaseModel):
name: str
age: int
# Create a full partial model
FullSomethingPartial = create_partial_model(Something)
FullSomethingPartial() # Same as FullSomethingPartial(name=None, age=None)