fastapi-typed-errors
English · Русский
Typed HTTP errors for FastAPI — exact Literal codes in OpenAPI, discriminated oneOf unions on the code field, and a single source of truth: the error class itself.
In plain FastAPI, raise HTTPException(404, "...") buries the error code in a string — clients can't switch on it, OpenAPI has no code type or body schema, and you hand-maintain responses={} on every route. This package makes an error a class: its HTTP status, machine-readable code and response model are declared once and derived automatically, so your error contract is typed, self-documenting, and verifiable in CI.
class NotFoundError(BaseError[Literal[ErrorCode.NOT_FOUND]]):
http_status = HTTPStatus.NOT_FOUND
@router.get("/items/{item_id}")
def get_item(item_id: int) -> Annotated[Item, Raises[NotFoundError]]:
if item_id == 0:
raise NotFoundError("No item")
return Item(item_id=item_id)
The response body is always {"code": "NOT_FOUND", "detail": "No item"}, and OpenAPI gets a 404 with the exact Literal["NOT_FOUND"] code and body model — no manual responses.
Install
pip install fastapi-typed-errors # core + decorator
pip install "fastapi-typed-errors[cli]" # + the CI-checker CLI
Requires Python 3.12+, FastAPI ≥ 0.115, Pydantic ≥ 2.9.
How to use it
1. Define errors and register the one handler.
from fastapi import FastAPI
from fastapi_typed_errors import BaseError, handle_base_error
app = FastAPI()
app.add_exception_handler(BaseError, handle_base_error)
2. Declare errors — pick your level of magic:
# a) by hand (core only) — write responses yourself
@app.get("/x", responses={404: {"model": error_models(NotFoundError)}})
def a() -> Item: ...
# b) declared — the marker fills responses for you
router = with_errors(APIRouter())
@router.get("/y")
def b() -> Annotated[Item, Raises[NotFoundError, ForbiddenError]]: ...
# c) automatic — no markers at all; errors are found statically
router = with_errors(APIRouter(), auto=True)
@router.get("/z")
def c(user: Annotated[User, Depends(current_user)]) -> Item:
raise NotFoundError("...") # auto -> 404, plus whatever current_user raises
3. Verify the contract in CI. check_raises compares what each route declares against what it can actually raise — in the endpoint and its whole dependency tree:
def test_error_contracts() -> None:
assert check_raises(app).ok
Or as a command: fastapi-typed-errors check app.main:app (exit 0/1/2).
Why it's cool
- Exact types in OpenAPI — a precise
Literalcode per status; several errors on one status become a discriminatedoneOfunion, so Swagger UI shows a variant picker by code. - Single source of truth — status, code and model declared once; the metaclass derives the rest.
- Zero-boilerplate
responses— via theRaisesmarker or fully automaticauto=True. - Static contract checking —
check_raisescatches a raise you forgot to declare (or a dead declaration) before it ships. - Non-invasive —
with_errorspatches the router in place and preserves object identity, soinclude_router, websockets and app-level decorators keep working natively. - Rigorous — fully typed (
py.typed), 100% branch-covered, checked withruff+tyat max strictness.
Documentation
📖 Full documentation — guide, customization (custom envelopes, ABC, bare-string codes), limitations, and an auto-generated API reference. Available in English and Russian.
License
MIT.
Metadata
Release files for fastapi-typed-errors 1.0.1
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_typed_errors-1.0.1.tar.gz | 21.1 kB | Without provenance | Details for fastapi_typed_errors-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_typed_errors-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for fastapi_typed_errors-1.0.1-py3-none-any.whl |
Total release size: 48.1 kB
Release files / fastapi_typed_errors-1.0.1.tar.gz
| Download URL | fastapi_typed_errors-1.0.1.tar.gz |
|---|---|
| Size | 21.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f7e5a51f42f4daaa45893cdd1ae4004c348ffeb8f9bb60e56e8329dc4f3f356
|
|
BLAKE2b-256 checksum How to use checksums |
d27cc705bcef7cd6dd7d6ee7e6d6b3006adb4e4777317400b2b47ab30d130622
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / fastapi_typed_errors-1.0.1-py3-none-any.whl
| Download URL | fastapi_typed_errors-1.0.1-py3-none-any.whl |
|---|---|
| Size | 27.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5fd6112421a99e6738357ac9aeb573954154ffe2479e0f42062606734c8df9e8
|
|
BLAKE2b-256 checksum How to use checksums |
6bef30bd569c3980d21970dc98033df13d3ea8cf8fa564d022483ed04b85d4a2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for fastapi-typed-errors 1.0.1
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.