TOON (Token-Oriented Object Notation) Python Library

1.6.0 · active · verified Thu Apr 16

Toonify is a Python implementation of TOON (Token-Oriented Object Notation), a compact, human-readable serialization format designed specifically for use with Large Language Models (LLMs). It aims to reduce token consumption by 30-60% compared to JSON, while preserving data structure, type safety, and human readability. The library provides functions to encode Python dictionaries and Pydantic models into TOON strings and decode them back. It is actively maintained with a regular release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic usage of the `encode` and `decode` functions to convert a Python dictionary containing nested data into a TOON string and then back into a Python dictionary. It showcases how `toonify` automatically uses a compact tabular format for uniform arrays.

from toon import encode, decode

data = {
    "products": [
        {"sku": "LAP-001", "name": "Gaming Laptop", "price": 1299.99},
        {"sku": "MOU-042", "name": "Wireless Mouse", "price": 29.99},
    ]
}

# Encode Python dict to TOON
toon_string = encode(data)
print("Encoded TOON string:\n" + toon_string)
# Expected output for data above:
# products[2]{sku,name,price}:
# LAP-001,Gaming Laptop,1299.99
# MOU-042,Wireless Mouse,29.99

# Decode TOON back to Python
result = decode(toon_string)
assert result == data
print("Decoded Python object:" + str(result))

view raw JSON →