Token-optimized serialization for Pydantic models using TOON and PLOON formats
Project description
tknpack
Token-optimized serialization for Pydantic models using TOON and PLOON formats. Reduce LLM token usage by up to 60% compared to JSON while maintaining full round-trip fidelity.
Features
- Dual format support - TOON (indentation-based) and PLOON (path-based) serialization
- Pydantic v2 integration -
encode()/decode()for anyBaseModel - pydantic-ai wrapper -
TokenPackModeltransparently encodes tool results before sending to LLMs - Unified Codec - Single
Codecclass for both formats with configurable options - Round-trip safe - Encode and decode without data loss
- Zero extra dependencies - Core requires only
pydantic>=2.0
Quick Start
from tknpack import encode, decode, Format
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
active: bool
user = User(name="Ada", age=30, active=True)
# TOON encoding (default)
toon = encode(user)
print(toon)
# name: Ada
# age: 30
# active: true
# PLOON encoding
ploon = encode(user, format=Format.PLOON)
print(ploon)
# [root#1](name,age,active)
#
# 1:1|Ada|30|true
# Decode back to Pydantic model
restored = decode(toon, User)
assert restored == user
Installation
# Install with pip
pip install tknpack
# Or install with uv
uv add tknpack
# With pydantic-ai support (optional)
pip install tknpack[ai]
# or
uv add tknpack[ai]
The ai extra installs pydantic-ai>=2.0, required for the TokenPackModel wrapper.
Usage
Codec API
The Codec class provides a unified interface for both formats:
from tknpack.core import Codec, Format, ToonOptions, PloonOptions
# TOON with defaults
codec = Codec()
encoded = codec.encode({"id": 1, "name": "Ada"})
decoded = codec.decode(encoded)
# PLOON with defaults
codec = Codec(Format.PLOON)
encoded = codec.encode({"users": [{"id": 1, "name": "Ada"}]})
# Custom options
codec = Codec(Format.TOON, ToonOptions(indent=4, delimiter="|"))
codec = Codec(Format.PLOON, PloonOptions(compact=True))
Pydantic Models
from tknpack import encode, decode, Format
# TOON (default)
toon_str = encode(model)
restored = decode(toon_str, MyModel)
# PLOON
ploon_str = encode(model, format=Format.PLOON)
restored = decode(ploon_str, MyModel, format=Format.PLOON)
pydantic-ai Integration
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from tknpack.ai import TokenPackModel
from tknpack.core import Format, PloonOptions
# TOON encoding (default)
model = TokenPackModel(OpenAIModel("gpt-4o"))
# PLOON encoding with compact format
model = TokenPackModel(
OpenAIModel("gpt-4o"),
format=Format.PLOON,
options=PloonOptions(compact=True),
)
agent = Agent(model=model)
# Tool results are automatically encoded before hitting the LLM API
Format Comparison
TOON (Text Object Oriented Notation)
Indentation-based, human-readable format:
name: tknpack
tasks[3,]{id,title,completed}:
1,Implement encoder,true
2,Write tests,true
3,Add docs,false
PLOON (Path-Level Object Oriented Notation)
Path-based format with single schema declaration for maximum token efficiency:
[tasks#3](id,title,completed)
1:1|1|Implement encoder|true
1:2|2|Write tests|true
1:3|3|Add docs|false
Token Savings
- TOON - ~50% reduction vs JSON
- PLOON - ~60% reduction vs JSON
Configuration Options
ToonOptions
indent(int, default2) - Indentation spacesdelimiter(str, default,) - Field delimiter character
PloonOptions
field_delimiter(str, default|) - Separator between valuespath_separator(str, default:) - Separator in array paths (depth:index)compact(bool, defaultFalse) - Semicolon-separated compact format
Project Structure
src/tknpack/
__init__.py # Public API: encode(), decode()
ai.py # TokenPackModel for pydantic-ai
core/
__init__.py # Re-exports
codec.py # Unified Codec class
types.py # Format, ToonOptions, PloonOptions
errors.py # Error types
toon/
encoder.py # TOON encoder
decoder.py # TOON decoder
ploon/
schema.py # PLOON schema builder/parser
encoder.py # PLOON encoder
decoder.py # PLOON decoder
Development
Setup
git clone https://github.com/bigbag/tknpack.git
cd tknpack
make venv/create
make venv/install/all
Commands
make test # Run tests with coverage
make lint # Run ruff + mypy
make format # Format code with ruff
make clean # Clean cache and build files
Running Tests
# Run all tests with coverage
uv run pytest --cov=tknpack --cov-report=term-missing
# Run specific test file
uv run pytest tests/test_ploon_encoder.py -v
# Run specific test class or method
uv run pytest tests/test_encoder.py::TestFlatObjects -v
uv run pytest tests/test_decoder.py::TestRoundTrip::test_simple_object -v
API Reference
encode(model, options=None, *, format=Format.TOON) -> str
Encode a Pydantic model to TOON or PLOON format.
decode(text, model_class, *, format=Format.TOON) -> T
Decode a TOON or PLOON string back to a Pydantic model.
Codec(format=Format.TOON, options=None)
Unified encoder/decoder for raw Python dicts and lists.
TokenPackModel(wrapped, *, format=Format.TOON, options=None)
pydantic-ai model wrapper that encodes tool results automatically.
License
MIT License - see LICENSE file.
Project details
Release history Release notifications | RSS feed
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 tknpack-0.1.0.tar.gz.
File metadata
- Download URL: tknpack-0.1.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d51e9f5c1e225f9c6f6c8736620311f4e976db687c2bb1c4a2d8d33cbe7b04b1
|
|
| MD5 |
829b4aff2b58cee66e03747d612162b8
|
|
| BLAKE2b-256 |
f47d84448b4e4c0a881ccc9b49395b4c7f036059b9f5ec1d1f3730cacdb23c22
|
Provenance
The following attestation bundles were made for tknpack-0.1.0.tar.gz:
Publisher:
publish.yml on bigbag/tknpack
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tknpack-0.1.0.tar.gz -
Subject digest:
d51e9f5c1e225f9c6f6c8736620311f4e976db687c2bb1c4a2d8d33cbe7b04b1 - Sigstore transparency entry: 1188061946
- Sigstore integration time:
-
Permalink:
bigbag/tknpack@6a481668109b6fb92998b22de03dd05cc34f4991 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/bigbag
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6a481668109b6fb92998b22de03dd05cc34f4991 -
Trigger Event:
push
-
Statement type:
File details
Details for the file tknpack-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tknpack-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f2e18a355154686b7e87558075b5d945757d0c5df641eda5090a4ce5ec5a67b
|
|
| MD5 |
5159bbfc0486ca336efd187f5feed257
|
|
| BLAKE2b-256 |
8270abee8ba3de477c27bb22e9b8903a79be393a4f640e396add418d47473b50
|
Provenance
The following attestation bundles were made for tknpack-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on bigbag/tknpack
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tknpack-0.1.0-py3-none-any.whl -
Subject digest:
7f2e18a355154686b7e87558075b5d945757d0c5df641eda5090a4ce5ec5a67b - Sigstore transparency entry: 1188062001
- Sigstore integration time:
-
Permalink:
bigbag/tknpack@6a481668109b6fb92998b22de03dd05cc34f4991 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/bigbag
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6a481668109b6fb92998b22de03dd05cc34f4991 -
Trigger Event:
push
-
Statement type: