Skip to main content

TOON (Token-Oriented Object Notation) Python API - A compact data format optimized for LLM token usage

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tost-0.1.2.tar.gz (15.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tost-0.1.2-cp38-abi3-win_amd64.whl (739.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

tost-0.1.2-cp38-abi3-win32.whl (669.6 kB view details)

Uploaded CPython 3.8+Windows x86

tost-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (993.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

tost-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (965.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

tost-0.1.2-cp38-abi3-macosx_11_0_arm64.whl (845.9 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tost-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl (895.0 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file tost-0.1.2.tar.gz.

File metadata

  • Download URL: tost-0.1.2.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for tost-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d63f46e837d233590b5114241abf57bb4c0a998e86058983071b48426d794c2c
MD5 f53f2211b8e1e2ecf260888e09f70de3
BLAKE2b-256 c8700e24ce8eadc362e8b12dfe5843851c6fa1f440a41c6d614b2a546526d09f

See more details on using hashes here.

File details

Details for the file tost-0.1.2-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: tost-0.1.2-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 739.6 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for tost-0.1.2-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f34b957a1cb081e42aba4f280e5fb2f9ee0be76012efa3d0d0786ed048d2aa3f
MD5 5a2a18b72c341f629bf9084e1b6e43b3
BLAKE2b-256 e2cd2484466e97d957f0acc01573042a3e00027ea44f0cdf6fe7620c1464ae9f

See more details on using hashes here.

File details

Details for the file tost-0.1.2-cp38-abi3-win32.whl.

File metadata

  • Download URL: tost-0.1.2-cp38-abi3-win32.whl
  • Upload date:
  • Size: 669.6 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for tost-0.1.2-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 6ac2b78f288f7da38234ca6ccb3e0723c421e9fdd22d95fbe60018d26c0bc78d
MD5 3ae07b97bd6e9adf145e7bd25692e994
BLAKE2b-256 2e32d884827ffd1a73324fd211cd5105443b02d41fda1682c0f0c1aa91a71ae2

See more details on using hashes here.

File details

Details for the file tost-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tost-0.1.2-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 29ccbc6d2354128c0c0965109e2d26ae59f822ac40c699d1fbf98ef7f95191a8
MD5 ffd48702c1cee183c972b3eaf9326892
BLAKE2b-256 321fd58ab7ba9c12b995309da2dbf82dd8d18c205f4e6b69cadbc351a227bba1

See more details on using hashes here.

File details

Details for the file tost-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tost-0.1.2-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0e6b02cec42e0dc6105143b9fb5509208025268abb5261e158de5420eba669fb
MD5 a6c1d9ae8aef1e3d45d501747ccbcea7
BLAKE2b-256 f621a65e59e0265443c77a928448d0aee363d0bd981f0601710447229ffa3c65

See more details on using hashes here.

File details

Details for the file tost-0.1.2-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tost-0.1.2-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0598555b1b064fe9f1e2e7a7a9bc32d5b40203ca76f2423763abc97061479480
MD5 41267ab355a31b1a8c43f8735e330f68
BLAKE2b-256 b1efa6b111058c44ee689fbe005412786e5576780e911f071a4fc1477be1ee62

See more details on using hashes here.

File details

Details for the file tost-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tost-0.1.2-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f9b91239930590f89b1a1f985f3b55d94603314a2ee7acf627061b21a7b62487
MD5 f3ba0876499ae8e80caa418004716ec7
BLAKE2b-256 6f510224af7b031d83047eae25a9930a73a099c604a921fb20cd809f92b75ab7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page