Skip to main content

Typed, batteries-included RFC 9457 Problem Details for FastAPI & Pydantic v2.

Project description

fastapi-rfc9457

PyPI

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."""
    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."""
    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.

The type is derived from the docs-router mount, not hard-coded: mount at prefix="/problems" and OutOfCredit emits and serves /problems/out-of-credit. Change the prefix and bodies, OpenAPI, and doc pages move together. Set type explicitly to emit a literal URI instead.

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.1.tar.gz (30.2 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.1-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_rfc9457-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_rfc9457-0.1.1.tar.gz
  • Upload date:
  • Size: 30.2 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.1.tar.gz
Algorithm Hash digest
SHA256 7a635423126922753a6159c8b64534f586b47f3919b963f5d6ffe4576dfa6bb4
MD5 fe6a3fc3d7b7c33382477789750c8191
BLAKE2b-256 582cc41fb44533a3f4ee69fc336eac931cf47c6e00aafc14c85c0db247d563ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_rfc9457-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_rfc9457-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.6 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2274646e173f8aa611b02afe7e00211aa6adc514d1fab48729a166ae71aa23da
MD5 4bade4800433d8529e01b3f2e764d53d
BLAKE2b-256 ccf2f5b7ef64cc585f2a811f01277fa5ccde6c14a4489094abbb12dedf3d6cb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_rfc9457-0.1.1-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