Compression middleware for Starlette - supporting ZStd, Brotli, and GZip
Project description
starlette-compress
starlette-compress is a fast and simple middleware for compressing responses in Starlette. It supports more compression algorithms than Starlette's built-in GZipMiddleware, and has more sensible defaults.
- Python 3.9+ support
- Python 3.14
compression.zstdsupport - Compatible with
asyncioandtriobackends - ZStd, Brotli, and GZip compression
- Sensible default configuration
- Zero-Clause BSD — public domain dedication
- Semantic Versioning compliance
Installation
pip install starlette-compress
Basic Usage
Starlette
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_compress import CompressMiddleware
middleware = [
Middleware(CompressMiddleware)
]
app = Starlette(routes=..., middleware=middleware)
FastAPI
You can use starlette-compress with FastAPI too:
from fastapi import FastAPI
from starlette_compress import CompressMiddleware
app = FastAPI()
app.add_middleware(CompressMiddleware)
Advanced Usage
Changing Minimum Response Size
Control the minimum size of the response to compress. By default, responses must be at least 500 bytes to be compressed. Streaming-registered types (e.g. text/event-stream) bypass this threshold and are always compressed when accepted.
# Starlette
middleware = [
Middleware(CompressMiddleware, minimum_size=1000)
]
# FastAPI
app.add_middleware(CompressMiddleware, minimum_size=1000)
Tuning Compression Levels
Adjust the compression levels for each algorithm. Higher levels mean smaller files but slower compression. Default level is 4 for all algorithms.
# Starlette
middleware = [
Middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)
]
# FastAPI
app.add_middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)
Supporting Custom Content-Types
Manage the supported content-types. Unknown response types are not compressed. Check here for the default configuration.
from starlette_compress import add_compress_type, remove_compress_type
add_compress_type("application/my-custom-type")
remove_compress_type("application/json")
Server-Sent Events (SSE)
text/event-stream responses are compressed by default. The middleware flushes the compressor after each non-empty ASGI body message (it does not parse SSE frames — two events in one message share one flush; empty continuation messages are skipped). Streaming types bypass minimum_size. The middleware forwards http.response.start immediately so clients such as EventSource can open before the first event — though ASGI servers may still buffer that start until the first body. On pathsend-capable scopes, start is deferred to the first content message (body or pathsend) so a file send is never advertised as compressed.
To opt out:
from starlette_compress import remove_compress_type
remove_compress_type("text/event-stream")
NDJSON / JSONL are not registered by default (they are neither compressed nor treated as streaming until added). Opt in when you need compression and/or low-latency line delivery:
from starlette_compress import add_compress_type
add_compress_type("application/x-ndjson", streaming=True)
add_compress_type("application/jsonl", streaming=True)
Flush guarantees that compressed bytes are available from the codec after each non-empty message; it does not control proxy or CDN buffering (this package does not set X-Accel-Buffering or similar).
Dropping Accept-Encoding Header
By default, starlette-compress leaves the request Accept-Encoding header intact. Set
remove_accept_encoding=True to remove it before calling downstream middleware/application.
Project details
Release history Release notifications | RSS feed
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 starlette_compress-1.8.0.tar.gz.
File metadata
- Download URL: starlette_compress-1.8.0.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dc0077468a2276b6dfd1b4f946fb61b5465a41c996c010486248e65af15b4b6
|
|
| MD5 |
a30b10c0ccd69a06dff32a757d13d113
|
|
| BLAKE2b-256 |
aea4bf7bb0dec4aa4541e7523a9d40cf10002d775449e111306959898a935d1c
|
Provenance
The following attestation bundles were made for starlette_compress-1.8.0.tar.gz:
Publisher:
ci.yaml on Zaczero/pkgs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
starlette_compress-1.8.0.tar.gz -
Subject digest:
4dc0077468a2276b6dfd1b4f946fb61b5465a41c996c010486248e65af15b4b6 - Sigstore transparency entry: 2192514262
- Sigstore integration time:
-
Permalink:
Zaczero/pkgs@1f1d989bb5744efe8b94f6aefb643b8dee95f71f -
Branch / Tag:
refs/tags/starlette-compress/1.8.0 - Owner: https://github.com/Zaczero
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@1f1d989bb5744efe8b94f6aefb643b8dee95f71f -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file starlette_compress-1.8.0-py3-none-any.whl.
File metadata
- Download URL: starlette_compress-1.8.0-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c991e21d31c7f845a004055db9ae3c7e79ed59cac34c8444e9e7e689456b623
|
|
| MD5 |
c92ffc662af5f96a031873e29c05a0f5
|
|
| BLAKE2b-256 |
749a8eb13069f311042b0f44b5b3a5c59e50d265863b323757ab51a84564f65f
|
Provenance
The following attestation bundles were made for starlette_compress-1.8.0-py3-none-any.whl:
Publisher:
ci.yaml on Zaczero/pkgs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
starlette_compress-1.8.0-py3-none-any.whl -
Subject digest:
3c991e21d31c7f845a004055db9ae3c7e79ed59cac34c8444e9e7e689456b623 - Sigstore transparency entry: 2192514281
- Sigstore integration time:
-
Permalink:
Zaczero/pkgs@1f1d989bb5744efe8b94f6aefb643b8dee95f71f -
Branch / Tag:
refs/tags/starlette-compress/1.8.0 - Owner: https://github.com/Zaczero
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@1f1d989bb5744efe8b94f6aefb643b8dee95f71f -
Trigger Event:
workflow_dispatch
-
Statement type: