Skip to main content

flatwire (Python)

Streaming JSON serialization that keeps memory flat and time linear — stream large collections element-by-element instead of materializing the whole payload.

pip install flatwire
import flatwire

# Whole-value convenience (byte-compatible with the standard library).
data = flatwire.encode({"hello": "world"})
value = flatwire.decode(data)

# Stream a large collection to a binary writer, one element at a time.
with open("out.json", "wb") as fp:
    flatwire.encode_array((row for row in huge_iterator), fp)

# Read it back lazily — peak memory is one element, not the whole array.
with open("out.json", "rb") as fp:
    for row in flatwire.decode_array(fp):
        handle(row)

Wire format is plain JSON, so nothing downstream changes.

Speed

Streaming decode is built on the standard library's C-accelerated JSON parser (raw_decode), so it stays close to native speed while still yielding one element at a time — roughly 2× json.loads on large arrays, not the order-of-magnitude penalty a pure-Python scanner would pay, with ~99% less peak memory. Encode can match orjson too:

pip install flatwire[fast]   # pulls in orjson; used automatically when present

With the fast extra installed, per-element encoding routes through orjson (~1.5× a bulk orjson.dumps) while keeping memory flat. It's entirely optional — the default install has zero dependencies. See bench/REPORT.md for the full measured comparison.

Checked streams (partial-stream failure semantics)

When you stream a large array over HTTP you have already sent 200 OK before an error can occur. Checked streams wrap the collection in an envelope whose terminal status is written last, so the consumer can tell clean completion, an in-band producer error after N rows, and truncation apart:

from flatwire import encode_checked_array, decode_checked_array, StreamError, TruncatedStream

with open("out.json", "wb") as fp:
    encode_checked_array(rows, fp)          # writes ...,"complete":true} last

with open("out.json", "rb") as fp:
    try:
        for row in decode_checked_array(fp):
            handle(row)
    except StreamError as e:        # producer failed after N rows (details on the wire)
        ...
    except TruncatedStream:         # stream ended without a terminal status
        ...

The envelope is plain JSON, so a checked stream written in any flatwire language decodes in every other. See docs/FAILURE.md.

Streaming XML

The same API streams a typed, fully round-trippable XML representation — for which the standard DOM parsers would otherwise build the whole tree in memory:

# Stream a large collection as XML.
with open("out.xml", "wb") as fp:
    flatwire.encode_array(rows, fp, format="xml")        # optional: root="items"

# Parse it back lazily, one element at a time (iterparse under the hood).
with open("out.xml", "rb") as fp:
    for row in flatwire.decode_array(fp, format="xml"):
        handle(row)

Measured (bench/xml_bench.py): parsing a 12 MB document with ElementTree.fromstring peaks at ~125 MB, while flatwire's streaming parse holds ~4 MB (~97% lower); streaming encode is flat at ~900 bytes.

Binary formats (MessagePack & CBOR)

The same streaming pair also speaks two compact binary wires, selected with format=. Both use a canonical encoding, so the bytes are identical across all six flatwire languages:

# MessagePack
flatwire.encode_array(rows, fp, format="msgpack")
for row in flatwire.decode_array(fp, format="msgpack"):
    handle(row)

# CBOR (RFC 8949)
flatwire.encode_array(rows, fp, format="cbor")
for row in flatwire.decode_array(fp, format="cbor"):
    handle(row)

See the monorepo README for the cross-language story, and the live benchmark dashboard for the measured numbers.

Command-line tool

Installing the package also installs a flatwire CLI — a streaming Swiss-army knife that processes files of any size in flat memory, across all four wire formats:

flatwire cat data.json            # stream elements, one JSON line each
flatwire convert data.json data.cbor   # stream-convert json -> cbor
flatwire stats data.cbor          # element count, throughput, largest element

See docs/CLI.md for the full reference, and docs/RECIPES.md for production patterns (S3→Postgres, Kafka/NATS, LLM token streams).

Apache-2.0.

Metadata

Release files for flatwire 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flatwire 1.1.0
File Size Uploaded
flatwire-1.1.0.tar.gz 24.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flatwire 1.1.0
File Interpreter ABI Platform
flatwire-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 45.5 kB

Release files / flatwire-1.1.0.tar.gz

Download URL flatwire-1.1.0.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f43502540cd08b3ffea516c7f98070db636bf70e45582b491aa6d1c76b24a241
BLAKE2b-256 checksum
How to use checksums
bac5d411efd990a75adaf8d2bbc8491134ffc4c4e733250892d73be02825734a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / flatwire-1.1.0-py3-none-any.whl

Download URL flatwire-1.1.0-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04c03691cac42b724228213d2a60409889cd7b3ea10e2759222db7db5ddd5a85
BLAKE2b-256 checksum
How to use checksums
1d7e339747b5e4edcc09ede4c8028357146ab8f018e5fdce704e3938ec46a011
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page