xaviviro/python-toon

🐍 TOON for Python (Token-Oriented Object Notation) Encoder/Decoder - Reduce LLM token costs by 30-60% with structured data.

What it solves

This project provides a Python implementation of the TOON (Token-Oriented Object Notation) format, designed to reduce the number of tokens used when transmitting structured data to Large Language Models (LLMs). It aims to solve the problem of token inefficiency in standard formats like JSON, which often contain redundant punctuation that consumes unnecessary tokens.

How it works

TOON combines indentation-based nesting (similar to YAML) and a tabular format for uniform data rows (similar to CSV). It eliminates redundant braces, brackets, and quotes where possible. For uniform object collections, it collapses the keys into a single header and lists the values in rows, significantly reducing the character and token count.

Who it’s for

Developers building LLM-integrated applications who want to optimize their prompts and responses to save on token costs and potentially improve model performance by providing more compact data representations.

Highlights

  • Token Reduction: Achieves 30-60% token reduction compared to JSON for uniform data.
  • Tabular Arrays: Uses CSV-like rows for uniform object collections, including support for nested uniform objects.
  • Bidirectional Conversion: Includes both encode and decode functions with strict validation options.
  • CLI Tool: Provides a command-line interface for converting between JSON and TOON formats.
  • Zero Dependencies: Written in pure Python 3.8+.

Related

  • Project
  • Project
  • Project
  • Project