skillfed

starlette-compress

Compression middleware for Starlette - supporting ZStd, Brotli, and GZip

starlette-compress v1.8.0 1.1M downloads/30d#4,351 on PyPI3
Permissive license 0BSD Active released

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-compress

uv

uv add starlette-compress

poetry

poetry add starlette-compress

Installing 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

Development Status :: 5 - Production/StableEnvironment :: Web EnvironmentFramework :: AnyIOFramework :: AsyncIOFramework :: FastAPIFramework :: TrioIntended Audience :: DevelopersLicense :: OSI Approved :: Zero-Clause BSD (0BSD)Operating System :: OS IndependentProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Topic :: Internet :: WWW/HTTP :: Dynamic ContentTopic :: Internet :: WWW/HTTP :: WSGI :: MiddlewareTopic :: Software Development :: Libraries :: Python ModulesTyping :: Typed

Tags

starlette response compression middlewarefastapi gzip brotli zstd compressionhttp response compression asgistarlette middleware compressionzstandard brotli gzip middlewareasgi response encodingweb response compression
middlewarecompressionasgi

More Python Modules packages