starlette-compress
Compression middleware for Starlette - supporting ZStd, Brotli, and GZip
What it is and what it does
starlette-compress is a middleware layer for Starlette and FastAPI applications that automatically compresses HTTP responses using one of three algorithms: ZStd, Brotli, or GZip. It sits between your application and the client, intercepting responses and reducing their size before transmission. The middleware applies sensible defaults—compressing responses 500 bytes or larger, using compression level 4 for all algorithms—while remaining configurable for different minimum sizes, compression levels, and content types.
The package handles both standard request-response flows and streaming scenarios like Server-Sent Events (SSE), where it flushes the compressor after each message to maintain low latency. It works with asyncio and trio backends, supports modern Python versions (3.9 through 3.14), and requires four runtime dependencies: starlette itself, plus brotli, brotlicffi, and zstandard for the respective compression algorithms.
Use it for:
- Reduce bandwidth on Starlette or FastAPI APIs by automatically compressing JSON, HTML, and text responses
- Add ZStd or Brotli compression to applications that only have GZip available in Starlette's built-in middleware
- Compress Server-Sent Events (SSE) streams with per-message flushing to maintain real-time delivery
- Tune compression levels per algorithm to balance file size reduction against CPU cost for specific workloads
- Manage compression for custom content types or opt out of compression for specific response types
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Middleware for Starlette and FastAPI that compresses HTTP responses using ZStd, Brotli, or GZip, with sensible defaults and more algorithm options than Starlette's built-in GZipMiddleware.
Yes. The package is actively maintained, has no known vulnerabilities, uses a permissive license, and offers a straightforward drop-in middleware for Starlette/FastAPI with more compression options and better defaults than the built-in alternative. Low install friction and broad Python version support make it a practical choice for any Starlette or FastAPI project needing response compression.
Install
starlette-compress on PyPI
pip
pip install starlette-compressuv
uv add starlette-compresspoetry
poetry add starlette-compressInstalling starlette-compress
Before you install
Low install friction with four runtime dependencies (brotli, brotlicffi, starlette, zstandard). Actively maintained with a release 27 days ago and recent commits. Supports Python 3.9 through 3.14.
License in practice
Licensed under 0BSD (Zero-Clause BSD), a permissive public domain dedication with no restrictions on use, modification, or distribution.
Quickstart
pip install starlette-compress
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_compress import CompressMiddleware
middleware = [Middleware(CompressMiddleware)]
app = Starlette(routes=..., middleware=middleware)
Verify before relying
- Performance impact compared to Starlette's built-in GZipMiddleware under typical production loads
- Whether all four runtime dependencies (brotli, brotlicffi, starlette, zstandard) are always required or conditionally installed
Package facts
| License | 0BSD (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — brotli, brotlicffi, starlette, zstandard |
| Maintenance | actively maintained — 27 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,114,521/month — #4,351 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: starlette_compress-1.8.0-py3-none-any.whl
Keywords: asgi, brotli, compression, deflate, encoding, fastapi, gzip, http, middleware, performance, response, starlette, web, zstandard, zstd
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
brotli-asgiBrotliMiddleware adds Brotli compression to…
permissive · top 15,000 on PyPI
starlette-cramjamMiddleware for Starlette that compresses HTTP…
permissive · top 15,000 on PyPI
starlette-contextMiddleware for Starlette that stores and…
permissive · top 5,000 on PyPI
Flask-CompressFlask-Compress automatically compresses Flask…
permissive · top 5,000 on PyPI
starletteStarlette is a lightweight ASGI framework for…
permissive · top 100 on PyPI
SecwebSecweb applies security headers (CSP, HSTS,…
copyleft · top 15,000 on PyPI
starlette-csrfStarlette middleware that protects web…
permissive · top 15,000 on PyPI
fastapi-eventsFastapi-events is an event dispatching and…
permissive · top 15,000 on PyPI
starsessionsProvides session management middleware for…
permissive · top 15,000 on PyPI
whitenoiseWhiteNoise serves static files directly from a…
permissive · top 5,000 on PyPI