OpenAPI (Swagger) Renderer for Sphinx
sphinxcontrib-openapi is a Sphinx extension that renders OpenAPI (formerly Swagger) specifications directly within Sphinx documentation. It leverages `sphinxcontrib-httpdomain` to provide an HTTP domain for describing RESTful HTTP APIs, avoiding the need to re-implement core functionality. It is actively maintained with its latest version 0.9.0 released in February 2026.
Warnings
- gotcha Older versions of `sphinxcontrib-openapi` (prior to current `0.9.0`) have been reported to struggle with certain structures in OpenAPI 3.0.x specifications, particularly concerning response object nesting, and were more reliably compatible with Swagger 2.0. If encountering rendering issues with OpenAPI 3.x, ensure you are on the latest `sphinxcontrib-openapi` version.
- gotcha The `sphinxcontrib-httpdomain` package is a mandatory dependency and must be installed and listed in your `conf.py` `extensions` list alongside `sphinxcontrib.openapi`. Failure to include it will result in `sphinxcontrib-openapi` not functioning correctly as it relies on its HTTP domain definitions.
- gotcha Specific configuration options for the `openapi` directive, such as `:examples:` or filtering `paths`, might not always work as expected for all OpenAPI spec structures or can lead to incomplete rendering. For instance, issues have been reported where schema examples do not show or filtering with multiple paths fails.
- gotcha As a Sphinx extension, `sphinxcontrib-openapi` can be affected by major breaking changes in Sphinx itself. Sphinx releases (e.g., Sphinx 9.0) often introduce incompatible changes to internal APIs that extensions rely on. Always test `sphinxcontrib-openapi` after upgrading Sphinx to a new major version.
Install
-
pip install sphinxcontrib-openapi
Imports
- sphinxcontrib.openapi
import sphinxcontrib.openapi
extensions = ['sphinxcontrib.openapi']
Quickstart
# conf.py
# Add 'sphinxcontrib.openapi' to your extensions list
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinxcontrib.httpdomain', # Ensure this is also included if not already
'sphinxcontrib.openapi'
]
# docs/api.rst
# Create a sample OpenAPI spec file at docs/specs/openapi.yml
# For example:
# swagger: "2.0"
# info:
# title: My API
# version: "1.0.0"
# paths:
# /greet:
# get:
# summary: Greet a user
# responses:
# 200:
# description: A greeting message
# In your .rst file, use the directive:
"""
API Documentation
=================
.. openapi:: specs/openapi.yml
:paths: /greet
:examples:
:group:
"""