Skip to main content

tron-python

CI PyPI Python License: MIT

Python SDK for the TRON (Token Reduced Object Notation) format — a compact, LLM-friendly superset of JSON that reduces token usage by 20–40 % by hoisting repeated object schemas into a class-definition header.

See the full specification at tron-format.github.io.


Why TRON?

When sending structured data to LLMs you pay per token. JSON repeats key names for every object in an array:

[
  {"name": "Alice", "role": "eng", "active": true},
  {"name": "Bob",   "role": "mktg","active": false}
]

TRON defines the schema once and uses compact instantiation syntax in the body:

class A: name,role,active

[A("Alice","eng",true),A("Bob","mktg",false)]

Same data, fewer tokens. TRON is a JSON superset — the body section is valid JSON extended with ClassName(arg, …) syntax, so existing JSON parsers can read the data without modification.


Installation

pip install tron-python

No runtime dependencies. Requires Python 3.9+.

Optional extras:

pip install tron-python[benchmark]   # adds tiktoken for token-count comparisons
pip install tron-python[pydantic]    # explicit pydantic dependency pin

Quick start

import tron

# Serialize
data = [
    {"name": "Alice", "role": "eng", "score": 9.5},
    {"name": "Bob",   "role": "mktg","score": 8.1},
    {"name": "Carol", "role": "eng", "score": 9.2},
]
text = tron.stringify(data)
print(text)
# class A: name,role,score
#
# [A("Alice","eng",9.5),A("Bob","mktg",8.1),A("Carol","eng",9.2)]

# Deserialize
result = tron.parse(text)
assert result == data          # True — lossless roundtrip

# Plain JSON passthrough (TRON is a superset)
assert tron.parse('[1, 2, 3]') == [1, 2, 3]

API reference

tron.stringify(value) → str

Serialize any Python value to TRON.

Input type Output
None null
bool true / false
int, float JSON number (NaN/Inf → null)
str JSON-quoted string
list, tuple JSON array
dict ClassName(v1,v2,…) with header
datetime, date ISO-8601 string
Decimal float (NaN/Inf → null)
set, frozenset sorted JSON array
dataclass, Pydantic converted to dict first

Raises TypeError for bytes or unknown types.
Raises ValueError if a dict key contains a comma or newline.

tron.parse(text) → Any

Deserialize a TRON (or plain JSON) string.

Raises TypeError if text is not a str.
Raises ValueError for malformed TRON/JSON.

tron.benchmark_compare(value) → dict

Return character and (optionally) token counts for JSON vs TRON.

import tron

data = [{"id": i, "name": f"user_{i}"} for i in range(100)]
info = tron.benchmark_compare(data)
# {
#   "json": {"chars": 2890},
#   "tron": {"chars": 1750, "char_savings_pct": 39.4},
#   "note": "Install tiktoken for token-count comparisons: pip install tiktoken"
# }

tron.print_benchmark(value)

Pretty-print a comparison table to stdout (convenience wrapper around benchmark_compare).


Format primer

A TRON document has two sections separated by a blank line:

class A: field1,field2   ← header: one class per unique object shape
class B: x,y,z

A("val1",42)             ← body: JSON with class instantiation
  • Any valid JSON is valid TRON (no header required for primitive/array values).
  • Class names are uppercase letters: A–Z, then AA, AB, …
  • Nested objects produce nested class instantiations: A("hello",B(1,2)).
  • The body uses standard JSON encoding for all values inside ().

Supported Python versions

Python 3.9, 3.10, 3.11, 3.12 (tested in CI).


Development

git clone https://github.com/tron-format/tron-python
cd tron-python
pip install -e ".[dev]"

# Run the full test suite
pytest

# With coverage
pytest --cov=tron --cov-report=term-missing

# Lint
ruff check src tests

# Type-check
mypy src

Project layout

tron-python/
├── src/tron/
│   ├── __init__.py      Public API
│   ├── _stringify.py    Two-pass serializer
│   ├── _parse.py        Recursive-descent parser
│   ├── _benchmark.py    Benchmark utilities
│   ├── _utils.py        Shared helpers
│   └── py.typed         PEP 561 marker
├── tests/
│   ├── test_stringify.py
│   ├── test_parse.py
│   ├── test_roundtrip.py
│   └── test_edge_cases.py
└── pyproject.toml

Contributing

  1. Fork the repo and create a feature branch.
  2. Write tests before code — all new behaviour must be covered.
  3. Run ruff check, mypy src, and pytest before opening a PR.
  4. Follow Conventional Commits for commit messages.

Please open an issue before starting work on a significant change so we can align on the approach.


Acknowledgements

TRON was created by Tim Huang.
See the official specification and the reference JavaScript SDK.


License

MIT — see LICENSE.

Metadata

Release files for tron-python 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tron-python 0.1.0
File Size Uploaded
tron_python-0.1.0.tar.gz 22.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tron-python 0.1.0
File Interpreter ABI Platform
tron_python-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.5 kB

Release files / tron_python-0.1.0.tar.gz

Download URL tron_python-0.1.0.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3bf346a9928f0bce700f53847d9cffffac42c520da0cf687f155f20a61706679
BLAKE2b-256 checksum
How to use checksums
5e9a43ed06b796c13d7bfeda87dee4041c435b8b86190819d2cf7a8ea6eb8094
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 20, 2026.

Transparency log

Release files / tron_python-0.1.0-py3-none-any.whl

Download URL tron_python-0.1.0-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df919d50f963b69759a68e5d9653ad1cf072b1fc268bf736cb052a397a6b4969
BLAKE2b-256 checksum
How to use checksums
bdc5752a347b65ef4f86ae01cdba32638c7002fcf51dcf3fdc417483a42811d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 20, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page