TOON Python API
TOON (Token-Oriented Object Notation) is a compact, human-readable data format designed to reduce token usage when passing data to large language models. Compared to JSON format, TOON can reduce token usage by 30-60%.
This project provides a Python API library with a Rust backend, delivering high-performance Python bindings through PyO3.
Features
- ✅ Encode and Decode: Bidirectional conversion between Python objects and TOON format
- ✅ Table Format Optimization: Automatically detects uniform object arrays and compresses them using table format
- ✅ Multiple Array Formats: Supports inline arrays, table arrays, list arrays, and arrays of arrays
- ✅ Nested Structures: Full support for nested objects and arrays
- ✅ Custom Options: Supports custom indentation, delimiters, and length markers
- ✅ High Performance: Rust backend provides fast encoding/decoding performance
Installation
pip install tost
Requirements:
- Python 3.8+
Development Installation
If you need to install from source or for development:
# Install Rust (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Install maturin
pip install maturin
# Install from source
pip install .
# Or install in development mode (recommended for development)
maturin develop
# Or build wheel files
maturin build --release
Usage Examples
Basic Encoding
from tost import encode
# Simple object
obj = {
"id": 123,
"name": "Ada Lovelace",
"email": "ada@example.com",
"active": True
}
result = encode(obj)
print(result)
# Output:
# id: 123
# name: Ada Lovelace
# email: ada@example.com
# active: true
Table Format Arrays
from tost import encode
# Table format array (auto-optimized)
products = {
"items": [
{"sku": "LAPTOP-15", "qty": 5, "price": 899.99},
{"sku": "MOUSE-BT", "qty": 25, "price": 29.99},
{"sku": "KEYBOARD-MX", "qty": 12, "price": 149.00}
]
}
result = encode(products)
print(result)
# Output:
# items[3]{sku,qty,price}:
# LAPTOP-15,5,899.99
# MOUSE-BT,25,29.99
# KEYBOARD-MX,12,149
Inline Arrays
from tost import encode
# Inline array (primitive type array)
tags = {
"tags": ["javascript", "typescript", "nodejs", "llm"]
}
result = encode(tags)
print(result)
# Output:
# tags[4]: javascript,typescript,nodejs,llm
Nested Structures
from tost import encode
order = {
"orderId": "ORD-2025-001",
"customer": {
"name": "John Smith",
"email": "john@example.com"
},
"items": [
{"product": "Widget A", "quantity": 2, "price": 19.99},
{"product": "Widget B", "quantity": 1, "price": 34.50}
],
"total": 74.48,
"tags": ["priority", "gift-wrap"]
}
result = encode(order)
print(result)
# Output:
# orderId: ORD-2025-001
# customer:
# name: John Smith
# email: john@example.com
# items[2]{product,quantity,price}:
# Widget A,2,19.99
# Widget B,1,34.5
# total: 74.48
# tags[2]: priority,gift-wrap
Decoding
from tost import decode
tost_str = """
id: 123
name: Ada Lovelace
active: true
items[2]{sku,qty}:
A1,2
B2,1
"""
result = decode(tost_str)
print(result)
# Output:
# {
# 'id': 123,
# 'name': 'Ada Lovelace',
# 'active': True,
# 'items': [
# {'sku': 'A1', 'qty': 2},
# {'sku': 'B2', 'qty': 1}
# ]
# }
Custom Options
from tost import encode
obj = {
"items": [
{"sku": "A1", "qty": 2},
{"sku": "B2", "qty": 1}
]
}
# Custom indentation, delimiter, and length marker
result = encode(
obj,
indent=4, # 4-space indentation
delimiter="|", # Use pipe as delimiter
length_marker="#" # Use # as length marker
)
print(result)
# Output:
# items[#2|]{sku|qty}:
# A1|2
# B2|1
API Reference
encode(obj, indent=2, delimiter=",", length_marker=None)
Encode a Python object to TOON format string.
Parameters:
obj: Python object to encode (dict, list, primitive types, etc.)indent(int, optional): Number of spaces per indentation level (default: 2)delimiter(str, optional): Delimiter for array values and table rows (default: ',')length_marker(str, optional): Prefix marker for array length (e.g., '#')
Returns:
str: TOON format string
Examples:
result = encode({"id": 123, "name": "Alice"})
result = encode(obj, indent=4, delimiter="|", length_marker="#")
decode(tost_str)
Decode a TOON format string to Python object.
Parameters:
tost_str(str): TOON format string
Returns:
- Python object (dict, list, or primitive type)
Examples:
obj = decode("id: 123\nname: Alice")
TOON Format Specification
Object Format
key: value
Table Array Format
When all objects in an array have the same keys and all values are primitive types, table format is used:
items[N]{field1,field2,field3}:
value1,value2,value3
value4,value5,value6
Inline Array Format
Primitive type arrays use inline format:
tags[N]: value1,value2,value3
List Format
Mixed or non-uniform arrays use list format:
items[N]:
- value1
- key: value
other: value2
- value3
Array of Arrays Format
pairs[N]:
- [M]: value1,value2
- [M]: value3,value4
Root-Level Arrays
When the root-level value is an array, use a header form without a key name:
[N]{field1,field2}:
value1,value2
value3,value4
Or for primitive type arrays:
[N]: value1,value2,value3
Project Structure
tost/
├── Cargo.toml # Rust workspace configuration
├── pyproject.toml # Python package configuration
├── README.md # Project documentation
├── rust/ # Rust core library
│ ├── Cargo.toml
│ └── src/
│ ├── lib.rs # Main library file (contains PyO3 bindings)
│ ├── encode.rs # TOON encoding implementation
│ └── decode.rs # TOON decoding implementation
└── python/ # Python package
├── src/
│ └── tost/ # Python package
│ ├── __init__.py
│ └── tost.py # Python interface wrapper
└── tests/ # Python tests
└── test_tost.py
Development
Running Tests
# Rust tests
cd rust
cargo test
# Python tests
cd python
pytest tests/
Building
# Development mode
maturin develop
# Release mode
maturin build --release
License
MIT License
References
Language
Metadata
Release files for tost 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tost-0.1.2.tar.gz | 15.2 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| tost-0.1.2-cp38-abi3-win_amd64.whl | CPython 3.8 | abi3 | Windows x86-64 | Details |
| tost-0.1.2-cp38-abi3-win32.whl | CPython 3.8 | abi3 | Windows x86-32 | Details |
| tost-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.8 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| tost-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | CPython 3.8 | abi3 | Linux glibc 2.17+ ARM64 | Details |
| tost-0.1.2-cp38-abi3-macosx_11_0_arm64.whl | CPython 3.8 | abi3 | macOS 11.0+ ARM64 | Details |
| tost-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl | CPython 3.8 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 5.1 MB
Release files / tost-0.1.2.tar.gz
| Download URL | tost-0.1.2.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d63f46e837d233590b5114241abf57bb4c0a998e86058983071b48426d794c2c
|
|
BLAKE2b-256 checksum How to use checksums |
c8700e24ce8eadc362e8b12dfe5843851c6fa1f440a41c6d614b2a546526d09f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Release files / tost-0.1.2-cp38-abi3-win_amd64.whl
| Download URL | tost-0.1.2-cp38-abi3-win_amd64.whl |
|---|---|
| Size | 739.6 kB |
| Tags | CPython 3.8 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
f34b957a1cb081e42aba4f280e5fb2f9ee0be76012efa3d0d0786ed048d2aa3f
|
|
BLAKE2b-256 checksum How to use checksums |
e2cd2484466e97d957f0acc01573042a3e00027ea44f0cdf6fe7620c1464ae9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Release files / tost-0.1.2-cp38-abi3-win32.whl
| Download URL | tost-0.1.2-cp38-abi3-win32.whl |
|---|---|
| Size | 669.6 kB |
| Tags | CPython 3.8 Windows x86-32 abi3 |
|
SHA-256 checksum How to use checksums |
6ac2b78f288f7da38234ca6ccb3e0723c421e9fdd22d95fbe60018d26c0bc78d
|
|
BLAKE2b-256 checksum How to use checksums |
2e32d884827ffd1a73324fd211cd5105443b02d41fda1682c0f0c1aa91a71ae2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Release files / tost-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | tost-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 993.2 kB |
| Tags | CPython 3.8 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
29ccbc6d2354128c0c0965109e2d26ae59f822ac40c699d1fbf98ef7f95191a8
|
|
BLAKE2b-256 checksum How to use checksums |
321fd58ab7ba9c12b995309da2dbf82dd8d18c205f4e6b69cadbc351a227bba1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Release files / tost-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | tost-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 965.0 kB |
| Tags | CPython 3.8 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
0e6b02cec42e0dc6105143b9fb5509208025268abb5261e158de5420eba669fb
|
|
BLAKE2b-256 checksum How to use checksums |
f621a65e59e0265443c77a928448d0aee363d0bd981f0601710447229ffa3c65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Release files / tost-0.1.2-cp38-abi3-macosx_11_0_arm64.whl
| Download URL | tost-0.1.2-cp38-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 845.9 kB |
| Tags | CPython 3.8 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
0598555b1b064fe9f1e2e7a7a9bc32d5b40203ca76f2423763abc97061479480
|
|
BLAKE2b-256 checksum How to use checksums |
b1efa6b111058c44ee689fbe005412786e5576780e911f071a4fc1477be1ee62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|
Release files / tost-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl
| Download URL | tost-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 895.0 kB |
| Tags | CPython 3.8 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
f9b91239930590f89b1a1f985f3b55d94603314a2ee7acf627061b21a7b62487
|
|
BLAKE2b-256 checksum How to use checksums |
6f510224af7b031d83047eae25a9930a73a099c604a921fb20cd809f92b75ab7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.6
|