Registry / web-framework / brotli-asgi

brotli-asgi

JSON →
library1.6.0pypypi✓ verified 22d ago

brotli-asgi is an ASGI middleware that provides Brotli response compression for ASGI applications like Starlette and FastAPI. It offers faster and more dense compression than GZip, making it a drop-in replacement for the GZipMiddleware. As of v1.6.0, it remains actively maintained with periodic updates for dependencies and occasional feature enhancements.

pip install brotli-asgi
INSTALL
IMPORT
SIG · BROTLI-ASGI
B
brotli-asgi
web-frameworkpythonv1.6.0
Install
2.0s avg
Import
207ms
Disk
23MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.214s · 24.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.0s · import 0.200s · 26MB
23MB installed
● package 23MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BrotliMiddleware
from brotli_asgi import BrotliMiddleware

This example demonstrates how to integrate `BrotliMiddleware` with a FastAPI application. The middleware is added globally, applying Brotli compression to responses that meet the specified criteria (e.g., minimum size). It also configures a gzip fallback for clients that don't support Brotli.

from fastapi import FastAPI from brotli_asgi import BrotliMiddleware from starlette.responses import JSONResponse app = FastAPI() app.add_middleware( BrotliMiddleware, quality=4, # Compression quality (0-11, higher is slower but denser) mode="text", # Compression mode: "generic", "text", or "font" minimum_size=400, # Only compress responses larger than this many bytes gzip_fallback=True # Fallback to gzip if 'br' is not in Accept-Encoding ) @app.get("/") async def home(): return JSONResponse({"data": "a" * 4000}) # To run this example, you would typically use uvicorn: # uvicorn your_module_name:app --reload
Debug
Known issues
breakingA security vulnerability (GHSA-3qj8-93xh-pwh2) was addressed in v1.4.0. Users on older versions should upgrade immediately to mitigate potential risks related to underlying Starlette dependencies.
fix
Upgrade to brotli-asgi version 1.4.0 or newer.
affects: <1.4.0
gotchaBrotli compression can be CPU-intensive at higher quality settings. While it offers better compression ratios, it might increase latency (Time to First Byte) for highly dynamic content. Consider balancing `quality` and `minimum_size` parameters or pre-compressing static assets.
fix
Adjust `quality` (e.g., 3-5 for APIs) and `minimum_size` parameters in `BrotliMiddleware`. For static assets, pre-compression with higher quality is often preferred during build processes.
affects: All
gotchaThe middleware will not apply compression if a `Content-Encoding` header is already set on the response, to prevent double-encoding. Additionally, the correct double-encoding prevention behavior when using `gzip_fallback=True` requires Starlette version >=0.22.0.
fix
Ensure responses do not prematurely set `Content-Encoding` if `brotli-asgi` is expected to compress them. Maintain an up-to-date Starlette version, preferably >=0.22.0, for robust fallback handling. The 'avoid double encode' fix was in v1.3.0.
affects: <1.3.0 (for behavior change), <1.0.0 (for Starlette version reliance)
gotchaThe order of middleware in ASGI applications is critical. Ensure `BrotliMiddleware` is placed appropriately, especially relative to other middleware that might modify response bodies or headers (e.g., authentication, CORS).
fix
Carefully review your middleware stack. Generally, compression middleware should be applied early enough to process responses, but after any middleware that generates or significantly alters the response content. Consult your ASGI framework's documentation for middleware ordering best practices.
affects: All
gotchaTo exclude specific routes or handlers from compression, you typically leverage the ASGI framework's (e.g., Starlette, FastAPI) routing capabilities to apply middleware conditionally or to specific sub-applications/routers, rather than relying on a direct exclusion parameter within `BrotliMiddleware` itself.
fix
Refer to your ASGI framework's documentation on applying middleware to specific routes or mounted applications. For example, in Starlette/FastAPI, you can add middleware to a `Router` or `Mount` instead of the main application object.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'brotli_asgi'
The `brotli-asgi` package has not been installed in your Python environment or the Python interpreter cannot find it in its search path.
fix
Install the package using pip: `pip install brotli-asgi`
ImportError: cannot import name 'BrotliMiddleware' from 'brotli_asgi'
The `BrotliMiddleware` class is not found within the `brotli_asgi` module. This usually happens due to a typo in the import statement or an incorrect version of the library where the class name or structure might have changed.
fix
Ensure your import statement is `from brotli_asgi import BrotliMiddleware` and that you are using a compatible version of the library. If you suspect a version issue, try upgrading: `pip install --upgrade brotli-asgi`
AttributeError: module 'brotli' has no attribute 'error'
This error typically occurs when there's an incompatibility or a mix-up with the underlying `brotli` compression library (which `brotli-asgi` depends on). It's often seen in environments like Anaconda or when multiple Brotli-related packages (e.g., `brotli`, `brotlipy`, `brotlicffi`) cause conflicts, leading to the `brotli` module not exposing the expected 'error' attribute.
fix
Try to ensure a clean installation of the core `brotli` library. This might involve: `pip uninstall brotli brotlipy brotlicffi` followed by `pip install brotli brotli-asgi`. If using Conda, consider `conda install brotli` or `pip install brotlicffi` if the issue persists after updating Conda itself.
Upgrade
Version history
1.6.0latest on PyPI · released Jan 2, 2026
Audit
Dependencies
starletterequiredRuntime dependency for ASGI application integration.
brotlirequiredCore library for Brotli compression algorithm.
Agent activity
27 hits · last 30 days
node
24
OpenAI (training)
1
Resources
brotli-asgi — pip install brotli-asgi · libregistry