A collection of utilities for running FastAPI applications at Explosion AI
Project description
FastAPI Extras
This library is a collection of utilities for running FastAPI applications at Explosion AI.
HttpizeErrorsAPIRouter
This custom router's main functionality is to handle errors per route instead of through a global exception handler by adding the httpize_errors keyword argument to the FastAPI route declaration. This allows each route to return normal informative Python errors instead of the FastAPI HTTPException class to get valid responses.
It also times each request and sets the X-Response-Time header on the Response
An example route
@router.get("/testing", httpize_errors={ValueError: 400})
def test_route(i: int):
if i < 1:
raise ValueError("Bad Input Data")
return {"i": i}
If the ValueError is raised, this custom router knows to return a Response with a status code of 400 (Bad Request) and the message provided to the ValueError
Usage
FastAPI doesn't have built-in support for overriding the app Router, however this is required since we add a new keyword argument to the route declaration. FastAPI doesn't pass **kwargs forward, it only passes explict named keyword arguments.
To get around this, we need to overwrite the app router manually and refresh the routes after all of them have been included in the main app. This looks like:
from fastapi import FastAPI
from fastapi_extras import HttpizeErrorsAPIRouter, init_app
import uvicorn
# API Router (could be in another module)
api_router = HttpizeErrorsAPIRouter(tags=["tests"])
@api_router.get("/testing", httpize_errors={ValueError: 400})
def test_route(i: int):
if i < 1:
raise ValueError("Bad Input Data")
return {"i": i}
# Main app definition
app = FastAPI()
# Overwrite App Router to use the custom HttpizeErrorsAPIRouter
app.router = HttpizeErrorsAPIRouter.from_app(app)
# Include API Router from above
app.include_router(api_router)
# Refresh the App (this rebuilds the Starlette Middleware Stack)
init_app(app)
uvicorn.run(app)
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
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_explosion_extras-0.5.0.tar.gz.
File metadata
- Download URL: fastapi_explosion_extras-0.5.0.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fdae6934c4aeee27a4cb68a3dc0c44ca302941ee4adec8762666614f224867b
|
|
| MD5 |
25f9cda36965a0bfd677c0a15902b7a0
|
|
| BLAKE2b-256 |
523b5b9696e620ebb03932cb32d6a8e5c540646cdd2fe4fddac7f27abb3dbbf7
|
Provenance
The following attestation bundles were made for fastapi_explosion_extras-0.5.0.tar.gz:
Publisher:
publish_pypi.yml on explosion/fastapi-explosion-extras
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_explosion_extras-0.5.0.tar.gz -
Subject digest:
5fdae6934c4aeee27a4cb68a3dc0c44ca302941ee4adec8762666614f224867b - Sigstore transparency entry: 945332105
- Sigstore integration time:
-
Permalink:
explosion/fastapi-explosion-extras@26e454eb487787cc3e2cbc8745dfa39aa078486b -
Branch / Tag:
refs/tags/release-v0.5.0 - Owner: https://github.com/explosion
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_pypi.yml@26e454eb487787cc3e2cbc8745dfa39aa078486b -
Trigger Event:
release
-
Statement type:
File details
Details for the file fastapi_explosion_extras-0.5.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_explosion_extras-0.5.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2425837bd450361bcf342dcb46479c25a2f92de7e0708695a64cde0610a1c3b6
|
|
| MD5 |
377eaaec5a8c39bc6a75777ada13c049
|
|
| BLAKE2b-256 |
f9160004fb17e8ea517b0c891700b9702e18818d0a8a8395274385e5e1e45bd0
|
Provenance
The following attestation bundles were made for fastapi_explosion_extras-0.5.0-py3-none-any.whl:
Publisher:
publish_pypi.yml on explosion/fastapi-explosion-extras
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_explosion_extras-0.5.0-py3-none-any.whl -
Subject digest:
2425837bd450361bcf342dcb46479c25a2f92de7e0708695a64cde0610a1c3b6 - Sigstore transparency entry: 945332132
- Sigstore integration time:
-
Permalink:
explosion/fastapi-explosion-extras@26e454eb487787cc3e2cbc8745dfa39aa078486b -
Branch / Tag:
refs/tags/release-v0.5.0 - Owner: https://github.com/explosion
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_pypi.yml@26e454eb487787cc3e2cbc8745dfa39aa078486b -
Trigger Event:
release
-
Statement type: