Skip to main content

A library to convert JSON, YAML, and XML to TOON format.

Project description

any2toon

any2toon is a robust and lightweight Python library designed to convert various data serialization formats (JSON, YAML, XML, CSV, Avro, Parquet, BSON) into TOON (Token Oriented Object Notation) to optimize interactions with LLMs.

Python Version License Build Status

📖 Introduction

any2toon solves the problem of preparing diverse data sources for Large Language Model (LLM) ingestion. LLMs often perform better (and cost less) when processing data that is free of excessive syntactic noise (like the braces and quotes in JSON).

TOON format is designed to be:

  • Token-Efficient: Minimizes punctuation overhead.
  • Human-Readable: Uses meaningful indentation and clean key-value pairs.
  • Structure-Preserving: Maintains the hierarchy and relationships of the original data.

This library acts as a universal adapter, taking standard formats (JSON, YAML, XML, CSV, Avro, Parquet) and normalizing them into this optimized notation.


🚀 Installation

Minimal Installation (JSON, YAML, XML, CSV support)

pip install any2toon

Full Installation (All formats + Optimizations)

pip install "any2toon[all]"

Format-Specific Installation

If you only need specific formats, you can install them individually to keep your environment light:

pip install "any2toon[parquet]" # For Parquet support (if installing from PyPI)
pip install ".[parquet]"        # If installing locally from source

Dependencies:

  • PyYAML: For parsing YAML files.
  • xmltodict: For converting XML parsing trees into Python dictionaries.
  • fastavro: (Optional) For reading binary Apache Avro OCF files.
  • pyarrow: (Optional) For reading Apache Parquet optimized columnar files.
  • pymongo: (Optional) For decoding BSON files.
  • polars / pandas: (Optional) For high-performance conversion of large datasets.

🛠️ Core Functionality & Approach

🛠️ Core Functionality & Approach

⚡ Performance & Optimization Strategy

The library uses a data-driven Tiered Optimization Strategy based on extensive benchmarking:

Input Format Threshold Priority 1 Priority 2 Fallback
CSV >= 100 rows Polars (Fastest) Pandas (Fast) Base Python
Parquet >= 100 rows Polars (Fastest) Pandas (Fast) Base Python
JSON/BSON/Avro >= 500 items Polars (Fastest) None Base Python

Why this strategy?

  • CSV/Parquet: Specialized engines like Polars and Pandas are drastically faster at parsing large files than Python's standard library. Polars is generally the winner (3x-6x faster).
  • List-of-Dicts (JSON/BSON): Creating a Pandas DataFrame from a list of objects has significant overhead. Our benchmarks show that Base Python is faster than Pandas for this specific case. However, Polars remains faster for large lists (>500 items), so it is the exclusive optimization path here.

The library automatically detects installed packages and selects the optimal path.

Conversion Pipeline

The library follows a consistent pipeline for all conversions:

  1. Ingestion: Read the raw input (string, bytes, or file stream) using a format-specific specialized library.
  2. Normalization: Convert the input into a standard Python object structure (Lists and Dictionaries).
  3. Serialization: Traverse the Python object and generate the TOON string.
    • Compact Format: Homogenous lists of objects are serialized as compact tables (root[N]{cols}:\n vals...) to save tokens.

Below is the detailed approach for each supported format.

1. JSON (JavaScript Object Notation)

Approach: We utilize Python's standard json library. Since JSON maps 1:1 with Python dictionaries and lists, this transformation is direct and high-fidelity.

from any2toon import convert_to_toon
json_data = '{"user": "alice", "roles": ["admin"]}'
print(convert_to_toon(json_data, 'json'))
# Output:
# user: alice
# roles[1]:
#   admin

