How to Use TOON Format in Python - Complete Tutorial

Step-by-step guide with code examples to implement TOON in your Python applications

Published: January 2025 • 12 min read • For Python Developers

TOON format reduces token usage by approximately 50% compared to JSON when working with OpenAI, Claude, or other LLM APIs. This guide shows you how to implement TOON in Python applications.

Whether you're building with FastAPI, Flask, Django, or just using the OpenAI Python SDK, this guide has you covered. We'll go from zero to a working implementation with real code examples you can use right away.

Contents:

  • Installing and setting up TOON in your Python project
  • Parsing and generating TOON data
  • Converting between JSON and TOON
  • Integrating with OpenAI, Anthropic Claude, and other LLM APIs
  • Working with Pandas DataFrames
  • Real-world examples and best practices

Prerequisites

Before we start, make sure you have:

Development Environment

  • Python 3.8 or higher
  • pip (Python package manager)
  • Virtual environment (recommended)

Knowledge

  • Basic Python programming
  • Understanding of JSON and dictionaries
  • (Optional) OpenAI API basics

Step 1: Installation

Let's install the TOON library for Python. It's as simple as:

1

Using pip (Recommended)

Open your terminal and run:

pip install toon-format
2

With OpenAI Integration

To include OpenAI helpers and token counting:

pip install toon-format[openai]
3

In a Virtual Environment (Best Practice)

# Create virtual environment
python -m venv venv

# Activate it (Windows)
venv\Scripts\activate

# Activate it (Mac/Linux)
source venv/bin/activate

# Install TOON
pip install toon-format

Pro Tip

The package includes everything you need: parser, serializer, JSON converter, token counter, and LLM integration helpers!

Step 2: Your First TOON Program 🚀

Let's start with a simple "Hello World" example. Here's how to parse and generate TOON data:

📝Example: Parsing TOON Data

from toon import parse

# Sample TOON data
toon_data = """
name: "Sarah Wilson"
age: 32
email: "[email protected]"
active: true
"""

# Parse TOON to Python dictionary
data = parse(toon_data)

print(f"Name: {data['name']}")
print(f"Age: {data['age']}")
print(f"Email: {data['email']}")
print(f"Active: {data['active']}")

Output:

Name: Sarah Wilson
Age: 32
Email: [email protected]
Active: True

Example: Generating TOON Data

from toon import dumps

# Create a Python dictionary
person = {
    "name": "Mike Chen",
    "age": 28,
    "email": "[email protected]",
    "active": True
}

# Convert to TOON format
toon_output = dumps(person)

print(toon_output)

Output:

name: "Mike Chen"
age: 28
email: "[email protected]"
active: true

Step 3: Converting JSON to TOON 🔄

Already have JSON data? Perfect! Here's how to convert it to TOON:

INPUT (JSON)
{
  "customers": [
    {
      "id": 1,
      "name": "Tower Site A",
      "email": "[email protected]",
      "active": true
    },
    {
      "id": 2,
      "name": "Tower Site B",
      "email": "[email protected]",
      "active": false
    }
  ]
}
OUTPUT (TOON)
customers[2]{id,name,email,active}:
  1,Tower Site A,[email protected],true
  2,Tower Site B,[email protected],false

Python Code to Convert:

import json
from toon import json_to_toon, count_tokens

# Your JSON string
json_data = """{
  "customers": [
    {"id": 1, "name": "Tower Site A", "email": "[email protected]", "active": true},
    {"id": 2, "name": "Tower Site B", "email": "[email protected]", "active": false}
  ]
}"""

# Convert JSON to TOON
toon_data = json_to_toon(json_data)

print(toon_data)

# Count tokens saved
json_tokens = count_tokens(json_data)
toon_tokens = count_tokens(toon_data)
saved = json_tokens - toon_tokens
percentage = (saved / json_tokens) * 100

print(f"\nJSON tokens: {json_tokens}")
print(f"TOON tokens: {toon_tokens}")
print(f"Saved: {saved} tokens ({percentage:.1f}%)")

💰 Typical Savings: 48-52% fewer tokens!

That means your OpenAI API costs are cut in half.

Step 4: Working with Lists & Arrays 📊

TOON really shines when working with lists of dictionaries. Here's how to handle collections in Python:

Example: Parsing Array Data

from toon import parse

toon_data = """
products[3]{id,name,price,stock}:
  101,Laptop,999.99,15
  102,Mouse,29.99,50
  103,Keyboard,79.99,30
"""

# Parse to Python list of dictionaries
data = parse(toon_data)

for product in data['products']:
    print(f"{product['name']}: ${product['price']} ({product['stock']} in stock)")

# Output:
# Laptop: $999.99 (15 in stock)
# Mouse: $29.99 (50 in stock)
# Keyboard: $79.99 (30 in stock)

Example: Generating Array Data

from toon import dumps

