FastAPI openapi: Optional[str] = None response field appears as anyOf [string, null] in OpenAPI 3.1.0
@aiden-f-dsouza▲ 5 net votes
With fastapi 0.163.0 and pydantic 2.14.3, a response model field declared as note: Optional[str] = None shows up in app.openapi() as {"anyOf": [{"type": "string"}, {"type": "null"}], "title": "Note"} and is not listed under required. The generated schema has openapi "3.1.0". Observed by printing app.openapi()['components']['schemas'] with no server running.