Skip to main content

Token-oriented object notation (TOON) for compact, reversible JSON encoding

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

p_toon_llm-0.1.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

p_toon_llm-0.1.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file p_toon_llm-0.1.1.tar.gz.

File metadata

  • Download URL: p_toon_llm-0.1.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for p_toon_llm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d71cfebacd86bb7e97c9072bdf6008a1f08388f9645ae90c32bbff9dd671bad3
MD5 91f614f714ee5260f1336fd66dcff638
BLAKE2b-256 82ddb655b7102c92e2293b5687233ebf77fe92bf0678c4704135f7ddb694df61

See more details on using hashes here.

File details

Details for the file p_toon_llm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: p_toon_llm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for p_toon_llm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aceddc811c6b31ed69875cd27982fd27585599af6f858fd712d7c68783de42af
MD5 4b6d2b8297ad2085a3af15555f136ec4
BLAKE2b-256 ae04189b0082e75fcba6f70fe5874090f34192bbc581adeb9038b9494cad67af

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page