hpack
A pure-Python implementation of the HPACK header compression algorithm used by HTTP/2
Repository Health
Technical Analysis
hpack implements HPACK, the header-compression scheme HTTP/2 requires to shrink the overhead of sending largely repetitive HTTP headers on every request. It provides an Encoder and Decoder pair that maintain the dynamic table and Huffman coding state HPACK depends on, translating between Python header lists and the compact binary representation that goes on the wire.
As one of the three python-hyper sibling libraries (alongside hyperframe and h2), hpack is a low-level, single-purpose building block: h2 depends on it directly to compress and decompress headers as part of implementing the full HTTP/2 protocol, and it can be used standalone by anything else that needs to speak HPACK.
What You Get
- An
Encoderclass that compresses a list of header name/value pairs into HPACK’s binary wire format, including dynamic table management - A
Decoderclass that reverses the process, turning received HPACK-encoded bytes back into header lists - A full Huffman coding table implementation (
huffman.py,huffman_constants.py,huffman_table.py) matching the HTTP/2 spec’s static Huffman code - Dedicated
exceptions.pytypes for HPACK-specific error conditions (decoding errors, table size violations) - Used directly by h2 as its HPACK codec, so any project consuming h2 already depends on hpack transitively
Common Use Cases
- Implementing HTTP/2 header compression/decompression as part of a custom HTTP/2 client or server (typically via h2 rather than hpack directly)
- Building tooling that inspects or manipulates raw HTTP/2 traffic and needs to decode HPACK-compressed header blocks
- Writing HPACK conformance tests or fuzzers against the encoder/decoder pair in isolation from the rest of HTTP/2
- Any protocol implementation outside HTTP/2 that chooses to reuse HPACK’s compression scheme for header-like data
Under The Hood
Architecture - hpack.py exposes the public Encoder/Decoder classes, table.py implements the dynamic header table with its eviction and size-accounting rules, struct.py handles HPACK’s variable-length integer encoding, and the huffman*.py modules implement the static Huffman code tables and encode/decode logic as a self-contained unit. Tech Stack - Pure Python with no external runtime dependencies, keeping it trivially embeddable in any project (h2 included) without pulling in additional transitive dependencies. Code Quality - 9 test files cover the encoder, decoder, dynamic table behavior, and Huffman coding paths, with CI and Codecov badges tracked in the README; the small, focused module layout (8 files total) keeps each concern isolated and easy to audit. API Design - The Encoder().encode(headers) / Decoder().decode(data) pair is a minimal, purpose-built API with almost no configuration surface beyond table size, matching HPACK’s narrow scope as a header-compression codec rather than a general-purpose library.
Used by 2 apps in this directory
Agno
Devops · AI Development · Automation
Build, run, and manage agent platforms with a full production stack — SDK, runtime, and control plane included.
SWIRL
Search · Databases · Data Engineering
Federated AI search and RAG across 100+ enterprise sources—no data extraction, no vector database required.