tknpack 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tknpack-0.1.0/.gitignore +75 -0
- tknpack-0.1.0/LICENSE +21 -0
- tknpack-0.1.0/PKG-INFO +273 -0
- tknpack-0.1.0/README.md +246 -0
- tknpack-0.1.0/pyproject.toml +81 -0
- tknpack-0.1.0/src/tknpack/__init__.py +63 -0
- tknpack-0.1.0/src/tknpack/ai.py +95 -0
- tknpack-0.1.0/src/tknpack/core/__init__.py +23 -0
- tknpack-0.1.0/src/tknpack/core/codec.py +33 -0
- tknpack-0.1.0/src/tknpack/core/errors.py +14 -0
- tknpack-0.1.0/src/tknpack/core/ploon/__init__.py +4 -0
- tknpack-0.1.0/src/tknpack/core/ploon/decoder.py +369 -0
- tknpack-0.1.0/src/tknpack/core/ploon/encoder.py +214 -0
- tknpack-0.1.0/src/tknpack/core/ploon/schema.py +223 -0
- tknpack-0.1.0/src/tknpack/core/toon/__init__.py +4 -0
- tknpack-0.1.0/src/tknpack/core/toon/decoder.py +466 -0
- tknpack-0.1.0/src/tknpack/core/toon/encoder.py +290 -0
- tknpack-0.1.0/src/tknpack/core/types.py +20 -0
tknpack-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
.idea
|
|
6
|
+
media
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
env/
|
|
14
|
+
build/
|
|
15
|
+
develop-eggs/
|
|
16
|
+
dist/
|
|
17
|
+
downloads/
|
|
18
|
+
eggs/
|
|
19
|
+
.eggs/
|
|
20
|
+
parts/
|
|
21
|
+
sdist/
|
|
22
|
+
var/
|
|
23
|
+
*.egg-info/
|
|
24
|
+
.installed.cfg
|
|
25
|
+
*.egg
|
|
26
|
+
|
|
27
|
+
configs/local_settings.py
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
*.manifest
|
|
31
|
+
*.spec
|
|
32
|
+
|
|
33
|
+
# Installer logs
|
|
34
|
+
pip-log.txt
|
|
35
|
+
pip-delete-this-directory.txt
|
|
36
|
+
|
|
37
|
+
# Unit test / coverage reports
|
|
38
|
+
htmlcov/
|
|
39
|
+
.tox/
|
|
40
|
+
.coverage
|
|
41
|
+
.coverage.*
|
|
42
|
+
.cache
|
|
43
|
+
nosetests.xml
|
|
44
|
+
coverage.xml
|
|
45
|
+
*,cover
|
|
46
|
+
.hypothesis/
|
|
47
|
+
|
|
48
|
+
# Translations
|
|
49
|
+
*.pot
|
|
50
|
+
|
|
51
|
+
# Django stuff:
|
|
52
|
+
*.log
|
|
53
|
+
|
|
54
|
+
# Sphinx documentation
|
|
55
|
+
docs/_build/
|
|
56
|
+
|
|
57
|
+
# PyBuilder
|
|
58
|
+
target/
|
|
59
|
+
|
|
60
|
+
#Ipython Notebook
|
|
61
|
+
.ipynb_checkpoints
|
|
62
|
+
venv/*
|
|
63
|
+
env/*
|
|
64
|
+
.venv/*
|
|
65
|
+
.env/*
|
|
66
|
+
|
|
67
|
+
# Idea modules
|
|
68
|
+
*.iml
|
|
69
|
+
|
|
70
|
+
.ruff_cache
|
|
71
|
+
.pytest_cache
|
|
72
|
+
.mypy_cache
|
|
73
|
+
|
|
74
|
+
.claude/
|
|
75
|
+
CLAUDE.md
|
tknpack-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
tknpack-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tknpack
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Token-optimized serialization for Pydantic models using TOON and PLOON formats
|
|
5
|
+
Project-URL: Homepage, https://github.com/bigbag/tknpack
|
|
6
|
+
Project-URL: Repository, https://github.com/bigbag/tknpack
|
|
7
|
+
Project-URL: Issues, https://github.com/bigbag/tknpack/issues
|
|
8
|
+
Author-email: Pavel Liashkov <pavel.liashkov@protonmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: llm,ploon,pydantic,serialization,token-optimization,toon
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Topic :: Text Processing :: General
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Requires-Dist: pydantic>=2.0
|
|
24
|
+
Provides-Extra: ai
|
|
25
|
+
Requires-Dist: pydantic-ai>=1.0; extra == 'ai'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# tknpack
|
|
29
|
+
|
|
30
|
+
[](https://github.com/bigbag/tknpack/actions?query=workflow%3ACI)
|
|
31
|
+
[](https://pypi.python.org/pypi/tknpack)
|
|
32
|
+
[](https://pypistats.org/packages/tknpack)
|
|
33
|
+
[](https://github.com/bigbag/tknpack)
|
|
34
|
+
[](https://github.com/bigbag/tknpack/blob/master/LICENSE)
|
|
35
|
+
|
|
36
|
+
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.
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
- **Dual format support** - TOON (indentation-based) and PLOON (path-based) serialization
|
|
41
|
+
- **Pydantic v2 integration** - `encode()` / `decode()` for any `BaseModel`
|
|
42
|
+
- **pydantic-ai wrapper** - `TokenPackModel` transparently encodes tool results before sending to LLMs
|
|
43
|
+
- **Unified Codec** - Single `Codec` class for both formats with configurable options
|
|
44
|
+
- **Round-trip safe** - Encode and decode without data loss
|
|
45
|
+
- **Zero extra dependencies** - Core requires only `pydantic>=2.0`
|
|
46
|
+
|
|
47
|
+
## Quick Start
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from tknpack import encode, decode, Format
|
|
51
|
+
from pydantic import BaseModel
|
|
52
|
+
|
|
53
|
+
class User(BaseModel):
|
|
54
|
+
name: str
|
|
55
|
+
age: int
|
|
56
|
+
active: bool
|
|
57
|
+
|
|
58
|
+
user = User(name="Ada", age=30, active=True)
|
|
59
|
+
|
|
60
|
+
# TOON encoding (default)
|
|
61
|
+
toon = encode(user)
|
|
62
|
+
print(toon)
|
|
63
|
+
# name: Ada
|
|
64
|
+
# age: 30
|
|
65
|
+
# active: true
|
|
66
|
+
|
|
67
|
+
# PLOON encoding
|
|
68
|
+
ploon = encode(user, format=Format.PLOON)
|
|
69
|
+
print(ploon)
|
|
70
|
+
# [root#1](name,age,active)
|
|
71
|
+
#
|
|
72
|
+
# 1:1|Ada|30|true
|
|
73
|
+
|
|
74
|
+
# Decode back to Pydantic model
|
|
75
|
+
restored = decode(toon, User)
|
|
76
|
+
assert restored == user
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Installation
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# Install with pip
|
|
83
|
+
pip install tknpack
|
|
84
|
+
|
|
85
|
+
# Or install with uv
|
|
86
|
+
uv add tknpack
|
|
87
|
+
|
|
88
|
+
# With pydantic-ai support (optional)
|
|
89
|
+
pip install tknpack[ai]
|
|
90
|
+
# or
|
|
91
|
+
uv add tknpack[ai]
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The `ai` extra installs `pydantic-ai>=2.0`, required for the `TokenPackModel` wrapper.
|
|
95
|
+
|
|
96
|
+
## Usage
|
|
97
|
+
|
|
98
|
+
### Codec API
|
|
99
|
+
|
|
100
|
+
The `Codec` class provides a unified interface for both formats:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from tknpack.core import Codec, Format, ToonOptions, PloonOptions
|
|
104
|
+
|
|
105
|
+
# TOON with defaults
|
|
106
|
+
codec = Codec()
|
|
107
|
+
encoded = codec.encode({"id": 1, "name": "Ada"})
|
|
108
|
+
decoded = codec.decode(encoded)
|
|
109
|
+
|
|
110
|
+
# PLOON with defaults
|
|
111
|
+
codec = Codec(Format.PLOON)
|
|
112
|
+
encoded = codec.encode({"users": [{"id": 1, "name": "Ada"}]})
|
|
113
|
+
|
|
114
|
+
# Custom options
|
|
115
|
+
codec = Codec(Format.TOON, ToonOptions(indent=4, delimiter="|"))
|
|
116
|
+
codec = Codec(Format.PLOON, PloonOptions(compact=True))
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Pydantic Models
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from tknpack import encode, decode, Format
|
|
123
|
+
|
|
124
|
+
# TOON (default)
|
|
125
|
+
toon_str = encode(model)
|
|
126
|
+
restored = decode(toon_str, MyModel)
|
|
127
|
+
|
|
128
|
+
# PLOON
|
|
129
|
+
ploon_str = encode(model, format=Format.PLOON)
|
|
130
|
+
restored = decode(ploon_str, MyModel, format=Format.PLOON)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### pydantic-ai Integration
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from pydantic_ai import Agent
|
|
137
|
+
from pydantic_ai.models.openai import OpenAIModel
|
|
138
|
+
from tknpack.ai import TokenPackModel
|
|
139
|
+
from tknpack.core import Format, PloonOptions
|
|
140
|
+
|
|
141
|
+
# TOON encoding (default)
|
|
142
|
+
model = TokenPackModel(OpenAIModel("gpt-4o"))
|
|
143
|
+
|
|
144
|
+
# PLOON encoding with compact format
|
|
145
|
+
model = TokenPackModel(
|
|
146
|
+
OpenAIModel("gpt-4o"),
|
|
147
|
+
format=Format.PLOON,
|
|
148
|
+
options=PloonOptions(compact=True),
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
agent = Agent(model=model)
|
|
152
|
+
# Tool results are automatically encoded before hitting the LLM API
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Format Comparison
|
|
156
|
+
|
|
157
|
+
### TOON ([Text Object Oriented Notation](https://github.com/ulpi-io/toon-spec))
|
|
158
|
+
|
|
159
|
+
Indentation-based, human-readable format:
|
|
160
|
+
|
|
161
|
+
```
|
|
162
|
+
name: tknpack
|
|
163
|
+
tasks[3,]{id,title,completed}:
|
|
164
|
+
1,Implement encoder,true
|
|
165
|
+
2,Write tests,true
|
|
166
|
+
3,Add docs,false
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### PLOON ([Path-Level Object Oriented Notation](https://www.ploon.ai))
|
|
170
|
+
|
|
171
|
+
Path-based format with single schema declaration for maximum token efficiency:
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
[tasks#3](id,title,completed)
|
|
175
|
+
|
|
176
|
+
1:1|1|Implement encoder|true
|
|
177
|
+
1:2|2|Write tests|true
|
|
178
|
+
1:3|3|Add docs|false
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Token Savings
|
|
182
|
+
|
|
183
|
+
- **TOON** - ~50% reduction vs JSON
|
|
184
|
+
- **PLOON** - ~60% reduction vs JSON
|
|
185
|
+
|
|
186
|
+
## Configuration Options
|
|
187
|
+
|
|
188
|
+
### ToonOptions
|
|
189
|
+
|
|
190
|
+
- `indent` (int, default `2`) - Indentation spaces
|
|
191
|
+
- `delimiter` (str, default `,`) - Field delimiter character
|
|
192
|
+
|
|
193
|
+
### PloonOptions
|
|
194
|
+
|
|
195
|
+
- `field_delimiter` (str, default `|`) - Separator between values
|
|
196
|
+
- `path_separator` (str, default `:`) - Separator in array paths (depth:index)
|
|
197
|
+
- `compact` (bool, default `False`) - Semicolon-separated compact format
|
|
198
|
+
|
|
199
|
+
## Project Structure
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
src/tknpack/
|
|
203
|
+
__init__.py # Public API: encode(), decode()
|
|
204
|
+
ai.py # TokenPackModel for pydantic-ai
|
|
205
|
+
core/
|
|
206
|
+
__init__.py # Re-exports
|
|
207
|
+
codec.py # Unified Codec class
|
|
208
|
+
types.py # Format, ToonOptions, PloonOptions
|
|
209
|
+
errors.py # Error types
|
|
210
|
+
toon/
|
|
211
|
+
encoder.py # TOON encoder
|
|
212
|
+
decoder.py # TOON decoder
|
|
213
|
+
ploon/
|
|
214
|
+
schema.py # PLOON schema builder/parser
|
|
215
|
+
encoder.py # PLOON encoder
|
|
216
|
+
decoder.py # PLOON decoder
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Development
|
|
220
|
+
|
|
221
|
+
### Setup
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
git clone https://github.com/bigbag/tknpack.git
|
|
225
|
+
cd tknpack
|
|
226
|
+
make venv/create
|
|
227
|
+
make venv/install/all
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Commands
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
make test # Run tests with coverage
|
|
234
|
+
make lint # Run ruff + mypy
|
|
235
|
+
make format # Format code with ruff
|
|
236
|
+
make clean # Clean cache and build files
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Running Tests
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
# Run all tests with coverage
|
|
243
|
+
uv run pytest --cov=tknpack --cov-report=term-missing
|
|
244
|
+
|
|
245
|
+
# Run specific test file
|
|
246
|
+
uv run pytest tests/test_ploon_encoder.py -v
|
|
247
|
+
|
|
248
|
+
# Run specific test class or method
|
|
249
|
+
uv run pytest tests/test_encoder.py::TestFlatObjects -v
|
|
250
|
+
uv run pytest tests/test_decoder.py::TestRoundTrip::test_simple_object -v
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## API Reference
|
|
254
|
+
|
|
255
|
+
### `encode(model, options=None, *, format=Format.TOON) -> str`
|
|
256
|
+
|
|
257
|
+
Encode a Pydantic model to TOON or PLOON format.
|
|
258
|
+
|
|
259
|
+
### `decode(text, model_class, *, format=Format.TOON) -> T`
|
|
260
|
+
|
|
261
|
+
Decode a TOON or PLOON string back to a Pydantic model.
|
|
262
|
+
|
|
263
|
+
### `Codec(format=Format.TOON, options=None)`
|
|
264
|
+
|
|
265
|
+
Unified encoder/decoder for raw Python dicts and lists.
|
|
266
|
+
|
|
267
|
+
### `TokenPackModel(wrapped, *, format=Format.TOON, options=None)`
|
|
268
|
+
|
|
269
|
+
pydantic-ai model wrapper that encodes tool results automatically.
|
|
270
|
+
|
|
271
|
+
## License
|
|
272
|
+
|
|
273
|
+
MIT License - see [LICENSE](LICENSE) file.
|
tknpack-0.1.0/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# tknpack
|
|
2
|
+
|
|
3
|
+
[](https://github.com/bigbag/tknpack/actions?query=workflow%3ACI)
|
|
4
|
+
[](https://pypi.python.org/pypi/tknpack)
|
|
5
|
+
[](https://pypistats.org/packages/tknpack)
|
|
6
|
+
[](https://github.com/bigbag/tknpack)
|
|
7
|
+
[](https://github.com/bigbag/tknpack/blob/master/LICENSE)
|
|
8
|
+
|
|
9
|
+
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.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Dual format support** - TOON (indentation-based) and PLOON (path-based) serialization
|
|
14
|
+
- **Pydantic v2 integration** - `encode()` / `decode()` for any `BaseModel`
|
|
15
|
+
- **pydantic-ai wrapper** - `TokenPackModel` transparently encodes tool results before sending to LLMs
|
|
16
|
+
- **Unified Codec** - Single `Codec` class for both formats with configurable options
|
|
17
|
+
- **Round-trip safe** - Encode and decode without data loss
|
|
18
|
+
- **Zero extra dependencies** - Core requires only `pydantic>=2.0`
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from tknpack import encode, decode, Format
|
|
24
|
+
from pydantic import BaseModel
|
|
25
|
+
|
|
26
|
+
class User(BaseModel):
|
|
27
|
+
name: str
|
|
28
|
+
age: int
|
|
29
|
+
active: bool
|
|
30
|
+
|
|
31
|
+
user = User(name="Ada", age=30, active=True)
|
|
32
|
+
|
|
33
|
+
# TOON encoding (default)
|
|
34
|
+
toon = encode(user)
|
|
35
|
+
print(toon)
|
|
36
|
+
# name: Ada
|
|
37
|
+
# age: 30
|
|
38
|
+
# active: true
|
|
39
|
+
|
|
40
|
+
# PLOON encoding
|
|
41
|
+
ploon = encode(user, format=Format.PLOON)
|
|
42
|
+
print(ploon)
|
|
43
|
+
# [root#1](name,age,active)
|
|
44
|
+
#
|
|
45
|
+
# 1:1|Ada|30|true
|
|
46
|
+
|
|
47
|
+
# Decode back to Pydantic model
|
|
48
|
+
restored = decode(toon, User)
|
|
49
|
+
assert restored == user
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Install with pip
|
|
56
|
+
pip install tknpack
|
|
57
|
+
|
|
58
|
+
# Or install with uv
|
|
59
|
+
uv add tknpack
|
|
60
|
+
|
|
61
|
+
# With pydantic-ai support (optional)
|
|
62
|
+
pip install tknpack[ai]
|
|
63
|
+
# or
|
|
64
|
+
uv add tknpack[ai]
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The `ai` extra installs `pydantic-ai>=2.0`, required for the `TokenPackModel` wrapper.
|
|
68
|
+
|
|
69
|
+
## Usage
|
|
70
|
+
|
|
71
|
+
### Codec API
|
|
72
|
+
|
|
73
|
+
The `Codec` class provides a unified interface for both formats:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from tknpack.core import Codec, Format, ToonOptions, PloonOptions
|
|
77
|
+
|
|
78
|
+
# TOON with defaults
|
|
79
|
+
codec = Codec()
|
|
80
|
+
encoded = codec.encode({"id": 1, "name": "Ada"})
|
|
81
|
+
decoded = codec.decode(encoded)
|
|
82
|
+
|
|
83
|
+
# PLOON with defaults
|
|
84
|
+
codec = Codec(Format.PLOON)
|
|
85
|
+
encoded = codec.encode({"users": [{"id": 1, "name": "Ada"}]})
|
|
86
|
+
|
|
87
|
+
# Custom options
|
|
88
|
+
codec = Codec(Format.TOON, ToonOptions(indent=4, delimiter="|"))
|
|
89
|
+
codec = Codec(Format.PLOON, PloonOptions(compact=True))
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Pydantic Models
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from tknpack import encode, decode, Format
|
|
96
|
+
|
|
97
|
+
# TOON (default)
|
|
98
|
+
toon_str = encode(model)
|
|
99
|
+
restored = decode(toon_str, MyModel)
|
|
100
|
+
|
|
101
|
+
# PLOON
|
|
102
|
+
ploon_str = encode(model, format=Format.PLOON)
|
|
103
|
+
restored = decode(ploon_str, MyModel, format=Format.PLOON)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### pydantic-ai Integration
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from pydantic_ai import Agent
|
|
110
|
+
from pydantic_ai.models.openai import OpenAIModel
|
|
111
|
+
from tknpack.ai import TokenPackModel
|
|
112
|
+
from tknpack.core import Format, PloonOptions
|
|
113
|
+
|
|
114
|
+
# TOON encoding (default)
|
|
115
|
+
model = TokenPackModel(OpenAIModel("gpt-4o"))
|
|
116
|
+
|
|
117
|
+
# PLOON encoding with compact format
|
|
118
|
+
model = TokenPackModel(
|
|
119
|
+
OpenAIModel("gpt-4o"),
|
|
120
|
+
format=Format.PLOON,
|
|
121
|
+
options=PloonOptions(compact=True),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
agent = Agent(model=model)
|
|
125
|
+
# Tool results are automatically encoded before hitting the LLM API
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Format Comparison
|
|
129
|
+
|
|
130
|
+
### TOON ([Text Object Oriented Notation](https://github.com/ulpi-io/toon-spec))
|
|
131
|
+
|
|
132
|
+
Indentation-based, human-readable format:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
name: tknpack
|
|
136
|
+
tasks[3,]{id,title,completed}:
|
|
137
|
+
1,Implement encoder,true
|
|
138
|
+
2,Write tests,true
|
|
139
|
+
3,Add docs,false
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### PLOON ([Path-Level Object Oriented Notation](https://www.ploon.ai))
|
|
143
|
+
|
|
144
|
+
Path-based format with single schema declaration for maximum token efficiency:
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
[tasks#3](id,title,completed)
|
|
148
|
+
|
|
149
|
+
1:1|1|Implement encoder|true
|
|
150
|
+
1:2|2|Write tests|true
|
|
151
|
+
1:3|3|Add docs|false
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Token Savings
|
|
155
|
+
|
|
156
|
+
- **TOON** - ~50% reduction vs JSON
|
|
157
|
+
- **PLOON** - ~60% reduction vs JSON
|
|
158
|
+
|
|
159
|
+
## Configuration Options
|
|
160
|
+
|
|
161
|
+
### ToonOptions
|
|
162
|
+
|
|
163
|
+
- `indent` (int, default `2`) - Indentation spaces
|
|
164
|
+
- `delimiter` (str, default `,`) - Field delimiter character
|
|
165
|
+
|
|
166
|
+
### PloonOptions
|
|
167
|
+
|
|
168
|
+
- `field_delimiter` (str, default `|`) - Separator between values
|
|
169
|
+
- `path_separator` (str, default `:`) - Separator in array paths (depth:index)
|
|
170
|
+
- `compact` (bool, default `False`) - Semicolon-separated compact format
|
|
171
|
+
|
|
172
|
+
## Project Structure
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
src/tknpack/
|
|
176
|
+
__init__.py # Public API: encode(), decode()
|
|
177
|
+
ai.py # TokenPackModel for pydantic-ai
|
|
178
|
+
core/
|
|
179
|
+
__init__.py # Re-exports
|
|
180
|
+
codec.py # Unified Codec class
|
|
181
|
+
types.py # Format, ToonOptions, PloonOptions
|
|
182
|
+
errors.py # Error types
|
|
183
|
+
toon/
|
|
184
|
+
encoder.py # TOON encoder
|
|
185
|
+
decoder.py # TOON decoder
|
|
186
|
+
ploon/
|
|
187
|
+
schema.py # PLOON schema builder/parser
|
|
188
|
+
encoder.py # PLOON encoder
|
|
189
|
+
decoder.py # PLOON decoder
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Development
|
|
193
|
+
|
|
194
|
+
### Setup
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
git clone https://github.com/bigbag/tknpack.git
|
|
198
|
+
cd tknpack
|
|
199
|
+
make venv/create
|
|
200
|
+
make venv/install/all
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Commands
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
make test # Run tests with coverage
|
|
207
|
+
make lint # Run ruff + mypy
|
|
208
|
+
make format # Format code with ruff
|
|
209
|
+
make clean # Clean cache and build files
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Running Tests
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
# Run all tests with coverage
|
|
216
|
+
uv run pytest --cov=tknpack --cov-report=term-missing
|
|
217
|
+
|
|
218
|
+
# Run specific test file
|
|
219
|
+
uv run pytest tests/test_ploon_encoder.py -v
|
|
220
|
+
|
|
221
|
+
# Run specific test class or method
|
|
222
|
+
uv run pytest tests/test_encoder.py::TestFlatObjects -v
|
|
223
|
+
uv run pytest tests/test_decoder.py::TestRoundTrip::test_simple_object -v
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## API Reference
|
|
227
|
+
|
|
228
|
+
### `encode(model, options=None, *, format=Format.TOON) -> str`
|
|
229
|
+
|
|
230
|
+
Encode a Pydantic model to TOON or PLOON format.
|
|
231
|
+
|
|
232
|
+
### `decode(text, model_class, *, format=Format.TOON) -> T`
|
|
233
|
+
|
|
234
|
+
Decode a TOON or PLOON string back to a Pydantic model.
|
|
235
|
+
|
|
236
|
+
### `Codec(format=Format.TOON, options=None)`
|
|
237
|
+
|
|
238
|
+
Unified encoder/decoder for raw Python dicts and lists.
|
|
239
|
+
|
|
240
|
+
### `TokenPackModel(wrapped, *, format=Format.TOON, options=None)`
|
|
241
|
+
|
|
242
|
+
pydantic-ai model wrapper that encodes tool results automatically.
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
|
|
246
|
+
MIT License - see [LICENSE](LICENSE) file.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "tknpack"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Token-optimized serialization for Pydantic models using TOON and PLOON formats"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
requires-python = ">=3.10"
|
|
8
|
+
authors = [{ name = "Pavel Liashkov", email = "pavel.liashkov@protonmail.com" }]
|
|
9
|
+
keywords = ["toon", "ploon", "serialization", "pydantic", "llm", "token-optimization"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 3 - Alpha",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.10",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Programming Language :: Python :: 3.14",
|
|
19
|
+
"Topic :: Text Processing :: General",
|
|
20
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
21
|
+
]
|
|
22
|
+
dependencies = ["pydantic>=2.0"]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
ai = ["pydantic-ai>=1.0"]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/bigbag/tknpack"
|
|
29
|
+
Repository = "https://github.com/bigbag/tknpack"
|
|
30
|
+
Issues = "https://github.com/bigbag/tknpack/issues"
|
|
31
|
+
|
|
32
|
+
[build-system]
|
|
33
|
+
requires = ["hatchling"]
|
|
34
|
+
build-backend = "hatchling.build"
|
|
35
|
+
|
|
36
|
+
[tool.hatch.build.targets.wheel]
|
|
37
|
+
packages = ["src/tknpack"]
|
|
38
|
+
|
|
39
|
+
[tool.hatch.build.targets.sdist]
|
|
40
|
+
include = ["src/tknpack"]
|
|
41
|
+
|
|
42
|
+
[dependency-groups]
|
|
43
|
+
dev = [
|
|
44
|
+
"mypy>=1.16.0",
|
|
45
|
+
"pyclean>=3.1.0",
|
|
46
|
+
"pytest>=8.4.0",
|
|
47
|
+
"pytest-asyncio>=0.24",
|
|
48
|
+
"pytest-cov>=6.1.1",
|
|
49
|
+
"ruff>=0.9.0",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.ruff]
|
|
53
|
+
line-length = 120
|
|
54
|
+
target-version = "py310"
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint]
|
|
57
|
+
select = ["E", "F", "W", "I", "B", "C4", "UP", "SIM"]
|
|
58
|
+
ignore = ["E203", "E501", "B008"]
|
|
59
|
+
|
|
60
|
+
[tool.ruff.format]
|
|
61
|
+
quote-style = "double"
|
|
62
|
+
indent-style = "space"
|
|
63
|
+
skip-magic-trailing-comma = false
|
|
64
|
+
line-ending = "auto"
|
|
65
|
+
|
|
66
|
+
[tool.pytest.ini_options]
|
|
67
|
+
testpaths = ["tests"]
|
|
68
|
+
python_files = ["test_*.py"]
|
|
69
|
+
python_functions = ["test_*"]
|
|
70
|
+
asyncio_mode = "auto"
|
|
71
|
+
addopts = "-v --tb=short --cov=tknpack --cov-report=term-missing"
|
|
72
|
+
|
|
73
|
+
[tool.mypy]
|
|
74
|
+
python_version = "3.10"
|
|
75
|
+
warn_return_any = true
|
|
76
|
+
warn_unused_configs = true
|
|
77
|
+
ignore_missing_imports = true
|
|
78
|
+
|
|
79
|
+
[tool.coverage.run]
|
|
80
|
+
source = ["src/tknpack"]
|
|
81
|
+
omit = ["tests/*"]
|