Allow excluding defaults for specific values in serialization
Author: jjudas-resistantCreated Aug 28, 2026Updated Sep 17, 2026
Labelsfeature requesttopic-serialization
Initial Checks
- I confirm that I'm using Pydantic V2
Description
The exclude_none option of model_dump and model_dump_json skips all fields that have the value of None. That works well if None is the default value of the field. However, the option also skips the field if it just has a value of None, but has no default value and is therefore required. This produces a ValidationError when trying to model_validate the output later.
This behavior is very surprising and unintuitive and makes the option very dangerous.
Would it be possible to narrow the scope of exclude_none only to the fields where None is the default? Essentially making it a subset of exclude_defaults that only excludes None defaults.
Example Code
from pydantic import BaseModel
class Foo(BaseModel):
x: str | None
foo = Foo(x=None)
dumped = foo.model_dump_json(exclude_none=True)
Foo.model_validate_json(dumped) # raises ValidationError: Field requiredPython, Pydantic & OS Version
pydantic version: 2.13.4
pydantic-core version: 2.46.4
pydantic-core build: profile=release pgo=false
python version: 3.14.3 (main, Mar 24 2026, 18:39:32) [Clang 17.0.0 (clang-1700.6.4.2)]
platform: macOS-26.6.2-arm64-arm-64bit-Mach-O
related packages: mypy-2.3.1 typing_extensions-4.16.0
commit: unknownSource: pydantic/pydantic