TOON (Token-Oriented Object Notation)

repository·main·Indexed 43 days ago

https://github.com/scrapegraphai/toonify

A compact, human-readable serialization format designed to reduce token usage and memory when passing structured data to Large Language Models (LLMs). Version 0.0.2 provides a Python API for encoding and decoding data, Pydantic model integration, and tools to generate structure templates for LLM prompts. It achieves significant reductions in file size and token counts, particularly for tabular and structured records, by using a header-based array representation and minimal syntax overhead.

Tokens
9.8K
Snippets
39
Records
50
Agent score
55%

What's inside TOON

  1. Benchmark results: TOON vs JSON

    main

    TOON (Token-Oriented Object Notation) provides significant savings in both file size and LLM token usage compared to JSON. Across 50 real-world datasets, TOON achieved an average of 63.9% smaller file sizes and 54.1% fewer tokens for LLM APIs.

    Key Benefits for LLM Users

    • Lower API Costs: Reduces token usage by over half on average.
    • Faster Responses: Less data to transmit results in lower latency.
    • Increased Context: More data can fit within the LLM's context window.

    Performance by Category

    CategoryAvg Size SavingsAvg Token Savings
    Tabular Data69.2%59.8%
    E-commerce66.1%56.4%
    Analytics65.7%55.2%
    API Data58.3%48.9%
    IoT/Sensors60.0%43.7%
    Social/Content61.5%52.1%
  2. How TOON achieves data reduction

    main

    TOON reduces size and token counts through three primary mechanisms:

    1. Eliminates Repeated Keys in Arrays: Unlike JSON, which repeats keys for every object in an array, TOON declares headers once for the entire collection.
      • JSON Example: [{"id": 1, "name": "Laptop"}, {"id": 2, "name": "Mouse"}]
      • TOON Example: [2]{id,name}: 1,Laptop 2,Mouse
    2. Minimal Syntax Overhead: TOON replaces heavy JSON syntax (braces {}, brackets [], quotes around keys/values, and commas) with indentation-based structure (similar to YAML), colons for key-value pairs, and quotes only when strictly necessary.
    3. Intelligent Type Handling: Automatically detects and preserves types (numbers, booleans, null) without requiring explicit quotes, maintaining human readability while minimizing tokens.
  3. Determine when to use TOON based on data type

    main

    TOON's efficiency depends on the structure of your data. It is most effective when data contains repeated structures or tabular patterns.

    • Tabular/Structured Data: (e.g., Survey responses, ML training data) Offers ~60-70% savings with excellent ROI.
    • E-commerce/Products: High savings (~56-66%) with very good efficiency.
    • API Results & IoT/Sensors: Good to very good savings (~44-58%).
    • Large Payloads: Payloads >1KB allow serialization overhead to be amortized.
    • Repeated LLM Calls: Where cumulative token savings justify the per-request CPU cost.

    When to Reconsider TOON

    • Real-time systems: If your application has microsecond latency requirements.
    • Small/Irregular Payloads: Single payloads <100 bytes or highly irregular data where objects do not share common structures (savings may drop below 40%).
    • Maximum Compatibility: If you require universal support for standard JSON tools without a decoding step.
  4. How TOON achieves memory and token savings

    main

    TOON reduces overhead through three primary mechanisms:

    1. Compact Array Representation: Unlike JSON, which repeats keys for every object in an array, TOON declares headers once for uniform arrays.

      • JSON: [{"id": 1, "name": "A"}, {"id": 2, "name": "B"}]
      • TOON: [2]{id,name}: 1,A 2,B
    2. Minimal Syntax Overhead: TOON eliminates the need for braces {}, brackets [] (except for array declarations), quotes around all keys, and excessive commas. It uses indentation for structure and colons for key-value pairs.

    3. Intelligent Type Handling: TOON automatically detects when quotes are unnecessary and preserves types like numbers, booleans, and null while maintaining human readability.

  5. Advanced TOON features: Key Folding and Path Expansion

    main

    TOON provides advanced features for managing complex or deeply nested data structures.

    Key Folding

    Reduces a single key chain into a dot-separated path. Use key_folding='safe' in encode().

    data = {'api': {'response': {'product': {'title': 'Wireless Keyboard'}}}}
    toon = encode(data, {'key_folding': 'safe'})
    # Output: api.response.product.title: Wireless Keyboard

    Path Expansion

    Expands dot-separated keys back into nested objects. Use expand_paths='safe' in decode().

    toon = 'store.location.zipcode: 10001'
    data = decode(toon, {'expand_paths': 'safe'})
    # Result: {'store': {'location': {'zipcode': 10001}}}
    # Key Folding
    toon = encode(data, {'key_folding': 'safe'})
    
    # Path Expansion
    data = decode(toon, {'expand_paths': 'safe'})
  6. Compare TOON vs JSON for LLM workflows

    main

    TOON (Token-Oriented Object Notation) is designed to be more efficient than JSON, specifically for LLM contexts.

    Use TOON when:

    • Passing data to LLM APIs (reduces token costs).
    • Working with uniform tabular data.
    • Context window is limited.
    • Human readability is required.

    Use JSON when:

    • Maximum compatibility is required.
    • Data is highly irregular or deeply nested.
    • Working with existing JSON-only tools.
  7. Understand TOON performance benefits

    main

    TOON (Token-Oriented Object Notation) is designed to provide massive memory and token savings compared to JSON, particularly for structured and tabular data.

    Key Performance Metrics

    On average, TOON achieves:

    • 63.9% reduction in file size and network bandwidth.
    • 54.1% reduction in token count for LLM APIs.

    Best Use Cases

    TOON excels in the following scenarios:

    • Tabular Data (e.g., database results, inventory): Up to 73.4% size reduction.
    • Uniform Arrays (e.g., ML training data): Up to 63.4% token savings.
    • Structured Records (e.g., e-commerce products): ~66% size reduction.
    • Analytics Data (e.g., surveys, metrics): Consistently 65-73% savings.

    When to Expect Lower Savings

    Savings may be lower (e.g., ~39%) when dealing with deeply nested objects with non-uniform structures.

  8. When to use TOON vs JSON

    main

    ✅ Use TOON for:

    • LLM API Payloads: To cut token costs significantly.
    • Database Query Results: Efficiently handling tabular data.
    • Analytics & Metrics: Time series and aggregates.
    • E-commerce Data: Product catalogs and inventory.
    • IoT Sensor Data: Regular, structured readings.
    • API Logs & Traces: Structured log entries.
    • ML Training Data: Feature vectors and labels.

    ⚠️ Use JSON for:

    • Highly irregular/nested data: While TOON still saves 20-40%, the efficiency is lower than for structured data.
    • Maximum compatibility: JSON is the universal standard for web interoperability.
    • Microsecond-level performance: JSON parsers are often faster for extremely high-frequency, low-latency requirements.
  9. How key folding and path expansion work

    main

    TOON provides advanced features to further compress or expand data structures.

    Key Folding

    When key_folding='safe' is used during encoding, single-key chains are collapsed into dot-separated paths. This reduces vertical space and token usage.

    Input:

    {'api': {'response': {'product': {'title': 'Wireless Keyboard'}}}}

    TOON Output:

    api.response.product.title: Wireless Keyboard

    Path Expansion

    When expand_paths='safe' is used during decoding, dot-separated keys are expanded back into nested objects.

    Input TOON:

    store.location.zipcode: 10001

    Python Output:

    {'store': {'location': {'zipcode': 10001}}}
    # Key Folding Example
    data = {'api': {'response': {'product': {'title': 'Wireless Keyboard'}}}}
    toon = encode(data, {'key_folding': 'safe'})
    # Result: api.response.product.title: Wireless Keyboard
    
    # Path Expansion Example
    toon_str = 'store.location.zipcode: 10001'
    data = decode(toon_str, {'expand_paths': 'safe'})
    # Result: {'store': {'location': {'zipcode': 10001}}}
  10. Run the TOON Benchmark Suite

    main

    To run the benchmarks and evaluate performance on your own datasets, follow these steps:

    1. Add your dataset to sample_datasets.py.
    2. Execute the benchmark suite (refer to the repository's main instructions for the specific execution command).
    3. Submit a Pull Request with your findings to contribute to the benchmark data.
  11. Run TOON benchmarks locally

    main

    To run the benchmark suite yourself and compare TOON against JSON, follow these steps:

    1. Clone the repository.
    2. Install the project and the tiktoken dependency.
    3. Execute the specific benchmark scripts.
    # Clone the repo
    git clone https://github.com/ScrapeGraphAI/toonify.git
    cd toonify
    
    # Install dependencies
    pip install -e .
    pip install tiktoken
    
    # Run specific benchmarks
    python benchmark/compare_formats.py
    python benchmark/memory_benchmark.py
    
    # Or run all benchmarks at once
    python benchmark/run_all.py