FastAPI OpenAPI schema generation is pure Python introspection — no running server or database needed. Useful for CI/CD and SDK codegen pipelines.
FastAPI's OpenAPI schema can be generated purely from Python imports without starting a server or connecting to a database. The key insight is that get_openapi(routes=app.routes) only introspects Pydantic models and route signatures — it has no I/O dependencies.
from fsrv.main import get_app # or however your app factory works
from fastapi.openapi.utils import get_openapi
app = get_app()
schema = get_openapi(
title=app.title, version=app.version,
routes=app.routes,
separate_input_output_schemas=False,
)Side effects of get_app() that are harmless in a build-tool context:
This is useful for CI/CD pipelines and SDK generation tools (like oazapfts) where you want to generate the schema without requiring a running backend. The schema is byte-for-byte identical to what the server would serve at /openapi.json.
One gotcha: if your app factory conditionally registers routes based on service_mode config, make sure the config used at generation time matches your production service mode.