A Python library for the TRON data serialization format — a compact JSON superset designed for LLM token efficiency.
TRON (Token Reduced Object Notation) extends JSON with schema-based class definitions, dramatically reducing token count when serializing structured data for LLM APIs. tron-format-py provides a complete Python implementation of the TRON specification.
pip install tron-format-pyRequirements: Python 3.10+
fromtron_format_pyimportTRON# Serialize to TRONvalue= [{"name": "Alice", "role": "admin"}, {"name": "Bob", "role": "user"}]
tron=TRON.stringify(value)
print(tron)
# class A: name,role## [A("Alice","admin"),A("Bob","user")]# Parse back to Pythonparsed=TRON.parse(tron)
assertparsed==value| Feature | Description |
|---|---|
| Schema-based encoding | Repeated object structures become class instantiations, eliminating key repetition |
| Named arguments | Assign values by property name for improved readability |
| Class inheritance | Extend existing classes to model hierarchical data |
| Pretty printing | Optional indented output with configurable indentation |
| Full spec compliance | 100% test coverage across all TRON format features |
| Round-trip safe | parse(stringify(data)) == data for all supported types |
Convert a Python object to a TRON format string.
| Parameter | Type | Default | Description |
|---|---|---|---|
value | Any | — | The Python object to serialize |
indent | int | None | None | Number of spaces for indentation. None or 0 produces compact output |
Returns:str — A TRON format string
Example:
# Compact output (default)TRON.stringify({"x": 1, "y": 2})
# '{"x":1,"y":2}'# Pretty outputTRON.stringify([{"x": 1}, {"x": 2}], indent=2)
# """class A: x## [# A(1),# A(2)# ]"""Parse a TRON format string into a Python object.
| Parameter | Type | Default | Description |
|---|---|---|---|
text | str | — | A TRON format string |
Returns:Any — The parsed Python object
Example:
TRON.parse('class Point: x, y\nPoint(10, 20)')
# {"x": 10, "y": 20}Objects can be instantiated using defined classes, mapping positional arguments to class properties:
tron='''class Order: index, items, totalclass Product: index, name, price, quantityOrder("ord-123", [ Product(1, "Widget", 19.99, 2), Product(2, "Gadget", 29.99, 1)], 109.96)'''TRON.parse(tron)
# {# "index": "ord-123",# "items": [# {"index": 1, "name": "Widget", "price": 19.99, "quantity": 2},# {"index": 2, "name": "Gadget", "price": 29.99, "quantity": 1}# ],# "total": 109.96# }TRON.parse('class User: name, email\nUser(name="alice", email="alice@example.com")')
# {"name": "alice", "email": "alice@example.com"}TRON.parse('''class Base: idclass Extended(Base): name, valueExtended(1, "test", 42)''')
# {"id": 1, "name": "test", "value": 42}| TRON Type | Python Type |
|---|---|
null | None |
true / false | True / False |
123 / 12.34 | int / float |
"string" | str |
[a, b, c] | list |
{a: 1, b: 2} | dict |
Class(a, b) | dict (keys from class definition) |
- Specification:tron-format.github.io
- Repository:github.com/tron-format/tron-python
- Issues:github.com/tron-format/tron-python/issues
- PyPI:pypi.org/project/tron-format-py
MIT License. Copyright (c) 2025-2026 Tim Huang. See LICENSE for details.