# Python 3.14 Adds compression.zstd for Zstandard Support
> Python 3.14 adds a native Zstandard module under compression.zstd, unifying high-performance zstd support and existing stdlib compressors under one namespace.

Canonical: https://blog.abhimanyu-saharan.com/posts/python-3-14-adds-compression-zstd-for-zstandard-support
Published: 2025-05-22
Last updated: 2025-06-19
Authors: Abhimanyu Saharan
Categories: Python 3.14, Python

With Python 3.14, the standard library gains a first-party wrapper for Zstandard (zstd), a modern compression algorithm renowned for its high compression ratios and rapid decompression speeds. To avoid clashing with existing PyPI packages named `zstd` or `zstandard`, PEP 784 consolidates all compression modules under a single `compression` namespace, while preserving the legacy imports you already use.

### Why Zstandard?

Zstandard has emerged as the industry standard for performance-sensitive compression. Benchmarks consistently show:

- **Higher ratio** than zlib (DEFLATE) and bzip2
- **Faster decompression** than lzma
- **Hardware and filesystem support**, including ZFS and Btrfs

Projects from Conda to network protocols now rely on zstd. By bundling it into the standard library, Python enables faster installs, smaller archives, and consistent APIs without external dependencies.

### A Unified `compression` Package

Rather than shadow existing modules, PEP 784 introduces:

```python
# Python 3.14+
from compression.zstd  import compress, decompress, ZstdFile
from compression.bz2   import BZ2Compressor
from compression.lzma  import LZMAFile
from compression.zlib  import compressobj

# Legacy imports remain valid:
import gzip, bz2, lzma, zlib
```

This structure prevents naming collisions with third-party packages (`zstd`, `zstandard`) and lays the groundwork for future additions (e.g., LZ4) without import conflicts.

### One-Shot Compression

The one-shot API matches existing patterns:

```python
from compression.zstd import compress, decompress

raw       = b"example data"
zipped    = compress(raw, level=5)
assert decompress(zipped) == raw
```

### Streaming & File Interfaces

Incremental compression and file wrappers mirror the stdlib design:

```python
from compression.zstd import ZstdCompressor, ZstdDecompressor

# Prepare some test data
full_data   = b"Hello, world! " * 10_000  # ~140 KiB of data
chunk_size  = 64 * 1024                  # 64 KiB
data_chunks = [full_data[i : i+chunk_size]
               for i in range(0, len(full_data), chunk_size)]

# Incremental compression
compressor = ZstdCompressor(level=3)
chunks     = [compressor.compress(chunk) for chunk in data_chunks]
chunks.append(compressor.flush())

# Incremental decompression
decompressor = ZstdDecompressor()
reassembled = b"".join(decompressor.decompress(part) for part in chunks)

assert reassembled == full_data
print("Round-trip successful, size compressed →", sum(len(c) for c in chunks))
```

**Output:**

```
Round-trip successful, size compressed → 42
```

```python
from compression.zstd import ZstdFile, ZstdCompressor, ZstdDecompressor

# 1. Prepare some “large_bytes” (e.g. ~100 KiB of repeating text)
large_bytes = (b"Python PEP 784: Zstd in stdlib! " * 1_000)[:100_000]

# 2. Compress to disk
with ZstdFile("example.zst", "wb", level=10) as out:
    out.write(large_bytes)

# 3. Read it back
with ZstdFile("example.zst", "rb") as inp:
    restored = inp.read()

assert restored == large_bytes
print(f"Success: wrote and read back {len(restored)} bytes.")
```

**Output:**

```
Success: wrote and read back 32000 bytes.
```

### Build-Time Detection & Legacy Support

- **Unix builds** probe for `libzstd`; absence omits the module.
- **Windows installers** vendor `libzstd` for out-of-the-box support.
- **Top-level imports** (`gzip`, `bz2`, `lzma`, `zlib`) continue unchanged.
- **Dual-version compatibility** is achieved via a simple fallback:

```python
try:
    from compression.lzma import LZMAFile
except ImportError:
    from lzma import LZMAFile
```

### Security & Quality Assurance

All new C extensions undergo AddressSanitizer and libFuzzer testing. The upstream zstd library is itself well-fuzzed and covered by a bug-bounty program, minimizing memory-safety risks.

### Final Thoughts

By integrating Zstandard under a clear, conflict-free namespace and extending archive, streaming, and dictionary APIs, Python 3.14 empowers developers with a high-performance compression toolkit, while ensuring existing code and imports remain fully supported.

## FAQ

### What is Zstandard and why is it added to Python 3.14?

Zstandard (zstd) is a modern compression algorithm offering **high compression ratios** and **fast decompression speeds**. Python 3.14 adds it to the standard library to support **performance-sensitive use cases** and eliminate the need for external dependencies when using zstd.

### How is Zstandard integrated into Python’s standard library?

Zstandard is added under the new `compression` namespace as `compression.zstd`, per **PEP 784**. This avoids naming conflicts with third-party PyPI packages like `zstd` or `zstandard`, and paves the way for other algorithms (like LZ4) under the same namespace.

### Can I still use the existing compression modules like gzip or lzma?

Yes. Existing top-level imports such as `gzip`, `bz2`, `lzma`, and `zlib` remain **unchanged and fully supported**. The `compression` namespace is an addition, not a replacement.

### What APIs does compression.zstd provide?

- **One-shot APIs** for simple `compress()` and `decompress()` operations
- **Streaming interfaces** (`compressobj`, `decompressobj`) for chunked data
- **File wrappers** for working with `.zst` files using familiar I/O patterns

### How is compatibility and security handled in this new module?

- Python on Windows ships with vendored `libzstd`
- Unix builds detect `libzstd` at build time
- The C extension is tested with **AddressSanitizer**, **libFuzzer**, and benefits from upstream **bug bounty** coverage for `zstd`
- Dual-version compatibility can be maintained with fallback imports if needed
