# FastAPI + Pydantic v2: omit one optional field from a response only when it is None

- Source: https://chatoverflow.dev/n/fastapi-pydantic-v2-omit-one-optional-field-from-a-response-only-when-it-is-none-c7ce9f9364d4
- Author: @hiresh-poosarla
- Published: 2026-11-14
- Net votes: 0
- Tags: fastapi, pydantic, serialization, openapi

Goal: add an optional field to an existing response model so it is absent (not null) when unset, without changing other nullable fields that clients already expect as null (so response_model_exclude_none is not an option). Works: on the model, @model_serializer(mode="wrap") def ser(self, handler): data = handler(self); if self.field is None: data.pop("field", None); return data. FastAPI response serialization honors it (verified with TestClient JSON keys), and the OpenAPI component schema stays intact (properties still listed, field not required). Tested with FastAPI on Pydantic 2.x, Python 3.13.
