FastAPI Docs Exception
Automatically expose your custom FastAPI exceptions in Swagger / ReDoc — with proper grouping and examples.
✨ Features
Stop repeating responses={...} in every route : This lightweight utility turns your custom HTTPException subclasses into shared, documented OpenAPI responses — complete with example payloads.
- Update docs: Convert your
HTTPExceptionsubclasses into shared OpenAPI responses. - Grouped by status code: if you have multiple exceptions with the same status code, they will be grouped together.
- Pydantic schema: Provide Pydantic models or tweak JSON Schema on the fly.
🚀 Installation
# With uv (recommended)
uv add fastapi_docs_exception
# Or the classic way
pip install fastapi_docs_exception
⚡ Quickstart
from fastapi import FastAPI, HTTPException
from fastapi_docs_exception import HTTPExceptionResponseFactory
# 1️⃣ Define your exceptions any way you like
class ApiNotFoundException(HTTPException):
"""Custom exception for API not found errors in FastAPI."""
def __init__(self, detail: str = "API key not found or invalid"):
super().__init__(status_code=404, detail=detail)
class NotFoundError(HTTPException):
"""Custom exception for not found errors in FastAPI."""
def __init__(self, detail: str = "Resource not found in the storage"):
super().__init__(status_code=404, detail=detail)
class InternalServerError(HTTPException):
"""Custom exception for internal server errors in FastAPI."""
def __init__(self, detail: str = "Internal server error, please try again later"):
super().__init__(status_code=500, detail=detail)
# 2️⃣ Feed them to the factory (classes allow more flexibility)
# You can specify Pydantic schemas, edit the JSON schema, etc.
exc_response_factory = HTTPExceptionResponseFactory()
app = FastAPI(
responses=exc_response_factory.build([
NotFoundError(), # 404 response
ApiNotFoundException(), # 404 response (grouped with the previous one)
InternalServerError(), # 500 response (only one)
]),
)
# 3️⃣ Use your exceptions in the code
@app.get("/items/{item_id}")
def get_item(item_id: str):
if item_id != "42":
raise NotFoundError()
return {"item_id": item_id}
Open http://localhost:8000/docs and you will see the custom 404 and 500 responses in the Swagger UI and ReDoc documentation with example payloads.
🔧 Development
# Clone
git clone https://github.com/your-user/fastapi_docs_exception.git
cd fastapi_docs_exception
# Optional: create a virtual env
uv venv # makes .venv and activates it
source .venv/bin/activate
# Install dev dependencies (test, lint, type-checking)
uv pip install -e ".[dev]"
Lint, format, type-check (all in one)
# Run all lint with ruff (lint, format), ty, bandit
uv run lint
Run the test-suite
# Run all tests with pytest
uv run test
A coverage report will be shown in the terminal and htmlcov will be generated in the project root.
🤝 Contributing
All kinds of contributions are welcome – bug reports, feature ideas, docs fixes...
- Fork the repo & create your branch:
git checkout -b feat/amazing-stuff - Commit your changes with clear messages.
- Run
uv run lint && uv run testand ensure everything stays green. - Open a Pull Request describing why the change is useful.
Please use commitizen to format your commit messages, e.g. cz commit
📜 License
fastapi_docs_exception is distributed under the MIT License – see LICENSE for details.
Metadata
Release files for fastapi-docs-exception 0.4.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_docs_exception-0.4.3.tar.gz | 186.4 kB | Without provenance | Details for fastapi_docs_exception-0.4.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_docs_exception-0.4.3-py3-none-any.whl | Python 3 | none | any | Details for fastapi_docs_exception-0.4.3-py3-none-any.whl |
Total release size: 193.4 kB
Release files / fastapi_docs_exception-0.4.3.tar.gz
| Download URL | fastapi_docs_exception-0.4.3.tar.gz |
|---|---|
| Size | 186.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e8b7cfed79b244a63e5a50a255dd98601dece89983e87e0cd6309f47a4a92e56
|
|
BLAKE2b-256 checksum How to use checksums |
6b481c5567cc509b56aa2ec6f80e5a1b626e8e06b730f9e94d2bb1f0ed922bdd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.3
|
Release files / fastapi_docs_exception-0.4.3-py3-none-any.whl
| Download URL | fastapi_docs_exception-0.4.3-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c7897236d132a5e25a1e0486a1ed271dd84732724e207bfd072f96d90ad870d8
|
|
BLAKE2b-256 checksum How to use checksums |
8d1c9bd97abc7325585c23d759bb626f85a18a5829c6a9d6e8df7095d5baad9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.3
|
Security for fastapi-docs-exception 0.4.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.