Skip to main content

TOON Format (Python)

TOON is a compact, reversible encoding of JSON designed to reduce LLM prompt tokens. This implementation focuses on a minimal, deterministic syntax and fast encode/decode.

Core Syntax

  1. Objects use a key list and a value list: {k1,k2|v1|v2}
  2. Arrays use pipe separators: [v1|v2|v3]
  3. Tables (uniform dict arrays) use CSV blocks: ^csv[k1,k2|r1c1,r1c2|r2c1,r2c2]

TOON is best for mixed or nested data. Pure tabular data is still smaller in raw CSV.

Quick Start

from toon_format import encode, decode, encode_best, encode_as, convert_format

data = {"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}

print(encode(data))
print(decode(encode(data)))
print(encode_best(data))
print(encode_as(data, "json"))
print(convert_format(encode(data), "yaml"))

Installation

  1. Install the package from PyPI:
pip install p-toon-llm
  1. Optional dev tools for build and upload:
pip install -e .[dev]

Auto Mode

  1. Pick the smallest format automatically (tokens by default):
best = encode_best(data, candidates=("toon", "json", "yaml", "csv"))
print(best["format"], best["text"])
  1. Or use auto mode inside encode():
print(encode(data, options={"mode": "auto", "candidates": ("toon", "json", "csv")}))

Benchmarks

This repository provides tooling to measure token savings locally so you can benchmark your own data. Run the demo or use compare_formats for a quick comparison, then expand to your real datasets.

Example workflow:

  1. Start with a representative dataset that matches your production structure.
  2. Compare TOON vs JSON compact, JSON pretty, YAML, and CSV.
  3. Measure both tokens and chars, and pick the best format for your use case.
from toon_format import compare_formats, estimate_savings

print(compare_formats(data))
print(estimate_savings(data))

If you want to publish benchmark results in this README, add your measured numbers and dataset description.

Format Examples

Example object:

{name,age|Alice|30}

Example array:

[1|2|3]

Example table:

^csv[id,name|1,Alice|2,Bob]

Layout

src/
  encoder.py
  decoder.py
  detect.py
  tokens.py
  compare.py
  formats.py
  convert.py
  toon_format.py

Docs

  1. docs/index.md
  2. docs/format.md
  3. docs/api.md
  4. docs/performance.md
  5. docs/prompt_header.md

Tests

python -m pytest

Demo

python examples/demo.py

Metadata

Release files for p-toon-llm 0.1.1

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

Source distribution (sdist)

Source distribution for p-toon-llm 0.1.1
File Size Uploaded Provenance
p_toon_llm-0.1.1.tar.gz 12.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for p-toon-llm 0.1.1
File Interpreter ABI Platform
p_toon_llm-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.2 kB

Release files / p_toon_llm-0.1.1.tar.gz

Download URL p_toon_llm-0.1.1.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d71cfebacd86bb7e97c9072bdf6008a1f08388f9645ae90c32bbff9dd671bad3
BLAKE2b-256 checksum
How to use checksums
82ddb655b7102c92e2293b5687233ebf77fe92bf0678c4704135f7ddb694df61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / p_toon_llm-0.1.1-py3-none-any.whl

Download URL p_toon_llm-0.1.1-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aceddc811c6b31ed69875cd27982fd27585599af6f858fd712d7c68783de42af
BLAKE2b-256 checksum
How to use checksums
ae04189b0082e75fcba6f70fe5874090f34192bbc581adeb9038b9494cad67af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Security for p-toon-llm 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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