Starlette Cramjam
raw JSON → 0.7.0 verified Mon Apr 27 auth: no python
Middleware that adds compression (gzip, brotli, zstd, deflate) to Starlette ASGI applications using the cramjam library. Version 0.7.0; releases are irregular.
pip install starlette-cramjam Common errors
error ImportError: cannot import name 'CompressMiddleware' from 'starlette_cramjam' ↓
cause The class is in the .middleware submodule, not in __init__.py.
fix
Use: from starlette_cramjam.middleware import CompressMiddleware
error Compression not applied to 204 No Content responses ↓
cause Version 0.7.0 intentionally skips compression for 204 responses to avoid breaking RFC 7231.
fix
If you must compress 204, downgrade to <0.7.0 or modify middleware source.
Warnings
breaking Python 3.9 and 3.10 support dropped in version 0.6.0; requires Python >=3.11. ↓
fix Upgrade Python to 3.11+ or pin to starlette-cramjam <0.6.0.
gotcha The middleware may skip compression for small responses based on default minimum size (min_size). Responses smaller than 500 bytes are not compressed. ↓
fix Set min_size=0 in CompressMiddleware(min_size=0) to compress all responses.
Imports
- CompressMiddleware wrong
from starlette_cramjam import CompressMiddlewarecorrectfrom starlette_cramjam.middleware import CompressMiddleware
Quickstart
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route
from starlette_cramjam.middleware import CompressMiddleware
async def hello(request):
return PlainTextResponse("Hello, World!")
app = Starlette(routes=[Route('/', hello)])
app.add_middleware(CompressMiddleware)