2. YAML (YAML Ain't Markup Language)

Approach: We use PyYAML's safe_load to parse YAML. This renders YAML's alias/anchor features and complex types into resolved Python objects before conversion, ensuring the final TOON output is a clean data representation without parser-specific artifacts.

yaml_data = """
user: bob
attributes:
  active: true
"""
print(convert_to_toon(yaml_data, 'yaml'))
# Output:
# user: bob
# attributes:
#   active: true

3. XML (eXtensible Markup Language)

Approach: XML is inherently more complex due to attributes vs. text content. We leverage xmltodict to parse the XML tree.

  • Normalization Strategy: Elements become keys. Nested elements become nested dictionaries.
  • Note: Root elements are preserved, maintaining the document's semantic structure.
xml_data = "<config><mode>production</mode></config>"
print(convert_to_toon(xml_data, 'xml'))
# Output:
# config:
#   mode: production

4. CSV (Comma Separated Values)

Approach: We use the standard csv library's DictReader.

  • Assumption: The first row of the CSV must be a header row.
  • Transformation: Each row becomes a dictionary where keys are column headers. The final structure is a List of Dictionaries.
  • Goal: To turn tabular data into a record-oriented format readable by LLMs.
csv_data = "id,status\n1,open\n2,closed"
print(convert_to_toon(csv_data, 'csv'))
# Output:
# root[2]{id,status}:
#  1,open
#  2,closed

5. Apache Avro

Approach: We use fastavro for high-performance reading of binary Avro data.

  • Prerequisite: The input must be in OCF (Object Container File) format, which embeds the schema within the file itself. This allows any2toon to be stateless and schema-registry agnostic.
  • Transformation: The binary stream is iterated over to produce standard Python dictionaries.
# Assuming 'bytes_data' is loaded from a valid .avro file
print(convert_to_toon(bytes_data, 'avro'))
# Output example:
# root[100]{id,timestamp,value}:
#  1,1620000000,12.5
#  ...

6. Apache Parquet

Approach: We leverage pyarrow to read Parquet files.

  • Pipeline: Parquet (Columnar) $\to$ Arrow Table $\to$ Python List of Dicts (Row-based) $\to$ TOON.
  • Efficiency Note: While Parquet is columnar, TOON is row-oriented (for reading). We incur a transformation cost here to make the data human-readable, effectively "pivoting" the data structures.
# Assuming 'bytes_data' is loaded from a valid .parquet file
print(convert_to_toon(bytes_data, 'parquet'))
# Output example:
# root[50]{user,score}:
#  alice,99
#  bob,85

7. BSON (Binary JSON)

Approach: We use pymongo (specifically its bson module) to decode BSON data.

  • Support: Handles both single BSON documents and concatenated BSON streams (mongo dumps).
  • Transformation: BSON types are decoded into standard Python dictionaries/lists, then serialized to TOON.
import bson
# Assuming 'bytes_data' is a BSON byte string
print(convert_to_toon(bytes_data, 'bson'))

📚 API Reference

convert_to_toon(data_input, input_format) -> str

The universal entry point.

  • data_input: str (for text formats), bytes or BytesIO (for binary formats like Avro/Parquet), or dict/list (if pre-parsed).
  • input_format: Case-insensitive string: 'json', 'yaml', 'xml', 'csv', 'avro', 'parquet', 'bson'.

Specific Converters

Found in any2toon.converters:

  • json_to_toon(data)
  • yaml_to_toon(data)
  • xml_to_toon(data)
  • csv_to_toon(data)
  • avro_to_toon(data)
  • parquet_to_toon(data)
  • bson_to_toon(data)

Exceptions

  • InvalidFormatError: Raised if you request a format not supported.
  • ConversionError: Raised if the input data is malformed or parsing fails.

🧪 Testing

The library is backed by a comprehensive pytest suite ensuring fidelity for all conversions.

# Activate your environment
source venv/bin/activate

# Run tests
pytest

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

any2toon-0.1.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

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

any2toon-0.1.0-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file any2toon-0.1.0.tar.gz.

File metadata

  • Download URL: any2toon-0.1.0.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for any2toon-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b13c77bfe9fb8a53d8c0192e6f7a8feae93b66657fe42a341d4e48fe7e812677
MD5 297565c80ed55ce15b71cfeaef3a8ec9
BLAKE2b-256 3d4c85992f30cec93c8149f3fc1cd0efe0932d8282a89e008ff213088ae6b28f

See more details on using hashes here.

Provenance

The following attestation bundles were made for any2toon-0.1.0.tar.gz:

Publisher: publish.yml on AleLoredo/any2toon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file any2toon-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: any2toon-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for any2toon-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b2f72e9f070bc8713d878908756e2f42c143febef7806c3f0511ff93efd84c6
MD5 922ff604e04380195edde57eb1e69162
BLAKE2b-256 e5014e1e0b311ccde1412ea05260fd69acca959a5eb6bdecc509638ed5ff8c21

See more details on using hashes here.

Provenance

The following attestation bundles were made for any2toon-0.1.0-py3-none-any.whl:

Publisher: publish.yml on AleLoredo/any2toon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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