Typed, batteries-included RFC 9457 Problem Details for FastAPI & Pydantic v2.
Project description
fastapi-rfc9457
Typed, batteries-included RFC 9457 "Problem Details for HTTP APIs" for FastAPI & Pydantic.
Define an error once - it serializes as application/problem+json, documents
itself in OpenAPI, and parses back into a typed exception on the client.
from fastapi import FastAPI
from fastapi_rfc9457 import Problem, add_problem_handlers, get_problem_docs_router, problems
class OutOfCredit(Problem):
"""The account does not have enough credit."""
type = "/problems/out-of-credit"
title = "Out of Credit"
status = 403
balance: int # typed extension members, checked at the raise site
accounts: list[str]
class AccountSuspended(Problem):
"""The account is suspended and cannot be charged."""
type = "/problems/account-suspended"
title = "Account Suspended"
status = 403
app = FastAPI()
add_problem_handlers(app) # handlers + problem+json OpenAPI
app.include_router(get_problem_docs_router(), prefix="/problems") # dereferenceable type URIs
@app.get("/charge", responses=problems(OutOfCredit, AccountSuspended))
async def charge() -> dict:
raise OutOfCredit(detail="Not enough credit.", balance=30, accounts=["/acct/12"])
Accurate OpenAPI, for free
One route can declare several failure modes. Distinct statuses get their own
response; same-status problems become a oneOf union you flip through in
Swagger's Examples dropdown — all under application/problem+json.
Dereferenceable type URIs
Mount the docs router and every problem type resolves to a live page listing
its typed extension members.
Features
- Typed + validated extension members that round-trip through serialization, OpenAPI, and the client.
- Accurate per-route OpenAPI under
application/problem+json. - Structured 422 that preserves field + list-index mapping (no flattening).
- Client-side parsing back into typed problems.
Install
uv add fastapi-rfc9457 # add fastapi-rfc9457[client] for the httpx hook
Example
cd example && uv run uvicorn main:app --reload # then open localhost:8000/docs
See example/ for the full runnable app, and
example/client.py for the httpx hook (fastapi-rfc9457[client])
that raises those problems back as typed exceptions on the consumer side.
Notes
- Replaces FastAPI's default 422 body with
application/problem+json.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_rfc9457-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_rfc9457-0.1.0.tar.gz
- Upload date:
- Size: 24.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71d2fb4973885e997a112807377dd4227ad1eac553a53f58c869f157b1bc1b85
|
|
| MD5 |
a12e51d562f0742c624de086e339afa7
|
|
| BLAKE2b-256 |
cafe077eb76985ec9fa9d5c3efd3be5d5017b7263cdf1c93aed9afc03e6c5e39
|
Provenance
The following attestation bundles were made for fastapi_rfc9457-0.1.0.tar.gz:
Publisher:
publish.yaml on PythonFZ/fastapi-rfc9457
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_rfc9457-0.1.0.tar.gz -
Subject digest:
71d2fb4973885e997a112807377dd4227ad1eac553a53f58c869f157b1bc1b85 - Sigstore transparency entry: 1860508318
- Sigstore integration time:
-
Permalink:
PythonFZ/fastapi-rfc9457@9cc743bd672cbf06020d8c70c4e290d9e7548213 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/PythonFZ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@9cc743bd672cbf06020d8c70c4e290d9e7548213 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_rfc9457-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_rfc9457-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40c501cfabd7dffedd6ada3014ee229a10805c51f8bfe96805fd75d1f0103b71
|
|
| MD5 |
66ad720c512d398f586df4fbaefb95ba
|
|
| BLAKE2b-256 |
874c7bca3e71f9cd42075909c49798067fb9c8c46c312f3e940858d16a15052b
|
Provenance
The following attestation bundles were made for fastapi_rfc9457-0.1.0-py3-none-any.whl:
Publisher:
publish.yaml on PythonFZ/fastapi-rfc9457
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_rfc9457-0.1.0-py3-none-any.whl -
Subject digest:
40c501cfabd7dffedd6ada3014ee229a10805c51f8bfe96805fd75d1f0103b71 - Sigstore transparency entry: 1860508563
- Sigstore integration time:
-
Permalink:
PythonFZ/fastapi-rfc9457@9cc743bd672cbf06020d8c70c4e290d9e7548213 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/PythonFZ
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@9cc743bd672cbf06020d8c70c4e290d9e7548213 -
Trigger Event:
release
-
Statement type: