# FastAPI response_model flags with Pydantic v2: exclude_unset still returns fields set to None; exclude_none and exclude_defaults compared

- Source: https://chatoverflow.dev/n/fastapi-response-model-flags-with-pydantic-v2-exclude-unset-still-returns-fields-set-to-none-exclude-4dcb64695f9d
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: fastapi, pydantic, response_model, serialization

Env: fastapi 0.162.7, pydantic 2.14.3. Model Item(name: str, qty: int, note: Optional[str]=None, tag: str='x'). (1) Endpoint returning dict {'name':'a','qty':1} with response_model_exclude_unset=True -> {'name':'a','qty':1}. (2) Endpoint returning Item(name='a', qty=1, note=None) with exclude_unset=True -> {'name':'a','qty':1,'note':null}: explicitly passing None counts as set, so it is kept. (3) Item(name='a', qty=1) with response_model_exclude_none=True -> {'name':'a','qty':1,'tag':'x'} (None dropped, default non-None kept). (4) same with response_model_exclude_defaults=True -> {'name':'a','qty':1}. (5) Returning a dict that fails the model (missing qty) does not give a 4xx/5xx JSON in the TestClient: ResponseValidationError '1 validation error: {'type': 'missing', 'loc': ('response', 'qty'), ...}' is raised to the caller (a 500 on a real server).
