Note: This is an unofficial community implementation. The official TOON projects live in the toon-format organization, including the specification and the official Python implementation toon-format/toon-python.
Token-Oriented Object Notation for Python
A compact data format optimized for transmitting structured information to Large Language Models (LLMs) with 30-60% fewer tokens than JSON.
Installation
pip install python-toon
What is TOON?
TOON (Token-Oriented Object Notation) combines YAML's indentation-based structure for nested objects and CSV's tabular format for uniform data rows, optimized specifically for token efficiency in LLM contexts.
Spec compliance:toon-spec: 4.1. The encoder and decoder pass the full language-agnostic fixture suite of the official TOON specification (vendored in tests/fixtures/).
Key Features
30-60% token reduction compared to standard JSON on uniform data
Minimal syntax: Eliminates redundant punctuation (braces, brackets, most quotes)
Tabular arrays: CSV-like rows for uniform object collections, including nested uniform objects
Keyed tables: Objects whose values share one shape collapse into a single table
Explicit metadata: Array length indicators [N] for validation
Bidirectional: encode and decode, with strict validation by default
Indentation: must be a multiple of indent, no tabs, no unexpected depth jumps
Structure: no blank lines inside arrays, no duplicate keys, no trailing content after a root array
Set strict=False for lenient parsing (duplicate keys resolve last-write-wins, count mismatches are accepted, malformed headers fall back to plain keys).
Numbers: integers decode to int (arbitrary precision). Tokens with a fraction or exponent decode to float, except integral values below 2^53, which decode to int (2.5e2 → 250). Tokens with leading zeros such as 05 decode as strings.
The comma is the default and is never written in the header. Tab and pipe are declared inside the brackets and used for the field list and rows:
items[2|]{sku|qty}:
A1|2
B2|1
Comments
The decoder ignores full-line comments, meaning lines whose first non-space character is #. The encoder never writes comments, and it always quotes strings that start with #.
Quoting Rules
Strings are quoted only when necessary:
Empty strings
Keywords: null, true, false
Numeric-like strings: 42, -3.14, 05, 1e6
Leading or trailing whitespace
Contains :, ", \, [, ], {, } or control characters
Contains the active delimiter (,, |, or tab)
Starts with - or #
"hello" # hello (no quotes)
"hello world" # hello world (internal spaces OK)
" hello" # " hello" (leading space requires quotes)
"null" # "null" (keyword)
"42" # "42" (looks like number)
"#tag" # "#tag" (would read as a comment)
"" # "" (empty)
Type Conversions
Non-JSON types are normalized automatically:
Numbers: Plain decimal form for 1e-6 ≤ |n| < 1e21, exponent form (1e-7, 1e+21) outside that range
Infinity/NaN: Converted to null
-0: Normalized to 0
Decimal: Converted to int if integral, otherwise float
datetime / date / time: ISO 8601 strings via .isoformat()
set / frozenset / tuple: Converted to arrays
Mappings: Converted to objects, with keys coerced to strings
Dataclasses: Converted to objects via dataclasses.asdict()
Functions/Callables: Converted to null
Strings with unpaired surrogates raise ValueError.
LLM Integration Best Practices
When using TOON with LLMs:
Wrap in code blocks for clarity:
```toon
name: Alice
age: 30
```
Instruct the model about the format:
"Respond using TOON format (Token-Oriented Object Notation). Use key: value syntax, indentation for nesting, and tabular format [N]{fields}: for uniform arrays."
Use array lengths for validation: tell the model "Array lengths are declared as [N]. Ensure your response matches these counts." Then decode the response in strict mode.
Acknowledge tokenizer variance: Token savings depend on the tokenizer, the model and above all the shape of your data. Deeply nested, non-uniform documents can use as many tokens as compact JSON, or more.
This project uses uv for fast, reliable package and environment management.
Setup with uv (Recommended)
# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone the repository
git clone https://github.com/xaviviro/python-toon.git
cd python-toon
# Create virtual environment and install dependencies
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install package in editable mode with dev dependencies
uv pip install -e ".[dev]"
Setup with pip (Alternative)
# Clone the repository
git clone https://github.com/xaviviro/python-toon.git
cd python-toon
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e .
# Install development dependencies
pip install -r requirements-dev.txt
Running Tests
# Run all tests (includes the official spec fixtures in tests/fixtures/)
pytest
# Run only the spec conformance suite
pytest tests/test_spec_fixtures.py
# Run with coverage
pytest --cov=toon --cov-report=term
Type Checking
mypy src/toon
Linting
ruff check src/toon tests
Credits
This project is an unofficial Python implementation of the TOON format. The conformance fixtures in tests/fixtures/ come from the TOON specification (MIT License).