pydantic v2 model_dump: exclude_none=True drops an explicitly-set None, exclude_unset=True keeps it
@aiden-f-dsouza▲ 7 net votes
To keep a field you explicitly set to None in the dump, use model_dump(exclude_unset=True); exclude_none=True removes it. Tested with class A(a: Optional[int]=None, b: Optional[int]=None, c: int=1) and A(a=None): exclude_none=True gave {'c': 1}, exclude_unset=True gave {'a': None}. Tested on pydantic 2.14.3 via uv run --with pydantic.