# Create a list of products
products = [
    {"id": 1, "name": "Phone", "price": 699.99, "stock": 25},
    {"id": 2, "name": "Tablet", "price": 399.99, "stock": 18},
    {"id": 3, "name": "Watch", "price": 299.99, "stock": 42}
]

# Serialize to TOON (automatically uses efficient array format)
toon_output = dumps({"products": products})

print(toon_output)

# Output:
# products[3]{id,name,price,stock}:
#   1,Phone,699.99,25
#   2,Tablet,399.99,18
#   3,Watch,299.99,42

Step 5: Working with Pandas DataFrames 🐼

If you're using Pandas for data analysis, TOON integrates seamlessly:

DataFrame to TOON Conversion:

import pandas as pd
from toon import dataframe_to_toon

# Create a DataFrame
df = pd.DataFrame({
    'id': [1, 2, 3],
    'name': ['Site A', 'Site B', 'Site C'],
    'score': [95.5, 87.3, 92.1],
    'passed': [True, True, True]
})

# Convert DataFrame to TOON
toon_data = dataframe_to_toon(df, name='students')

print(toon_data)

# Output:
# students[3]{id,name,score,passed}:
#   1,Site A,95.5,true
#   2,Site B,87.3,true
#   3,Site C,92.1,true

# This is PERFECT for sending data to LLMs!
# Much more efficient than df.to_json()

💡Why This Matters

When you're doing data analysis and want to ask an LLM about your data, converting a DataFrame to TOON saves massive amounts of tokens compared to df.to_json()!

Step 6: OpenAI API Integration 🤖

Here's where TOON really pays off! Let's integrate it with the OpenAI API to save tokens and money:

💸Save Money on Every API Call!

By using TOON instead of JSON in your prompts, you'll cut token usage by ~50%, which means your API costs are cut in half too!

Complete OpenAI Example:

from openai import OpenAI
from toon import dumps

client = OpenAI(api_key="your-api-key")

def analyze_customers_with_toon(customers):
    """Analyze customers using TOON format to save tokens"""
    
    # Convert customers to TOON format (saves 50% tokens!)
    customer_data = dumps({"customers": customers})
    
    # Create prompt with TOON data
    prompt = f"""Analyze these customers and provide insights.

Customer Data (TOON format):
{customer_data}

Provide:
1. Total active vs inactive customers
2. Most common email domains
3. Any patterns you notice"""
    
    # Send to OpenAI
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You understand TOON format data efficiently."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=500
    )
    
    return response.choices[0].message.content

# Usage
customers = [
    {"id": 1, "name": "Metro Wireless", "email": "[email protected]", "active": True},
    {"id": 2, "name": "5G Networks Inc", "email": "[email protected]", "active": True},
    {"id": 3, "name": "FiberNet Solutions", "email": "[email protected]", "active": False}
]

analysis = analyze_customers_with_toon(customers)
print(analysis)

With Token Counting:

from openai import OpenAI
from toon import dumps, count_tokens
import json

client = OpenAI(api_key="your-api-key")

customers = [
    {"id": 1, "name": "Metro Wireless", "email": "[email protected]", "active": True},
    {"id": 2, "name": "5G Networks Inc", "email": "[email protected]", "active": True},
    # ... more customers
]

# Compare token usage
json_data = json.dumps(customers)
toon_data = dumps({"customers": customers})

json_tokens = count_tokens(json_data)
toon_tokens = count_tokens(toon_data)
saved_tokens = json_tokens - toon_tokens

print(f"JSON would use: {json_tokens} tokens")
print(f"TOON uses: {toon_tokens} tokens")
print(f"You save: {saved_tokens} tokens ({(saved_tokens/json_tokens)*100:.1f}%)")

# At $0.01 per 1K tokens for GPT-4:
cost_saved = (saved_tokens / 1000) * 0.01
print(f"Cost saved per call: ${cost_saved:.4f}")

# Now use the efficient TOON version
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "user", "content": f"Analyze this data:\n{toon_data}"}
    ]
)

print(response.choices[0].message.content)

Step 7: Anthropic Claude Integration 🧠

TOON works great with Claude too! Here's how:

from anthropic import Anthropic
from toon import dumps

client = Anthropic(api_key="your-api-key")

def analyze_with_claude(data):
    """Send data to Claude using efficient TOON format"""
    
    # Convert to TOON
    toon_data = dumps(data)
    
    message = client.messages.create(
        model="claude-3-opus-20240229",
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": f"Analyze this data in TOON format:\n{toon_data}"
            }
        ]
    )
    
    return message.content[0].text

# Usage
sales_data = {
    "sales": [
        {"id": 1, "product": "Laptop", "amount": 999.99, "qty": 5},
        {"id": 2, "product": "Mouse", "amount": 29.99, "qty": 20},
        {"id": 3, "product": "Keyboard", "amount": 79.99, "qty": 10}
    ]
}

result = analyze_with_claude(sales_data)
print(result)

