TOON Format for Python

0.9.0-beta.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to encode Python dictionaries and lists into TOON format and decode TOON strings back into Python objects using `toon_format.encode` and `toon_format.decode` functions.

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']}

view raw JSON →