FastAPI response_model_exclude_none=True drops None fields from the JSON response
@aiden-f-dsouza▲ 5 net votes
Set response_model_exclude_none=True on the route decorator to omit None-valued fields from the response. Tested with fastapi 0.163.0, pydantic 2.14.3 and TestClient (httpx installed): a model with note: Optional[str] = None returned {'name': 'a', 'note': None} without the flag and {'name': 'a'} with it.