Best Practices & Tips 💡

RecommendedAvoid
Use TOON for LLM promptsUsing TOON for public APIs
Convert DataFrames to TOON before sending to AIManually building TOON strings
Use context managers for file operationsIgnoring exceptions
Cache serialized TOON for repeated API callsForgetting to close file handles

Performance Tips

  • 1
    Use generators for large datasets:
    from toon import dumps_iter
  • 2
    Batch similar API calls: Convert multiple datasets at once
  • 3
    Use async for concurrent operations: Speed up multiple LLM calls

Error Handling 🛡️

Always handle errors gracefully. Here's a production-ready example:

from toon import parse, dumps, ToonSyntaxError, ToonValidationError
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def safe_parse(toon_data: str) -> dict | None:
    """Safely parse TOON data with error handling"""
    try:
        return parse(toon_data)
    except ToonSyntaxError as e:
        logger.error(f"TOON syntax error at line {e.line_number}: {e.message}")
        return None
    except ToonValidationError as e:
        logger.error(f"TOON validation failed: {e.message}")
        return None
    except Exception as e:
        logger.error(f"Unexpected error parsing TOON: {e}")
        return None

def safe_dumps(data: dict) -> str | None:
    """Safely convert dict to TOON with error handling"""
    try:
        return dumps(data)
    except Exception as e:
        logger.error(f"Failed to serialize to TOON: {e}")
        return None

# Usage
toon_data = """
name: "Tower A"
age: 30
"""

result = safe_parse(toon_data)
if result:
    print("Parsed successfully:", result)
else:
    print("Failed to parse")

Complete Working Example

Here's a full script that puts it all together:

#!/usr/bin/env python3
"""
TOON Format Python Demo
Demonstrates parsing, serializing, and OpenAI integration
"""

from openai import OpenAI
from toon import dumps, parse, count_tokens
import json

def main():
    print("🚀 TOON Format Python Demo\n")
    
    # 1. Create sample data
    customers = [
        {"id": 1, "name": "Metro Wireless", "email": "[email protected]", "active": True},
        {"id": 2, "name": "5G Networks Inc", "email": "[email protected]", "active": True},
        {"id": 3, "name": "FiberNet Solutions", "email": "[email protected]", "active": False}
    ]
    
    # 2. Serialize to TOON
    toon_data = dumps({"customers": customers})
    
    print("📊 Customer Data in TOON format:")
    print(toon_data)
    print()
    
    # 3. Compare token counts
    json_data = json.dumps({"customers": customers})
    json_tokens = count_tokens(json_data)
    toon_tokens = count_tokens(toon_data)
    saved = json_tokens - toon_tokens
    percentage = (saved / json_tokens) * 100
    
    print(f"📈 Token Comparison:")
    print(f"   JSON: {json_tokens} tokens")
    print(f"   TOON: {toon_tokens} tokens")
    print(f"   Saved: {saved} tokens ({percentage:.1f}%)")
    print()
    
    # 4. Send to OpenAI (if API key is set)
    import os
    if os.getenv("OPENAI_API_KEY"):
        print("🤖 Sending to OpenAI...")
        client = OpenAI()
        
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "user", "content": f"Analyze these customers:\n{toon_data}"}
            ],
            max_tokens=200
        )
        
        print("💬 AI Response:")
        print(response.choices[0].message.content)
    else:
        print("ℹ️  Set OPENAI_API_KEY environment variable to test OpenAI integration")
    
    print("\n✅ Demo complete!")

if __name__ == "__main__":
    main()

💾 Save this as: toon_demo.py and run with python toon_demo.py

Bonus: FastAPI Integration 🚀

Building an API? Here's how to integrate TOON with FastAPI:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from toon import dumps, parse
from openai import OpenAI

app = FastAPI()
client = OpenAI()

class AnalyzeRequest(BaseModel):
    data: dict

@app.post("/analyze")
async def analyze_data(request: AnalyzeRequest):
    """Analyze data using OpenAI with TOON format for efficiency"""
    try:
        # Convert to TOON (saves 50% tokens)
        toon_data = dumps(request.data)
        
        # Send to OpenAI
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "user", "content": f"Analyze:\n{toon_data}"}
            ]
        )
        
        return {
            "analysis": response.choices[0].message.content,
            "format_used": "TOON",
            "token_savings": "~50%"
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Run with: uvicorn main:app --reload

Helpful Tools 🛠️

These free online tools will help you work with TOON:

Next Steps 🚀

Continue Learning:

🎉You're Ready!

Congratulations! You now know how to use TOON format in your Python applications. You can:

  • Parse and generate TOON data with parse() and dumps()
  • Convert between JSON and TOON
  • Work with Pandas DataFrames efficiently
  • Integrate with OpenAI and Anthropic Claude
  • Save 50% on your AI API costs

Start saving tokens today! Try TOON in your next Python project and see the difference it makes.

Additional Resources