TOON (Token-Oriented Object Notation) converter component for Langflow
Project description
TOON Converter for Langflow
A custom Langflow component that converts structured data (JSON, XML, CSV, HTML) into TOON (Token-Oriented Object Notation) format to optimize token usage for LLM prompts.
Features
- Multi-format Support: Convert JSON, XML, CSV, and HTML to TOON format
- Token Optimization: Achieve 30%+ token reduction compared to formatted JSON
- Configurable Delimiters: Choose between comma, tab, or pipe separators
- Token Statistics: View before/after token counts and savings percentage
- Memory Warnings: Get alerts for large datasets before processing
- Detailed Error Messages: Get location-specific error messages with corrected examples
Installation
From Source
git clone https://github.com/faisolp/langflow-toon-component.git
cd toon
pip install -e .
Dependencies
pip install -r requirements.txt
Required packages:
tiktoken>=0.5.0- Token counting for optimization analysisxmltodict>=0.13.0- XML parsing and conversionpytest>=7.0.0- Testing framework (development only)pytest-cov>=4.0.0- Test coverage reporting (development only)
Usage
Python API Usage
from langflow_toon import ToonConverter
from langflow_toon.models.data import ConversionConfig, Delimiter
# Initialize converter
converter = ToonConverter()
# Convert JSON to TOON
json_data = '{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}'
result = converter.convert(json_data, input_format='JSON')
print(result.toon_output)
# Output:
# users[2]{id,name}:
# 1,Alice
# 2,Bob
print(f"Token reduction: {result.token_reduction} ({result.token_reduction/result.original_tokens*100:.1f}%)")
Basic Usage in Langflow
- Add the "TOON Converter" component to your Langflow flow
- Connect your data source to the
input_textport - Configure the
input_format(or enableauto_detectfor automatic detection) - Choose your preferred
csv_delimiter(comma, tab, or pipe) - Toggle
sort_keysto alphabetically sort object keys - Toggle
ensure_asciito encode non-ASCII characters - Connect the
toon_outputto your LLM prompt - View
original_tokens,toon_tokens, andtoken_reductionstatistics
Example: JSON to TOON
Input (JSON):
{
"users": [
{"id": 1, "name": "Alice", "role": "admin"},
{"id": 2, "name": "Bob", "role": "user"}
]
}
Output (TOON):
users[2]{id,name,role}:
1,Alice,admin
2,Bob,user
Token Savings: 85 → 42 tokens (50.6% reduction)
Example: CSV with Tab Delimiter
Input (CSV):
name,age,city
Alice,30,NYC
Bob,25,LA
Output (TOON with tab delimiter):
data[2 ]{name age city}:
Alice 30 NYC
Bob 25 LA
Example: Complete Langflow Workflow
[HTTP Request] → [TOON Converter] → [LLM Prompt]
↓
[Token Stats Display]
Flow Configuration:
- HTTP Request: Fetch JSON data from an API
- TOON Converter:
input_text: Connect from HTTP Request outputinput_format: JSONcsv_delimiter: comma (default)auto_detect: false (format known)sort_keys: false (preserve order)ensure_ascii: false (keep Unicode)
- LLM Prompt: Use
{{toon_output}}variable in prompt template - Token Stats Display: Show
{{original_tokens}},{{toon_tokens}},{{token_reduction}}
Example: Multi-Format Processing Pipeline
┌─────────────┐
│ Format │
│ Detector │
└──────┬──────┘
│
┌────────────────┼────────────────┐
│ │ │
┌────▼────┐ ┌────▼────┐ ┌────▼────┐
│ JSON │ │ XML │ │ CSV │
│ Source │ │ Source │ │ Source │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
└───────────────┼───────────────┘
│
┌──────▼──────┐
│ TOON │
│ Converter │
└──────┬──────┘
│
┌──────▼──────┐
│ LLM │
│ Processing │
└─────────────┘
Component API
Inputs
| Port | Type | Description |
|---|---|---|
input_data |
string | Raw data to convert (JSON, XML, CSV, or HTML) |
input_format |
enum | Data format: AUTO, JSON, XML, CSV, HTML |
delimiter |
enum | TOON field delimiter: COMMA, TAB, PIPE |
sort_keys |
boolean | Sort object keys alphabetically (default: false) |
Outputs
| Port | Type | Description |
|---|---|---|
toon_output |
string | TOON formatted data |
token_stats |
object | Token usage statistics |
error |
string | Error message (if conversion fails) |
Development
Running Tests
# Run all tests
pytest tests/
# Run unit tests only
pytest tests/unit/
# Run with coverage
pytest --cov=langflow_toon tests/
Project Structure
langflow_toon/
├── __init__.py
├── component.py # Main Langflow custom component
├── converters/ # Format-specific converters
├── validators/ # Input and TOON validation
├── utils/ # Token counting, memory estimation, error formatting
└── config/ # Constants and configuration
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details
Links
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langflow_toon-0.1.1.tar.gz.
File metadata
- Download URL: langflow_toon-0.1.1.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43827fa6dff15d31b81caaba253691b85bbbc35659e3db27fc02566e729a9e48
|
|
| MD5 |
6c78bf7d53bc70d0585bc4fd24fa7fb2
|
|
| BLAKE2b-256 |
b36b250404fdb01342befb668d931fa1dcb8f18dc25fa35baea24d9cf8230898
|
File details
Details for the file langflow_toon-0.1.1-py3-none-any.whl.
File metadata
- Download URL: langflow_toon-0.1.1-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b7695cfcaaf9406e959f5f389192d41f2e30cba37324e02b9e908f1afda8753
|
|
| MD5 |
31ea30ff6683351193ad55e8bcf2aaa8
|
|
| BLAKE2b-256 |
162c5ab2ba066a676b6449663403e7e0f741533d0aad2cb86b1f6a2133bd7405
|