TOON Format for Python
TOON (Token-Oriented Object Notation) is a compact, human-readable data format designed for LLM prompts to reduce token usage by 30-60% compared to JSON. It achieves this by eliminating redundant punctuation and using a tabular format for uniform data structures. The Python implementation, currently in beta (v0.9.0-beta.1), provides encoding and decoding functionalities, aiming for full compliance with the TOON specification.
Common errors
-
ModuleNotFoundError: No module named 'toon_format'
cause You likely installed the package using `pip install toon-format`, but the Python import path uses underscores, not hyphens.fixEnsure your import statement is `from toon_format import encode, decode` (using an underscore in `toon_format`) after installing `pip install toon-format`. -
SyntaxError: Invalid TOON format: Array length mismatch. Expected 3 items, got 2.
cause The TOON string you are trying to decode has an array length declared (e.g., `items[3]`) that does not match the actual number of items provided in the data rows.fixCorrect the TOON string so that the declared array length `[N]` precisely matches the number of data rows, or set `strict=False` during decoding (e.g., `decode(toon_string, options={'strict': False})`) to allow lenient parsing. -
ValueError: Invalid TOON format: Indentation error.
cause TOON strictly enforces 2-space indentation for nested objects, similar to YAML. Inconsistent or incorrect indentation will result in a parsing error.fixReview your TOON string to ensure all nested objects use exactly 2 spaces per indentation level and are consistent throughout the document. Avoid using tabs for indentation.
Warnings
- breaking The library is currently in beta (v0.9.x), and its API may change significantly before the 1.0.0 stable release.
- gotcha TOON is primarily optimized for uniform arrays of objects (tabular data) and may not always yield significant token savings or be ideal for deeply nested or highly non-uniform JSON structures.
- gotcha There are multiple Python implementations related to TOON (e.g., `py-toon-format`, `python-toon`, `toons`), leading to a fragmented ecosystem. Ensure you are using the official `toon-format/toon-python` implementation for the most up-to-date and spec-compliant version.
- gotcha The `decode` function operates in 'strict' mode by default, which means it will raise errors for any syntax inconsistencies, array length mismatches, or delimiter issues in the TOON string.
Install
-
pip install toon-format -
pip install toon-format[openai] -
pip install git+https://github.com/toon-format/toon-python.git
Imports
- encode
from toon_format.encoder import encode
from toon_format import encode
- decode
from toon_format.decoder import decode
from toon_format import decode
- load, dump
from toon_format.io import load, dump
from toon_format import load, dump
Quickstart
from toon_format import encode, decode
# Encode a simple Python dictionary to TOON
data_object = {"name": "Alice", "age": 30}
toon_object = encode(data_object)
print(f"\nEncoded Object:\n{toon_object}")
# Expected output:
# name: Alice
# age: 30
# Encode a list of uniform dictionaries (tabular array)
data_list = [
{"id": 1, "name": "Widget", "price": 9.99},
{"id": 2, "name": "Gadget", "price": 19.99}
]
toon_list = encode(data_list)
print(f"\nEncoded List:\n{toon_list}")
# Expected output:
# [2]{id,name,price}:
# 1,Widget,9.99
# 2,Gadget,19.99
# Decode TOON back to Python objects
toon_string_to_decode = """
items[2]: apple,banana
"""
decoded_data = decode(toon_string_to_decode)
print(f"\nDecoded Data:\n{decoded_data}")
# Expected output: {'items': ['apple', 'banana']}