Skip to main content

Python SDK for the TRON (Token Reduced Object Notation) format — compact, LLM-friendly JSON superset

Project description

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/Infnull)
str JSON-quoted string
list, tuple JSON array
dict ClassName(v1,v2,…) with header
datetime, date ISO-8601 string
Decimal float (NaN/Infnull)
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.

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

tron_python-0.1.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

tron_python-0.1.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tron_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3bf346a9928f0bce700f53847d9cffffac42c520da0cf687f155f20a61706679
MD5 ed77e3a263ad66d0028237a4d27bc870
BLAKE2b-256 5e9a43ed06b796c13d7bfeda87dee4041c435b8b86190819d2cf7a8ea6eb8094

See more details on using hashes here.

Provenance

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

Publisher: ci.yml on harishkr-git/tron-python

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

File details

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

File metadata

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

File hashes

Hashes for tron_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df919d50f963b69759a68e5d9653ad1cf072b1fc268bf736cb052a397a6b4969
MD5 0f30b955940f7537b4118befed2e5151
BLAKE2b-256 bdc5752a347b65ef4f86ae01cdba32638c7002fcf51dcf3fdc417483a42811d5

See more details on using hashes here.

Provenance

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

Publisher: ci.yml on harishkr-git/tron-python

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