FastAPI Extension Package
A well-tailored utility set for FastAPI.
Requirements
- Python 3.8+
Installation
To install FastAPI Extension Package, use pip:
pip install fastapi-ext-pkg
Shared middleware configuration
configure_middlewares registers middleware on an existing FastAPI application
before startup. Configure service values centrally while preserving routers and
lifespan:
from fastapi_extras.middlewares import configure_middlewares
configure_middlewares(
app,
root_path=settings.ROOT_PATH,
apm_config=settings.ELASTIC_APM,
cors={"allow_origins": settings.ALLOW_ORIGINS},
)
The default registration order is path removal, trace-header removal, APM,
extra_middlewares, CORS, then the garbage collector. Incoming requests traverse it in reverse. Use
order to preserve a service-specific order; include every configured name
exactly once. MiddlewareSpec defines custom middleware and named overrides
for replacement or disabling (enabled=False).
Use root_path=None, remove_cloud_trace_headers=False, apm_enabled=False,
or cors=None to omit middleware. garbage_collector=True registers
GarbageCollectorMiddleware as the outermost middleware; it is disabled by
default, and a garbage_collector override changes its options. Pass either apm_config or apm_client.
APM uses the upstream elasticapm.contrib.starlette.ElasticAPM by default and
requires the optional [apm] dependency. This branch contains no custom APM
implementation. To register another implementation supplied by the caller, pass
apm_middleware_class; apm_options supplies its constructor options.
Garbage collector middleware
GarbageCollectorMiddleware controls garbage collection without a background loop:
- After the application startup, it freezes the startup objects (
gc.freeze()), so later collections skip them. - Collections are driven by traffic: one runs after
every_requestsrequests (default 5000), or when the resident memory reachesmax_rss_mb, never more often thanmin_interval_seconds(default 60) plus a per-instance jitter of up to 20%. - Each collection runs after the response is sent and, on glibc, calls
malloc_trimto return freed memory to the operating system. This is best effort: fragmentedpymallocarenas may keep the resident memory high. - Each collection is logged at INFO level with the collected objects, the duration and the resident memory before and after.
from fastapi import FastAPI
from fastapi_extras.middlewares.garbage_collector import GarbageCollectorMiddleware
app = FastAPI()
app.add_middleware(GarbageCollectorMiddleware, max_rss_mb=768)
Metadata
Release files for fastapi-ext-pkg 0.8.0
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_ext_pkg-0.8.0.tar.gz | 15.0 kB | With provenance | Details for fastapi_ext_pkg-0.8.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_ext_pkg-0.8.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_ext_pkg-0.8.0-py3-none-any.whl |
Total release size: 35.6 kB
Release files / fastapi_ext_pkg-0.8.0.tar.gz
| Download URL | fastapi_ext_pkg-0.8.0.tar.gz |
|---|---|
| Size | 15.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
169655f425cd6495b30c73b4f691099f18ea8005c87013b964776be549d38378
|
|
BLAKE2b-256 checksum How to use checksums |
8f2db58464978ad8682941db328d6091ce8b6ee1c0d24112b361d5a1bf117684
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logRelease files / fastapi_ext_pkg-0.8.0-py3-none-any.whl
| Download URL | fastapi_ext_pkg-0.8.0-py3-none-any.whl |
|---|---|
| Size | 20.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc64a30fadc1f4d9ab65801851259ff7f99e6768f887c9a1ce4b00f4b2306795
|
|
BLAKE2b-256 checksum How to use checksums |
a2ef037a00f9acdf39eeabd22d3f3a51fb4ca04896db5388485b955f8a1b33de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logSecurity for fastapi-ext-pkg 0.8.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for fastapi_ext_pkg-0.8.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logA 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.