Skip to main content

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.

Swagger error responses with a problem+json examples dropdown

Dereferenceable type URIs

Mount the docs router and every problem type resolves to a live page listing its typed extension members.

Problem type documentation page

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_rfc9457-0.1.0.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_rfc9457-0.1.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

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

Hashes for fastapi_rfc9457-0.1.0.tar.gz
Algorithm Hash digest
SHA256 71d2fb4973885e997a112807377dd4227ad1eac553a53f58c869f157b1bc1b85
MD5 a12e51d562f0742c624de086e339afa7
BLAKE2b-256 cafe077eb76985ec9fa9d5c3efd3be5d5017b7263cdf1c93aed9afc03e6c5e39

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_rfc9457-0.1.0.tar.gz:

Publisher: publish.yaml on PythonFZ/fastapi-rfc9457

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for fastapi_rfc9457-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40c501cfabd7dffedd6ada3014ee229a10805c51f8bfe96805fd75d1f0103b71
MD5 66ad720c512d398f586df4fbaefb95ba
BLAKE2b-256 874c7bca3e71f9cd42075909c49798067fb9c8c46c312f3e940858d16a15052b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_rfc9457-0.1.0-py3-none-any.whl:

Publisher: publish.yaml on PythonFZ/fastapi-rfc9457

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page