Skip to main content
There are cases where you might need to modify the generated OpenAPI schema to add custom metadata, vendor extensions, or modify the structure to meet specific requirements.

The Normal Process

Understanding how FastAPI generates OpenAPI schemas helps you customize them effectively.

How OpenAPI Generation Works

A FastAPI application instance has an .openapi() method that returns the OpenAPI schema:
  1. When the application starts, a path operation for /openapi.json is registered
  2. This endpoint returns a JSON response from the .openapi() method
  3. The method checks the .openapi_schema property and returns it if available
  4. If not available, it generates the schema using fastapi.openapi.utils.get_openapi()
The .openapi_schema property acts as a cache to avoid regenerating the schema on every request.

The get_openapi() Function

The get_openapi() utility function accepts these parameters:
  • title: The OpenAPI title shown in the docs
  • version: Your API version (e.g., 2.5.0)
  • openapi_version: The OpenAPI specification version (default: 3.1.0)
  • summary: A short summary of the API
  • description: Detailed API description (supports Markdown)
  • routes: List of registered path operations from app.routes
  • webhooks: Webhook definitions
  • tags: Tag metadata for organizing endpoints
  • servers: Server information
  • terms_of_service: Terms of service URL
  • contact: Contact information
  • license_info: License details

Customizing the OpenAPI Schema

You can override the default OpenAPI generation to add custom extensions or modify the schema.

Basic FastAPI Application

Start with a standard FastAPI application:

Create a custom_openapi() Function

Define a function that generates and customizes the OpenAPI schema:
The if app.openapi_schema: check ensures the schema is only generated once and then cached for subsequent requests.

Override the openapi() Method

Replace the default method with your custom function:

Common Customization Examples

Adding Vendor Extensions

Many tools support vendor-specific extensions (prefixed with x-):

Modifying Security Schemes

Customize authentication documentation:

Adding Custom Response Examples

Enhance API documentation with additional examples:

Viewing Your Custom Schema

After customizing the OpenAPI schema:
  1. Start your application with uvicorn main:app --reload
  2. Visit /docs to see your changes in Swagger UI
  3. Visit /redoc to see your changes in ReDoc
  4. Access /openapi.json to view the raw schema
Be careful when modifying the OpenAPI schema structure. Invalid modifications may cause documentation UIs to fail or display incorrectly.

Best Practices

Always cache the schema: Use the .openapi_schema property to avoid regenerating the schema on every request.
  • Test thoroughly: Validate your custom schema using OpenAPI validators
  • Document extensions: Add comments explaining custom vendor extensions
  • Preserve structure: Don’t remove required OpenAPI fields
  • Use type checking: Leverage Python type hints when modifying the schema

Complete Example

Here’s a full example combining multiple customizations: