terset 0.2.2__tar.gz → 0.2.3__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.
- {terset-0.2.2/src/terset.egg-info → terset-0.2.3}/PKG-INFO +91 -87
- {terset-0.2.2 → terset-0.2.3}/README.md +90 -86
- {terset-0.2.2 → terset-0.2.3}/pyproject.toml +1 -1
- {terset-0.2.2 → terset-0.2.3}/src/terset/_terset.py +62 -16
- {terset-0.2.2 → terset-0.2.3/src/terset.egg-info}/PKG-INFO +91 -87
- {terset-0.2.2 → terset-0.2.3}/tests/test_rules.py +8 -1
- {terset-0.2.2 → terset-0.2.3}/tests/test_terset.py +11 -0
- {terset-0.2.2 → terset-0.2.3}/LICENSE +0 -0
- {terset-0.2.2 → terset-0.2.3}/setup.cfg +0 -0
- {terset-0.2.2 → terset-0.2.3}/src/terset/__init__.py +0 -0
- {terset-0.2.2 → terset-0.2.3}/src/terset/py.typed +0 -0
- {terset-0.2.2 → terset-0.2.3}/src/terset.egg-info/SOURCES.txt +0 -0
- {terset-0.2.2 → terset-0.2.3}/src/terset.egg-info/dependency_links.txt +0 -0
- {terset-0.2.2 → terset-0.2.3}/src/terset.egg-info/top_level.txt +0 -0
- {terset-0.2.2 → terset-0.2.3}/tests/test_decoder_errors.py +0 -0
- {terset-0.2.2 → terset-0.2.3}/tests/test_header_depth.py +0 -0
- {terset-0.2.2 → terset-0.2.3}/tests/test_header_performance.py +0 -0
- {terset-0.2.2 → terset-0.2.3}/tests/test_long_quoted.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: terset
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: Encode JSON records as compact, typed tables.
|
|
5
5
|
Author: StackDeck
|
|
6
6
|
License-Expression: MIT
|
|
@@ -102,7 +102,7 @@ Zero dependencies. Python 3.10 and up. Type hints are included.
|
|
|
102
102
|
|
|
103
103
|
<!-- docs:use -->
|
|
104
104
|
The API exports `encode`, `decode`, `RULES` and `rules_for`.
|
|
105
|
-
`rules_for(text)` gives the
|
|
105
|
+
`rules_for(text)` gives only the rules sentences that text needs. `RULES` keeps the full rules.
|
|
106
106
|
|
|
107
107
|
```python
|
|
108
108
|
from terset import decode, encode, rules_for
|
|
@@ -118,10 +118,10 @@ data = {
|
|
|
118
118
|
|
|
119
119
|
text = encode(data)
|
|
120
120
|
|
|
121
|
-
#
|
|
121
|
+
# One text: send its fitted rules right before the data.
|
|
122
122
|
messages = [
|
|
123
|
-
{"role": "system", "content":
|
|
124
|
-
{"role": "user", "content": f"Which users have no role?\n\n{text}"},
|
|
123
|
+
{"role": "system", "content": "You read Terset data."},
|
|
124
|
+
{"role": "user", "content": f"Which users have no role?\n\n{rules_for(text)}\n{text}"},
|
|
125
125
|
]
|
|
126
126
|
|
|
127
127
|
print(text)
|
|
@@ -141,7 +141,7 @@ True
|
|
|
141
141
|
```
|
|
142
142
|
|
|
143
143
|
1. `encode(value)` turns a JSON object or array into Terset text.
|
|
144
|
-
2.
|
|
144
|
+
2. Send `rules_for(text)` right before the data. A shared or cached system prompt for many texts takes `RULES` instead.
|
|
145
145
|
3. `decode(text)` gives back the same value. Ask the model to answer in JSON, since Terset is for reading.
|
|
146
146
|
|
|
147
147
|
Never write Terset by hand. Always use `encode`.
|
|
@@ -180,49 +180,49 @@ for Claude Opus 5.5. They give different numbers for the same text.
|
|
|
180
180
|
|
|
181
181
|
| Case | Terset | JSON compact | YAML | TOON best | Terset vs JSON compact |
|
|
182
182
|
|---|---|---|---|---|---|
|
|
183
|
-
| UI text, uneven rows |
|
|
184
|
-
| UI text, uniform rows |
|
|
185
|
-
| Translation batch | 1,
|
|
186
|
-
| File list, short | 9,
|
|
187
|
-
| File list, long |
|
|
188
|
-
| 200 users |
|
|
189
|
-
| Metrics, numbers and booleans | 2,
|
|
190
|
-
| Deep nested config |
|
|
191
|
-
| Mixed-type array |
|
|
192
|
-
| Reviews, long text | 3,
|
|
193
|
-
| Orders with line items | 3,
|
|
194
|
-
| Total |
|
|
195
|
-
|
|
196
|
-
Terset, rules line included, is under TOON's best in
|
|
197
|
-
under compact JSON in
|
|
198
|
-
|
|
199
|
-
Over all 11 cases, Terset totals
|
|
200
|
-
compact (
|
|
201
|
-
YAML (
|
|
183
|
+
| UI text, uneven rows | 519 | 697 | 812 | 814 | -25.5% |
|
|
184
|
+
| UI text, uniform rows | 480 | 737 | 910 | 518 | -34.9% |
|
|
185
|
+
| Translation batch | 1,139 | 1,448 | 1,674 | 1,110 | -21.3% |
|
|
186
|
+
| File list, short | 9,608 | 12,808 | 15,304 | 10,315 | -25.0% |
|
|
187
|
+
| File list, long | 15,042 | 19,969 | 23,966 | 15,502 | -24.7% |
|
|
188
|
+
| 200 users | 5,404 | 9,051 | 10,924 | 5,589 | -40.3% |
|
|
189
|
+
| Metrics, numbers and booleans | 2,940 | 4,798 | 5,999 | 3,181 | -38.7% |
|
|
190
|
+
| Deep nested config | 250 | 251 | 316 | 296 | -0.4% |
|
|
191
|
+
| Mixed-type array | 63 | 63 | 89 | 99 | 0.0% |
|
|
192
|
+
| Reviews, long text | 3,700 | 4,308 | 5,006 | 3,662 | -14.1% |
|
|
193
|
+
| Orders with line items | 3,899 | 4,678 | 5,846 | 4,982 | -16.7% |
|
|
194
|
+
| Total | 43,044 | 58,808 | 70,846 | 46,068 | -26.8% |
|
|
195
|
+
|
|
196
|
+
Terset, fitted rules line included, is under TOON's best in 9 of 11
|
|
197
|
+
cases and under compact JSON in 10 of 11.
|
|
198
|
+
|
|
199
|
+
Over all 11 cases, Terset totals 43,044 tokens: 26.8% fewer than JSON
|
|
200
|
+
compact (58,808), 50.5% fewer than JSON pretty (87,020), 39.2% fewer than
|
|
201
|
+
YAML (70,846), and 6.6% fewer than TOON's best (46,068).
|
|
202
202
|
|
|
203
203
|
### Claude Opus 5.5
|
|
204
204
|
|
|
205
205
|
| Case | Terset | JSON compact | YAML | TOON best | Terset vs JSON compact |
|
|
206
206
|
|---|---|---|---|---|---|
|
|
207
|
-
| UI text, uneven rows |
|
|
208
|
-
| UI text, uniform rows |
|
|
209
|
-
| Translation batch |
|
|
210
|
-
| File list, short |
|
|
211
|
-
| File list, long |
|
|
212
|
-
| 200 users | 7,
|
|
213
|
-
| Metrics, numbers and booleans |
|
|
214
|
-
| Deep nested config |
|
|
215
|
-
| Mixed-type array |
|
|
216
|
-
| Reviews, long text |
|
|
217
|
-
| Orders with line items | 5,
|
|
218
|
-
| Total |
|
|
219
|
-
|
|
220
|
-
Terset, rules line included, is under TOON's best in
|
|
221
|
-
under compact JSON in
|
|
222
|
-
|
|
223
|
-
Over all 11 cases, Terset totals
|
|
224
|
-
compact (
|
|
225
|
-
YAML (
|
|
207
|
+
| UI text, uneven rows | 766 | 1,083 | 1,062 | 1,065 | -29.3% |
|
|
208
|
+
| UI text, uniform rows | 745 | 1,253 | 1,160 | 774 | -40.5% |
|
|
209
|
+
| Translation batch | 1,813 | 2,514 | 2,413 | 1,833 | -27.9% |
|
|
210
|
+
| File list, short | 11,972 | 18,018 | 18,012 | 12,671 | -33.6% |
|
|
211
|
+
| File list, long | 21,259 | 30,405 | 31,199 | 22,019 | -30.1% |
|
|
212
|
+
| 200 users | 7,665 | 12,819 | 13,214 | 7,836 | -40.2% |
|
|
213
|
+
| Metrics, numbers and booleans | 3,310 | 6,535 | 6,960 | 3,467 | -49.3% |
|
|
214
|
+
| Deep nested config | 402 | 405 | 412 | 394 | -0.7% |
|
|
215
|
+
| Mixed-type array | 89 | 89 | 101 | 118 | 0.0% |
|
|
216
|
+
| Reviews, long text | 4,899 | 5,979 | 6,404 | 4,915 | -18.1% |
|
|
217
|
+
| Orders with line items | 5,662 | 7,002 | 7,208 | 6,268 | -19.1% |
|
|
218
|
+
| Total | 58,582 | 86,102 | 88,145 | 61,360 | -32.0% |
|
|
219
|
+
|
|
220
|
+
Terset, fitted rules line included, is under TOON's best in 10 of 11
|
|
221
|
+
cases and under compact JSON in 10 of 11.
|
|
222
|
+
|
|
223
|
+
Over all 11 cases, Terset totals 58,582 tokens: 32.0% fewer than JSON
|
|
224
|
+
compact (86,102), 48.4% fewer than JSON pretty (113,574), 33.5% fewer than
|
|
225
|
+
YAML (88,145), and 4.5% fewer than TOON's best (61,360).
|
|
226
226
|
|
|
227
227
|
### Input cost at list price
|
|
228
228
|
|
|
@@ -233,49 +233,49 @@ Claude Opus 5.5 at $4 per million input tokens, on the Claude counts:
|
|
|
233
233
|
|
|
234
234
|
| Case | Terset | JSON compact | vs JSON compact |
|
|
235
235
|
|---|---|---|---|
|
|
236
|
-
| UI text, uneven rows | $
|
|
237
|
-
| UI text, uniform rows | $
|
|
238
|
-
| Translation batch | $
|
|
239
|
-
| File list, short | $
|
|
240
|
-
| File list, long | $
|
|
241
|
-
| 200 users | $
|
|
242
|
-
| Metrics, numbers and booleans | $
|
|
243
|
-
| Deep nested config | $
|
|
244
|
-
| Mixed-type array | $0.
|
|
245
|
-
| Reviews, long text | $
|
|
246
|
-
| Orders with line items | $22.
|
|
247
|
-
| Total | $
|
|
236
|
+
| UI text, uneven rows | $3.06 | $4.33 | -$1.27 |
|
|
237
|
+
| UI text, uniform rows | $2.98 | $5.01 | -$2.03 |
|
|
238
|
+
| Translation batch | $7.25 | $10.06 | -$2.81 |
|
|
239
|
+
| File list, short | $47.89 | $72.07 | -$24.18 |
|
|
240
|
+
| File list, long | $85.04 | $121.62 | -$36.58 |
|
|
241
|
+
| 200 users | $30.66 | $51.28 | -$20.62 |
|
|
242
|
+
| Metrics, numbers and booleans | $13.24 | $26.14 | -$12.90 |
|
|
243
|
+
| Deep nested config | $1.61 | $1.62 | -$0.01 |
|
|
244
|
+
| Mixed-type array | $0.36 | $0.36 | $0.00 |
|
|
245
|
+
| Reviews, long text | $19.60 | $23.92 | -$4.32 |
|
|
246
|
+
| Orders with line items | $22.65 | $28.01 | -$5.36 |
|
|
247
|
+
| Total | $234.33 | $344.41 | -$110.08 |
|
|
248
248
|
|
|
249
249
|
GPT 6.1 Sol at $2 per million input tokens, context up to 272K, on the
|
|
250
250
|
`o200k_base` counts:
|
|
251
251
|
|
|
252
252
|
| Case | Terset | JSON compact | vs JSON compact |
|
|
253
253
|
|---|---|---|---|
|
|
254
|
-
| UI text, uneven rows | $1.
|
|
255
|
-
| UI text, uniform rows | $
|
|
256
|
-
| Translation batch | $2.
|
|
257
|
-
| File list, short | $19.
|
|
258
|
-
| File list, long | $
|
|
259
|
-
| 200 users | $
|
|
260
|
-
| Metrics, numbers and booleans | $5.
|
|
261
|
-
| Deep nested config | $0.
|
|
262
|
-
| Mixed-type array | $0.
|
|
263
|
-
| Reviews, long text | $7.
|
|
264
|
-
| Orders with line items | $7.
|
|
265
|
-
| Total | $
|
|
254
|
+
| UI text, uneven rows | $1.04 | $1.39 | -$0.35 |
|
|
255
|
+
| UI text, uniform rows | $0.96 | $1.47 | -$0.51 |
|
|
256
|
+
| Translation batch | $2.28 | $2.90 | -$0.62 |
|
|
257
|
+
| File list, short | $19.22 | $25.62 | -$6.40 |
|
|
258
|
+
| File list, long | $30.08 | $39.94 | -$9.86 |
|
|
259
|
+
| 200 users | $10.81 | $18.10 | -$7.29 |
|
|
260
|
+
| Metrics, numbers and booleans | $5.88 | $9.60 | -$3.72 |
|
|
261
|
+
| Deep nested config | $0.50 | $0.50 | $0.00 |
|
|
262
|
+
| Mixed-type array | $0.13 | $0.13 | $0.00 |
|
|
263
|
+
| Reviews, long text | $7.40 | $8.62 | -$1.22 |
|
|
264
|
+
| Orders with line items | $7.80 | $9.36 | -$1.56 |
|
|
265
|
+
| Total | $86.09 | $117.62 | -$31.53 |
|
|
266
266
|
|
|
267
267
|
How it was measured:
|
|
268
268
|
|
|
269
269
|
- The `o200k_base` counts use the `o200k_base` tokenizer, run locally.
|
|
270
|
-
- The Claude counts come from Anthropic's token count endpoint for `claude-opus-5-5`, run on 2026-11-
|
|
270
|
+
- The Claude counts come from Anthropic's token count endpoint for `claude-opus-5-5`, run on 2026-11-17 UTC.
|
|
271
271
|
- Each text is sent as one user message. The same run also counts the one-letter text "a", which comes back as 9 tokens. Its 8 tokens of fixed message framing are taken off every count, assuming "a" itself is 1 token.
|
|
272
272
|
- Every Claude case was made from the same input as the o200k_base case of the same name. The build checks this by comparing the compact JSON counted locally with o200k_base.
|
|
273
|
-
-
|
|
273
|
+
- All 11 case inputs are made-up data in this repository: `apps/site/src/data/count-cases.json`, built by `pnpm cases` from seed 20261117. `pnpm numbers` recounts them with o200k_base.
|
|
274
274
|
- Every format gets the same input.
|
|
275
275
|
- JSON compact is `JSON.stringify(value)`, with no spaces. It is the main baseline.
|
|
276
276
|
- JSON pretty is `JSON.stringify(value, null, 2)`, with a 2-space indent.
|
|
277
277
|
- YAML is the `yaml` package, version 2.9.1, `stringify` with default options.
|
|
278
|
-
- TOON is the smaller of its comma and tab forms for each case.
|
|
278
|
+
- TOON is the `@toon-format/toon` package, version 4.4.0, the smaller of its comma and tab forms for each case.
|
|
279
279
|
- Terset o200k_base counts include `rulesFor(text)`, fitted to each encoded text, once per prompt. The full `RULES` reference is 73 tokens. The Claude counts include `rulesFor(text)`, a newline and the data in one message.
|
|
280
280
|
- Each Terset output is decoded and compared with its input before it is counted.
|
|
281
281
|
|
|
@@ -288,10 +288,14 @@ How the cost was worked out:
|
|
|
288
288
|
|
|
289
289
|
Where Terset does not win:
|
|
290
290
|
|
|
291
|
-
- o200k_base: Compact JSON
|
|
292
|
-
- Claude: Compact JSON
|
|
293
|
-
-
|
|
294
|
-
-
|
|
291
|
+
- o200k_base: Compact JSON is smaller than or equal to Terset on mixed-type array (63 vs 63).
|
|
292
|
+
- Claude: Compact JSON is smaller than or equal to Terset on mixed-type array (89 vs 89).
|
|
293
|
+
- o200k_base: TOON's best is smaller than or equal to Terset in 2 of 11 cases: Translation batch (1,110 vs 1,139); Reviews, long text (3,662 vs 3,700).
|
|
294
|
+
- Claude: TOON's best is smaller than or equal to Terset in 1 of 11 cases: Deep nested config (394 vs 402).
|
|
295
|
+
- o200k_base: Plain CSV is 9 to 88 tokens smaller on 2 flat, uniform lists, because it needs no rules line. It has no types, it merges null, absent and empty text, and it cannot hold nesting. On orders with line items it is larger: 4,769 vs 3,899.
|
|
296
|
+
- Claude: Plain CSV is 23 to 38 tokens smaller on 4 flat, uniform lists, because it needs no rules line. It has no types, it merges null, absent and empty text, and it cannot hold nesting. On orders with line items it is larger: 6,490 vs 5,662.
|
|
297
|
+
|
|
298
|
+
Across 1,000 seeds of the same 11 case shapes, counted with o200k_base, Terset wins or loses against JSON compact and TOON the same way on every seed.
|
|
295
299
|
|
|
296
300
|
Token counts say nothing about answer quality. Terset is about fewer tokens.
|
|
297
301
|
Other tokenizers and models will give other counts. Full benchmark with pretty JSON and CSV counts:
|
|
@@ -305,33 +309,33 @@ On 244 questions and two models, accuracy is a tie. Terset used fewer input toke
|
|
|
305
309
|
|
|
306
310
|
| Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
|
|
307
311
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
308
|
-
| Terset |
|
|
309
|
-
| Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +
|
|
310
|
-
| JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +
|
|
311
|
-
| TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +
|
|
312
|
+
| Terset | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,188,659 | $5.65 | Reference | 2026-11-17 |
|
|
313
|
+
| Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +42.4% | 2026-11-12 |
|
|
314
|
+
| JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +80.6% | 2026-11-12 |
|
|
315
|
+
| TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +12.5% | 2026-11-12 |
|
|
312
316
|
|
|
313
317
|
#### GPT 6.1 Sol
|
|
314
318
|
|
|
315
319
|
| Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
|
|
316
320
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
317
|
-
| Terset |
|
|
318
|
-
| Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +
|
|
319
|
-
| JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +
|
|
320
|
-
| TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +
|
|
321
|
+
| Terset | 242 of 244 | 99.2% | 97.1% to 99.8% | 842,260 | $1.84 | Reference | 2026-11-17 |
|
|
322
|
+
| Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +35.6% | 2026-11-12 |
|
|
323
|
+
| JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +93.3% | 2026-11-12 |
|
|
324
|
+
| TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +19.1% | 2026-11-12 |
|
|
321
325
|
|
|
322
326
|
JSON is indented with 2 spaces. Compact JSON has no spaces or line breaks.
|
|
323
327
|
|
|
324
328
|
Accuracy is a tie. On 244 questions, every format's 95% interval overlaps every other format's, on both models.
|
|
325
|
-
Against compact JSON, Terset cost 29.
|
|
329
|
+
Against compact JSON, Terset cost 29.8% less on Claude Opus 5.5 and 26.2% less on GPT 6.1 Sol.
|
|
326
330
|
|
|
327
|
-
Cost is at API list price: input and output tokens times list prices read on 2026-11-12. Claude Opus 5.5 input tokens were counted with Anthropic's token counting endpoint over the exact request; GPT 6.1 Sol input tokens are as the model reported them. Temperature 0 where the model accepts it. Terset was rerun on 2026-11-
|
|
331
|
+
Cost is at API list price: input and output tokens times list prices read on 2026-11-12. Claude Opus 5.5 input tokens were counted with Anthropic's token counting endpoint over the exact request; GPT 6.1 Sol input tokens are as the model reported them. Temperature 0 where the model accepts it. Terset was rerun on 2026-11-17 with fitted rules, which send only the rules each text needs. The other formats did not change. Each row names its run date.
|
|
328
332
|
|
|
329
|
-
Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy). Every answer and its score: [2026-11-12](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-12) and [2026-11-
|
|
333
|
+
Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy). Every answer and its score: [2026-11-12](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-12) and [2026-11-17](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-17).
|
|
330
334
|
|
|
331
335
|
## When not to use it
|
|
332
336
|
|
|
333
337
|
<!-- docs:when-not -->
|
|
334
|
-
- **No lists of records.** Config files and deeply nested data stay JSON
|
|
338
|
+
- **No lists of records.** Config files and deeply nested data stay JSON. Terset sends no rules for them and saves a few tokens at most.
|
|
335
339
|
- **One-row lists.** A list needs two or more objects to become a table.
|
|
336
340
|
- **Mixed arrays.** A list with any item that is not an object stays JSON.
|
|
337
341
|
- **Nested lists.** Lists of objects inside a record stay JSON inside their cell. Only the top level becomes tables.
|
|
@@ -79,7 +79,7 @@ Zero dependencies. Python 3.10 and up. Type hints are included.
|
|
|
79
79
|
|
|
80
80
|
<!-- docs:use -->
|
|
81
81
|
The API exports `encode`, `decode`, `RULES` and `rules_for`.
|
|
82
|
-
`rules_for(text)` gives the
|
|
82
|
+
`rules_for(text)` gives only the rules sentences that text needs. `RULES` keeps the full rules.
|
|
83
83
|
|
|
84
84
|
```python
|
|
85
85
|
from terset import decode, encode, rules_for
|
|
@@ -95,10 +95,10 @@ data = {
|
|
|
95
95
|
|
|
96
96
|
text = encode(data)
|
|
97
97
|
|
|
98
|
-
#
|
|
98
|
+
# One text: send its fitted rules right before the data.
|
|
99
99
|
messages = [
|
|
100
|
-
{"role": "system", "content":
|
|
101
|
-
{"role": "user", "content": f"Which users have no role?\n\n{text}"},
|
|
100
|
+
{"role": "system", "content": "You read Terset data."},
|
|
101
|
+
{"role": "user", "content": f"Which users have no role?\n\n{rules_for(text)}\n{text}"},
|
|
102
102
|
]
|
|
103
103
|
|
|
104
104
|
print(text)
|
|
@@ -118,7 +118,7 @@ True
|
|
|
118
118
|
```
|
|
119
119
|
|
|
120
120
|
1. `encode(value)` turns a JSON object or array into Terset text.
|
|
121
|
-
2.
|
|
121
|
+
2. Send `rules_for(text)` right before the data. A shared or cached system prompt for many texts takes `RULES` instead.
|
|
122
122
|
3. `decode(text)` gives back the same value. Ask the model to answer in JSON, since Terset is for reading.
|
|
123
123
|
|
|
124
124
|
Never write Terset by hand. Always use `encode`.
|
|
@@ -157,49 +157,49 @@ for Claude Opus 5.5. They give different numbers for the same text.
|
|
|
157
157
|
|
|
158
158
|
| Case | Terset | JSON compact | YAML | TOON best | Terset vs JSON compact |
|
|
159
159
|
|---|---|---|---|---|---|
|
|
160
|
-
| UI text, uneven rows |
|
|
161
|
-
| UI text, uniform rows |
|
|
162
|
-
| Translation batch | 1,
|
|
163
|
-
| File list, short | 9,
|
|
164
|
-
| File list, long |
|
|
165
|
-
| 200 users |
|
|
166
|
-
| Metrics, numbers and booleans | 2,
|
|
167
|
-
| Deep nested config |
|
|
168
|
-
| Mixed-type array |
|
|
169
|
-
| Reviews, long text | 3,
|
|
170
|
-
| Orders with line items | 3,
|
|
171
|
-
| Total |
|
|
172
|
-
|
|
173
|
-
Terset, rules line included, is under TOON's best in
|
|
174
|
-
under compact JSON in
|
|
175
|
-
|
|
176
|
-
Over all 11 cases, Terset totals
|
|
177
|
-
compact (
|
|
178
|
-
YAML (
|
|
160
|
+
| UI text, uneven rows | 519 | 697 | 812 | 814 | -25.5% |
|
|
161
|
+
| UI text, uniform rows | 480 | 737 | 910 | 518 | -34.9% |
|
|
162
|
+
| Translation batch | 1,139 | 1,448 | 1,674 | 1,110 | -21.3% |
|
|
163
|
+
| File list, short | 9,608 | 12,808 | 15,304 | 10,315 | -25.0% |
|
|
164
|
+
| File list, long | 15,042 | 19,969 | 23,966 | 15,502 | -24.7% |
|
|
165
|
+
| 200 users | 5,404 | 9,051 | 10,924 | 5,589 | -40.3% |
|
|
166
|
+
| Metrics, numbers and booleans | 2,940 | 4,798 | 5,999 | 3,181 | -38.7% |
|
|
167
|
+
| Deep nested config | 250 | 251 | 316 | 296 | -0.4% |
|
|
168
|
+
| Mixed-type array | 63 | 63 | 89 | 99 | 0.0% |
|
|
169
|
+
| Reviews, long text | 3,700 | 4,308 | 5,006 | 3,662 | -14.1% |
|
|
170
|
+
| Orders with line items | 3,899 | 4,678 | 5,846 | 4,982 | -16.7% |
|
|
171
|
+
| Total | 43,044 | 58,808 | 70,846 | 46,068 | -26.8% |
|
|
172
|
+
|
|
173
|
+
Terset, fitted rules line included, is under TOON's best in 9 of 11
|
|
174
|
+
cases and under compact JSON in 10 of 11.
|
|
175
|
+
|
|
176
|
+
Over all 11 cases, Terset totals 43,044 tokens: 26.8% fewer than JSON
|
|
177
|
+
compact (58,808), 50.5% fewer than JSON pretty (87,020), 39.2% fewer than
|
|
178
|
+
YAML (70,846), and 6.6% fewer than TOON's best (46,068).
|
|
179
179
|
|
|
180
180
|
### Claude Opus 5.5
|
|
181
181
|
|
|
182
182
|
| Case | Terset | JSON compact | YAML | TOON best | Terset vs JSON compact |
|
|
183
183
|
|---|---|---|---|---|---|
|
|
184
|
-
| UI text, uneven rows |
|
|
185
|
-
| UI text, uniform rows |
|
|
186
|
-
| Translation batch |
|
|
187
|
-
| File list, short |
|
|
188
|
-
| File list, long |
|
|
189
|
-
| 200 users | 7,
|
|
190
|
-
| Metrics, numbers and booleans |
|
|
191
|
-
| Deep nested config |
|
|
192
|
-
| Mixed-type array |
|
|
193
|
-
| Reviews, long text |
|
|
194
|
-
| Orders with line items | 5,
|
|
195
|
-
| Total |
|
|
196
|
-
|
|
197
|
-
Terset, rules line included, is under TOON's best in
|
|
198
|
-
under compact JSON in
|
|
199
|
-
|
|
200
|
-
Over all 11 cases, Terset totals
|
|
201
|
-
compact (
|
|
202
|
-
YAML (
|
|
184
|
+
| UI text, uneven rows | 766 | 1,083 | 1,062 | 1,065 | -29.3% |
|
|
185
|
+
| UI text, uniform rows | 745 | 1,253 | 1,160 | 774 | -40.5% |
|
|
186
|
+
| Translation batch | 1,813 | 2,514 | 2,413 | 1,833 | -27.9% |
|
|
187
|
+
| File list, short | 11,972 | 18,018 | 18,012 | 12,671 | -33.6% |
|
|
188
|
+
| File list, long | 21,259 | 30,405 | 31,199 | 22,019 | -30.1% |
|
|
189
|
+
| 200 users | 7,665 | 12,819 | 13,214 | 7,836 | -40.2% |
|
|
190
|
+
| Metrics, numbers and booleans | 3,310 | 6,535 | 6,960 | 3,467 | -49.3% |
|
|
191
|
+
| Deep nested config | 402 | 405 | 412 | 394 | -0.7% |
|
|
192
|
+
| Mixed-type array | 89 | 89 | 101 | 118 | 0.0% |
|
|
193
|
+
| Reviews, long text | 4,899 | 5,979 | 6,404 | 4,915 | -18.1% |
|
|
194
|
+
| Orders with line items | 5,662 | 7,002 | 7,208 | 6,268 | -19.1% |
|
|
195
|
+
| Total | 58,582 | 86,102 | 88,145 | 61,360 | -32.0% |
|
|
196
|
+
|
|
197
|
+
Terset, fitted rules line included, is under TOON's best in 10 of 11
|
|
198
|
+
cases and under compact JSON in 10 of 11.
|
|
199
|
+
|
|
200
|
+
Over all 11 cases, Terset totals 58,582 tokens: 32.0% fewer than JSON
|
|
201
|
+
compact (86,102), 48.4% fewer than JSON pretty (113,574), 33.5% fewer than
|
|
202
|
+
YAML (88,145), and 4.5% fewer than TOON's best (61,360).
|
|
203
203
|
|
|
204
204
|
### Input cost at list price
|
|
205
205
|
|
|
@@ -210,49 +210,49 @@ Claude Opus 5.5 at $4 per million input tokens, on the Claude counts:
|
|
|
210
210
|
|
|
211
211
|
| Case | Terset | JSON compact | vs JSON compact |
|
|
212
212
|
|---|---|---|---|
|
|
213
|
-
| UI text, uneven rows | $
|
|
214
|
-
| UI text, uniform rows | $
|
|
215
|
-
| Translation batch | $
|
|
216
|
-
| File list, short | $
|
|
217
|
-
| File list, long | $
|
|
218
|
-
| 200 users | $
|
|
219
|
-
| Metrics, numbers and booleans | $
|
|
220
|
-
| Deep nested config | $
|
|
221
|
-
| Mixed-type array | $0.
|
|
222
|
-
| Reviews, long text | $
|
|
223
|
-
| Orders with line items | $22.
|
|
224
|
-
| Total | $
|
|
213
|
+
| UI text, uneven rows | $3.06 | $4.33 | -$1.27 |
|
|
214
|
+
| UI text, uniform rows | $2.98 | $5.01 | -$2.03 |
|
|
215
|
+
| Translation batch | $7.25 | $10.06 | -$2.81 |
|
|
216
|
+
| File list, short | $47.89 | $72.07 | -$24.18 |
|
|
217
|
+
| File list, long | $85.04 | $121.62 | -$36.58 |
|
|
218
|
+
| 200 users | $30.66 | $51.28 | -$20.62 |
|
|
219
|
+
| Metrics, numbers and booleans | $13.24 | $26.14 | -$12.90 |
|
|
220
|
+
| Deep nested config | $1.61 | $1.62 | -$0.01 |
|
|
221
|
+
| Mixed-type array | $0.36 | $0.36 | $0.00 |
|
|
222
|
+
| Reviews, long text | $19.60 | $23.92 | -$4.32 |
|
|
223
|
+
| Orders with line items | $22.65 | $28.01 | -$5.36 |
|
|
224
|
+
| Total | $234.33 | $344.41 | -$110.08 |
|
|
225
225
|
|
|
226
226
|
GPT 6.1 Sol at $2 per million input tokens, context up to 272K, on the
|
|
227
227
|
`o200k_base` counts:
|
|
228
228
|
|
|
229
229
|
| Case | Terset | JSON compact | vs JSON compact |
|
|
230
230
|
|---|---|---|---|
|
|
231
|
-
| UI text, uneven rows | $1.
|
|
232
|
-
| UI text, uniform rows | $
|
|
233
|
-
| Translation batch | $2.
|
|
234
|
-
| File list, short | $19.
|
|
235
|
-
| File list, long | $
|
|
236
|
-
| 200 users | $
|
|
237
|
-
| Metrics, numbers and booleans | $5.
|
|
238
|
-
| Deep nested config | $0.
|
|
239
|
-
| Mixed-type array | $0.
|
|
240
|
-
| Reviews, long text | $7.
|
|
241
|
-
| Orders with line items | $7.
|
|
242
|
-
| Total | $
|
|
231
|
+
| UI text, uneven rows | $1.04 | $1.39 | -$0.35 |
|
|
232
|
+
| UI text, uniform rows | $0.96 | $1.47 | -$0.51 |
|
|
233
|
+
| Translation batch | $2.28 | $2.90 | -$0.62 |
|
|
234
|
+
| File list, short | $19.22 | $25.62 | -$6.40 |
|
|
235
|
+
| File list, long | $30.08 | $39.94 | -$9.86 |
|
|
236
|
+
| 200 users | $10.81 | $18.10 | -$7.29 |
|
|
237
|
+
| Metrics, numbers and booleans | $5.88 | $9.60 | -$3.72 |
|
|
238
|
+
| Deep nested config | $0.50 | $0.50 | $0.00 |
|
|
239
|
+
| Mixed-type array | $0.13 | $0.13 | $0.00 |
|
|
240
|
+
| Reviews, long text | $7.40 | $8.62 | -$1.22 |
|
|
241
|
+
| Orders with line items | $7.80 | $9.36 | -$1.56 |
|
|
242
|
+
| Total | $86.09 | $117.62 | -$31.53 |
|
|
243
243
|
|
|
244
244
|
How it was measured:
|
|
245
245
|
|
|
246
246
|
- The `o200k_base` counts use the `o200k_base` tokenizer, run locally.
|
|
247
|
-
- The Claude counts come from Anthropic's token count endpoint for `claude-opus-5-5`, run on 2026-11-
|
|
247
|
+
- The Claude counts come from Anthropic's token count endpoint for `claude-opus-5-5`, run on 2026-11-17 UTC.
|
|
248
248
|
- Each text is sent as one user message. The same run also counts the one-letter text "a", which comes back as 9 tokens. Its 8 tokens of fixed message framing are taken off every count, assuming "a" itself is 1 token.
|
|
249
249
|
- Every Claude case was made from the same input as the o200k_base case of the same name. The build checks this by comparing the compact JSON counted locally with o200k_base.
|
|
250
|
-
-
|
|
250
|
+
- All 11 case inputs are made-up data in this repository: `apps/site/src/data/count-cases.json`, built by `pnpm cases` from seed 20261117. `pnpm numbers` recounts them with o200k_base.
|
|
251
251
|
- Every format gets the same input.
|
|
252
252
|
- JSON compact is `JSON.stringify(value)`, with no spaces. It is the main baseline.
|
|
253
253
|
- JSON pretty is `JSON.stringify(value, null, 2)`, with a 2-space indent.
|
|
254
254
|
- YAML is the `yaml` package, version 2.9.1, `stringify` with default options.
|
|
255
|
-
- TOON is the smaller of its comma and tab forms for each case.
|
|
255
|
+
- TOON is the `@toon-format/toon` package, version 4.4.0, the smaller of its comma and tab forms for each case.
|
|
256
256
|
- Terset o200k_base counts include `rulesFor(text)`, fitted to each encoded text, once per prompt. The full `RULES` reference is 73 tokens. The Claude counts include `rulesFor(text)`, a newline and the data in one message.
|
|
257
257
|
- Each Terset output is decoded and compared with its input before it is counted.
|
|
258
258
|
|
|
@@ -265,10 +265,14 @@ How the cost was worked out:
|
|
|
265
265
|
|
|
266
266
|
Where Terset does not win:
|
|
267
267
|
|
|
268
|
-
- o200k_base: Compact JSON
|
|
269
|
-
- Claude: Compact JSON
|
|
270
|
-
-
|
|
271
|
-
-
|
|
268
|
+
- o200k_base: Compact JSON is smaller than or equal to Terset on mixed-type array (63 vs 63).
|
|
269
|
+
- Claude: Compact JSON is smaller than or equal to Terset on mixed-type array (89 vs 89).
|
|
270
|
+
- o200k_base: TOON's best is smaller than or equal to Terset in 2 of 11 cases: Translation batch (1,110 vs 1,139); Reviews, long text (3,662 vs 3,700).
|
|
271
|
+
- Claude: TOON's best is smaller than or equal to Terset in 1 of 11 cases: Deep nested config (394 vs 402).
|
|
272
|
+
- o200k_base: Plain CSV is 9 to 88 tokens smaller on 2 flat, uniform lists, because it needs no rules line. It has no types, it merges null, absent and empty text, and it cannot hold nesting. On orders with line items it is larger: 4,769 vs 3,899.
|
|
273
|
+
- Claude: Plain CSV is 23 to 38 tokens smaller on 4 flat, uniform lists, because it needs no rules line. It has no types, it merges null, absent and empty text, and it cannot hold nesting. On orders with line items it is larger: 6,490 vs 5,662.
|
|
274
|
+
|
|
275
|
+
Across 1,000 seeds of the same 11 case shapes, counted with o200k_base, Terset wins or loses against JSON compact and TOON the same way on every seed.
|
|
272
276
|
|
|
273
277
|
Token counts say nothing about answer quality. Terset is about fewer tokens.
|
|
274
278
|
Other tokenizers and models will give other counts. Full benchmark with pretty JSON and CSV counts:
|
|
@@ -282,33 +286,33 @@ On 244 questions and two models, accuracy is a tie. Terset used fewer input toke
|
|
|
282
286
|
|
|
283
287
|
| Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
|
|
284
288
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
285
|
-
| Terset |
|
|
286
|
-
| Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +
|
|
287
|
-
| JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +
|
|
288
|
-
| TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +
|
|
289
|
+
| Terset | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,188,659 | $5.65 | Reference | 2026-11-17 |
|
|
290
|
+
| Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +42.4% | 2026-11-12 |
|
|
291
|
+
| JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +80.6% | 2026-11-12 |
|
|
292
|
+
| TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +12.5% | 2026-11-12 |
|
|
289
293
|
|
|
290
294
|
#### GPT 6.1 Sol
|
|
291
295
|
|
|
292
296
|
| Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
|
|
293
297
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
294
|
-
| Terset |
|
|
295
|
-
| Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +
|
|
296
|
-
| JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +
|
|
297
|
-
| TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +
|
|
298
|
+
| Terset | 242 of 244 | 99.2% | 97.1% to 99.8% | 842,260 | $1.84 | Reference | 2026-11-17 |
|
|
299
|
+
| Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +35.6% | 2026-11-12 |
|
|
300
|
+
| JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +93.3% | 2026-11-12 |
|
|
301
|
+
| TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +19.1% | 2026-11-12 |
|
|
298
302
|
|
|
299
303
|
JSON is indented with 2 spaces. Compact JSON has no spaces or line breaks.
|
|
300
304
|
|
|
301
305
|
Accuracy is a tie. On 244 questions, every format's 95% interval overlaps every other format's, on both models.
|
|
302
|
-
Against compact JSON, Terset cost 29.
|
|
306
|
+
Against compact JSON, Terset cost 29.8% less on Claude Opus 5.5 and 26.2% less on GPT 6.1 Sol.
|
|
303
307
|
|
|
304
|
-
Cost is at API list price: input and output tokens times list prices read on 2026-11-12. Claude Opus 5.5 input tokens were counted with Anthropic's token counting endpoint over the exact request; GPT 6.1 Sol input tokens are as the model reported them. Temperature 0 where the model accepts it. Terset was rerun on 2026-11-
|
|
308
|
+
Cost is at API list price: input and output tokens times list prices read on 2026-11-12. Claude Opus 5.5 input tokens were counted with Anthropic's token counting endpoint over the exact request; GPT 6.1 Sol input tokens are as the model reported them. Temperature 0 where the model accepts it. Terset was rerun on 2026-11-17 with fitted rules, which send only the rules each text needs. The other formats did not change. Each row names its run date.
|
|
305
309
|
|
|
306
|
-
Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy). Every answer and its score: [2026-11-12](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-12) and [2026-11-
|
|
310
|
+
Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy). Every answer and its score: [2026-11-12](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-12) and [2026-11-17](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-17).
|
|
307
311
|
|
|
308
312
|
## When not to use it
|
|
309
313
|
|
|
310
314
|
<!-- docs:when-not -->
|
|
311
|
-
- **No lists of records.** Config files and deeply nested data stay JSON
|
|
315
|
+
- **No lists of records.** Config files and deeply nested data stay JSON. Terset sends no rules for them and saves a few tokens at most.
|
|
312
316
|
- **One-row lists.** A list needs two or more objects to become a table.
|
|
313
317
|
- **Mixed arrays.** A list with any item that is not an object stays JSON.
|
|
314
318
|
- **Nested lists.** Lists of objects inside a record stay JSON inside their cell. Only the top level becomes tables.
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import heapq
|
|
5
6
|
import json
|
|
6
7
|
import math
|
|
7
8
|
import re
|
|
@@ -22,19 +23,22 @@ _HeaderTrie: TypeAlias = "dict[str, _HeaderTrie | None]"
|
|
|
22
23
|
RULES = "Tables use tab-separated headers and rows. Empty cells mean absent. JSON quotes distinguish numeric, boolean or null text from scalars. Nested cells use JSON. A leading bare id header marks string IDs. name{N} has N entries; the @key column holds each entry's key and is not a field. Header a.b is field b in object a."
|
|
23
24
|
|
|
24
25
|
_SENTENCES = RULES.split(". ")
|
|
25
|
-
|
|
26
|
+
_FITTED_RULES = [
|
|
27
|
+
("table", _SENTENCES[0] + "."),
|
|
28
|
+
("empty", _SENTENCES[1] + "."),
|
|
29
|
+
("quoted", _SENTENCES[2] + "."),
|
|
30
|
+
("json", _SENTENCES[3] + "."),
|
|
31
|
+
("id", _SENTENCES[4] + "."),
|
|
32
|
+
("keyed", _SENTENCES[5] + "."),
|
|
33
|
+
("nested", _SENTENCES[6]),
|
|
34
|
+
]
|
|
26
35
|
|
|
27
36
|
|
|
28
37
|
def rules_for(text: str) -> str:
|
|
29
38
|
"""Return the rules needed by canonical text, with decode's validation."""
|
|
30
39
|
features: set[str] = set()
|
|
31
40
|
_read_document(text, features)
|
|
32
|
-
|
|
33
|
-
if "keyed" in features:
|
|
34
|
-
sentences.append(_SENTENCES[5] + ".")
|
|
35
|
-
if "nested" in features:
|
|
36
|
-
sentences.append(_SENTENCES[6])
|
|
37
|
-
return " ".join(sentences)
|
|
41
|
+
return " ".join(sentence for feature, sentence in _FITTED_RULES if feature in features)
|
|
38
42
|
|
|
39
43
|
_KEY = re.compile(r"[A-Za-z_$][A-Za-z0-9_$.-]*\Z")
|
|
40
44
|
_NUMBER = re.compile(r"[-+]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][-+]?[0-9]+)?\Z")
|
|
@@ -205,24 +209,54 @@ def _flatten(path: list[str], values: list[JsonValue | object]) -> list[_Column]
|
|
|
205
209
|
return columns
|
|
206
210
|
|
|
207
211
|
|
|
208
|
-
def
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
+
def _header(rows: list[JsonObject]) -> list[str] | None:
|
|
213
|
+
"""One header that every row's own key order fits, or None when none does.
|
|
214
|
+
|
|
215
|
+
When several fields may come next, the one seen first wins, so a list whose
|
|
216
|
+
first-seen field order already fits every row keeps exactly that order.
|
|
217
|
+
"""
|
|
212
218
|
fields: list[str] = []
|
|
213
219
|
positions: dict[str, int] = {}
|
|
220
|
+
# A field's followers in some row, and how many fields must come before it.
|
|
221
|
+
followers: list[list[int]] = []
|
|
222
|
+
waiting: list[int] = []
|
|
214
223
|
for row in rows:
|
|
215
224
|
previous = -1
|
|
216
225
|
for key in _keys(row):
|
|
217
|
-
|
|
218
|
-
|
|
226
|
+
index = positions.get(key)
|
|
227
|
+
if index is None:
|
|
228
|
+
index = len(fields)
|
|
229
|
+
positions[key] = index
|
|
219
230
|
fields.append(key)
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
231
|
+
followers.append([])
|
|
232
|
+
waiting.append(0)
|
|
233
|
+
if previous >= 0:
|
|
234
|
+
followers[previous].append(index)
|
|
235
|
+
waiting[index] += 1
|
|
223
236
|
previous = index
|
|
224
237
|
if not fields:
|
|
225
238
|
return None
|
|
239
|
+
ready = [index for index, count in enumerate(waiting) if count == 0]
|
|
240
|
+
heapq.heapify(ready)
|
|
241
|
+
order: list[str] = []
|
|
242
|
+
while ready:
|
|
243
|
+
index = heapq.heappop(ready)
|
|
244
|
+
order.append(fields[index])
|
|
245
|
+
for following in followers[index]:
|
|
246
|
+
waiting[following] -= 1
|
|
247
|
+
if waiting[following] == 0:
|
|
248
|
+
heapq.heappush(ready, following)
|
|
249
|
+
# Fields left over sit on a cycle: two rows order the same pair both ways.
|
|
250
|
+
return order if len(order) == len(fields) else None
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _table(value: list[JsonObject], keyed: bool, entry_keys: list[str] | None = None, max_bytes: int | float = math.inf) -> str | None:
|
|
254
|
+
if len(value) < 2:
|
|
255
|
+
return None
|
|
256
|
+
rows = [{_unicode(key): child for key, child in row.items()} for row in value]
|
|
257
|
+
fields = _header(rows)
|
|
258
|
+
if fields is None:
|
|
259
|
+
return None
|
|
226
260
|
if len(rows) * (len(fields) - 1) >= max_bytes:
|
|
227
261
|
return None
|
|
228
262
|
columns: list[_Column] = []
|
|
@@ -400,6 +434,7 @@ def _read_table(lines: list[str], start: int, count: int, keyed: bool, row_depth
|
|
|
400
434
|
raise ValueError(_DEPTH_ERROR)
|
|
401
435
|
paths.append(path)
|
|
402
436
|
if features is not None:
|
|
437
|
+
features.add("table")
|
|
403
438
|
if keyed:
|
|
404
439
|
features.add("keyed")
|
|
405
440
|
if any(len(path) > 1 for path in paths):
|
|
@@ -424,6 +459,8 @@ def _read_table(lines: list[str], start: int, count: int, keyed: bool, row_depth
|
|
|
424
459
|
node[segment] = child
|
|
425
460
|
node = child
|
|
426
461
|
ids = not keyed and fields[0] == "id"
|
|
462
|
+
if ids and features is not None:
|
|
463
|
+
features.add("id")
|
|
427
464
|
result: JsonRoot = {} if keyed else []
|
|
428
465
|
for index in range(count):
|
|
429
466
|
if start + 1 + index >= len(lines):
|
|
@@ -437,6 +474,13 @@ def _read_table(lines: list[str], start: int, count: int, keyed: bool, row_depth
|
|
|
437
474
|
for column, (path, raw) in enumerate(zip(paths, cells)):
|
|
438
475
|
if column == 0 and ids and not raw:
|
|
439
476
|
raise ValueError("Missing string id.")
|
|
477
|
+
if features is not None and not (column == 0 and ids):
|
|
478
|
+
if not raw:
|
|
479
|
+
features.add("empty")
|
|
480
|
+
elif raw.startswith('"'):
|
|
481
|
+
features.add("quoted")
|
|
482
|
+
elif raw.startswith(("{", "[")):
|
|
483
|
+
features.add("json")
|
|
440
484
|
if raw:
|
|
441
485
|
_set_path(row, path, raw if column == 0 and ids else _read_cell(raw))
|
|
442
486
|
if isinstance(result, list):
|
|
@@ -504,6 +548,8 @@ def _read_document(text: str, features: set[str] | None = None) -> JsonRoot:
|
|
|
504
548
|
else:
|
|
505
549
|
if not rest.startswith(" "):
|
|
506
550
|
raise ValueError("Expected scalar field.")
|
|
551
|
+
if features is not None and rest[1:].startswith('"'):
|
|
552
|
+
features.add("quoted")
|
|
507
553
|
_set(result, key, _read_cell(rest[1:]))
|
|
508
554
|
try:
|
|
509
555
|
canonical = encode(result)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: terset
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.3
|
|
4
4
|
Summary: Encode JSON records as compact, typed tables.
|
|
5
5
|
Author: StackDeck
|
|
6
6
|
License-Expression: MIT
|
|
@@ -102,7 +102,7 @@ Zero dependencies. Python 3.10 and up. Type hints are included.
|
|
|
102
102
|
|
|
103
103
|
<!-- docs:use -->
|
|
104
104
|
The API exports `encode`, `decode`, `RULES` and `rules_for`.
|
|
105
|
-
`rules_for(text)` gives the
|
|
105
|
+
`rules_for(text)` gives only the rules sentences that text needs. `RULES` keeps the full rules.
|
|
106
106
|
|
|
107
107
|
```python
|
|
108
108
|
from terset import decode, encode, rules_for
|
|
@@ -118,10 +118,10 @@ data = {
|
|
|
118
118
|
|
|
119
119
|
text = encode(data)
|
|
120
120
|
|
|
121
|
-
#
|
|
121
|
+
# One text: send its fitted rules right before the data.
|
|
122
122
|
messages = [
|
|
123
|
-
{"role": "system", "content":
|
|
124
|
-
{"role": "user", "content": f"Which users have no role?\n\n{text}"},
|
|
123
|
+
{"role": "system", "content": "You read Terset data."},
|
|
124
|
+
{"role": "user", "content": f"Which users have no role?\n\n{rules_for(text)}\n{text}"},
|
|
125
125
|
]
|
|
126
126
|
|
|
127
127
|
print(text)
|
|
@@ -141,7 +141,7 @@ True
|
|
|
141
141
|
```
|
|
142
142
|
|
|
143
143
|
1. `encode(value)` turns a JSON object or array into Terset text.
|
|
144
|
-
2.
|
|
144
|
+
2. Send `rules_for(text)` right before the data. A shared or cached system prompt for many texts takes `RULES` instead.
|
|
145
145
|
3. `decode(text)` gives back the same value. Ask the model to answer in JSON, since Terset is for reading.
|
|
146
146
|
|
|
147
147
|
Never write Terset by hand. Always use `encode`.
|
|
@@ -180,49 +180,49 @@ for Claude Opus 5.5. They give different numbers for the same text.
|
|
|
180
180
|
|
|
181
181
|
| Case | Terset | JSON compact | YAML | TOON best | Terset vs JSON compact |
|
|
182
182
|
|---|---|---|---|---|---|
|
|
183
|
-
| UI text, uneven rows |
|
|
184
|
-
| UI text, uniform rows |
|
|
185
|
-
| Translation batch | 1,
|
|
186
|
-
| File list, short | 9,
|
|
187
|
-
| File list, long |
|
|
188
|
-
| 200 users |
|
|
189
|
-
| Metrics, numbers and booleans | 2,
|
|
190
|
-
| Deep nested config |
|
|
191
|
-
| Mixed-type array |
|
|
192
|
-
| Reviews, long text | 3,
|
|
193
|
-
| Orders with line items | 3,
|
|
194
|
-
| Total |
|
|
195
|
-
|
|
196
|
-
Terset, rules line included, is under TOON's best in
|
|
197
|
-
under compact JSON in
|
|
198
|
-
|
|
199
|
-
Over all 11 cases, Terset totals
|
|
200
|
-
compact (
|
|
201
|
-
YAML (
|
|
183
|
+
| UI text, uneven rows | 519 | 697 | 812 | 814 | -25.5% |
|
|
184
|
+
| UI text, uniform rows | 480 | 737 | 910 | 518 | -34.9% |
|
|
185
|
+
| Translation batch | 1,139 | 1,448 | 1,674 | 1,110 | -21.3% |
|
|
186
|
+
| File list, short | 9,608 | 12,808 | 15,304 | 10,315 | -25.0% |
|
|
187
|
+
| File list, long | 15,042 | 19,969 | 23,966 | 15,502 | -24.7% |
|
|
188
|
+
| 200 users | 5,404 | 9,051 | 10,924 | 5,589 | -40.3% |
|
|
189
|
+
| Metrics, numbers and booleans | 2,940 | 4,798 | 5,999 | 3,181 | -38.7% |
|
|
190
|
+
| Deep nested config | 250 | 251 | 316 | 296 | -0.4% |
|
|
191
|
+
| Mixed-type array | 63 | 63 | 89 | 99 | 0.0% |
|
|
192
|
+
| Reviews, long text | 3,700 | 4,308 | 5,006 | 3,662 | -14.1% |
|
|
193
|
+
| Orders with line items | 3,899 | 4,678 | 5,846 | 4,982 | -16.7% |
|
|
194
|
+
| Total | 43,044 | 58,808 | 70,846 | 46,068 | -26.8% |
|
|
195
|
+
|
|
196
|
+
Terset, fitted rules line included, is under TOON's best in 9 of 11
|
|
197
|
+
cases and under compact JSON in 10 of 11.
|
|
198
|
+
|
|
199
|
+
Over all 11 cases, Terset totals 43,044 tokens: 26.8% fewer than JSON
|
|
200
|
+
compact (58,808), 50.5% fewer than JSON pretty (87,020), 39.2% fewer than
|
|
201
|
+
YAML (70,846), and 6.6% fewer than TOON's best (46,068).
|
|
202
202
|
|
|
203
203
|
### Claude Opus 5.5
|
|
204
204
|
|
|
205
205
|
| Case | Terset | JSON compact | YAML | TOON best | Terset vs JSON compact |
|
|
206
206
|
|---|---|---|---|---|---|
|
|
207
|
-
| UI text, uneven rows |
|
|
208
|
-
| UI text, uniform rows |
|
|
209
|
-
| Translation batch |
|
|
210
|
-
| File list, short |
|
|
211
|
-
| File list, long |
|
|
212
|
-
| 200 users | 7,
|
|
213
|
-
| Metrics, numbers and booleans |
|
|
214
|
-
| Deep nested config |
|
|
215
|
-
| Mixed-type array |
|
|
216
|
-
| Reviews, long text |
|
|
217
|
-
| Orders with line items | 5,
|
|
218
|
-
| Total |
|
|
219
|
-
|
|
220
|
-
Terset, rules line included, is under TOON's best in
|
|
221
|
-
under compact JSON in
|
|
222
|
-
|
|
223
|
-
Over all 11 cases, Terset totals
|
|
224
|
-
compact (
|
|
225
|
-
YAML (
|
|
207
|
+
| UI text, uneven rows | 766 | 1,083 | 1,062 | 1,065 | -29.3% |
|
|
208
|
+
| UI text, uniform rows | 745 | 1,253 | 1,160 | 774 | -40.5% |
|
|
209
|
+
| Translation batch | 1,813 | 2,514 | 2,413 | 1,833 | -27.9% |
|
|
210
|
+
| File list, short | 11,972 | 18,018 | 18,012 | 12,671 | -33.6% |
|
|
211
|
+
| File list, long | 21,259 | 30,405 | 31,199 | 22,019 | -30.1% |
|
|
212
|
+
| 200 users | 7,665 | 12,819 | 13,214 | 7,836 | -40.2% |
|
|
213
|
+
| Metrics, numbers and booleans | 3,310 | 6,535 | 6,960 | 3,467 | -49.3% |
|
|
214
|
+
| Deep nested config | 402 | 405 | 412 | 394 | -0.7% |
|
|
215
|
+
| Mixed-type array | 89 | 89 | 101 | 118 | 0.0% |
|
|
216
|
+
| Reviews, long text | 4,899 | 5,979 | 6,404 | 4,915 | -18.1% |
|
|
217
|
+
| Orders with line items | 5,662 | 7,002 | 7,208 | 6,268 | -19.1% |
|
|
218
|
+
| Total | 58,582 | 86,102 | 88,145 | 61,360 | -32.0% |
|
|
219
|
+
|
|
220
|
+
Terset, fitted rules line included, is under TOON's best in 10 of 11
|
|
221
|
+
cases and under compact JSON in 10 of 11.
|
|
222
|
+
|
|
223
|
+
Over all 11 cases, Terset totals 58,582 tokens: 32.0% fewer than JSON
|
|
224
|
+
compact (86,102), 48.4% fewer than JSON pretty (113,574), 33.5% fewer than
|
|
225
|
+
YAML (88,145), and 4.5% fewer than TOON's best (61,360).
|
|
226
226
|
|
|
227
227
|
### Input cost at list price
|
|
228
228
|
|
|
@@ -233,49 +233,49 @@ Claude Opus 5.5 at $4 per million input tokens, on the Claude counts:
|
|
|
233
233
|
|
|
234
234
|
| Case | Terset | JSON compact | vs JSON compact |
|
|
235
235
|
|---|---|---|---|
|
|
236
|
-
| UI text, uneven rows | $
|
|
237
|
-
| UI text, uniform rows | $
|
|
238
|
-
| Translation batch | $
|
|
239
|
-
| File list, short | $
|
|
240
|
-
| File list, long | $
|
|
241
|
-
| 200 users | $
|
|
242
|
-
| Metrics, numbers and booleans | $
|
|
243
|
-
| Deep nested config | $
|
|
244
|
-
| Mixed-type array | $0.
|
|
245
|
-
| Reviews, long text | $
|
|
246
|
-
| Orders with line items | $22.
|
|
247
|
-
| Total | $
|
|
236
|
+
| UI text, uneven rows | $3.06 | $4.33 | -$1.27 |
|
|
237
|
+
| UI text, uniform rows | $2.98 | $5.01 | -$2.03 |
|
|
238
|
+
| Translation batch | $7.25 | $10.06 | -$2.81 |
|
|
239
|
+
| File list, short | $47.89 | $72.07 | -$24.18 |
|
|
240
|
+
| File list, long | $85.04 | $121.62 | -$36.58 |
|
|
241
|
+
| 200 users | $30.66 | $51.28 | -$20.62 |
|
|
242
|
+
| Metrics, numbers and booleans | $13.24 | $26.14 | -$12.90 |
|
|
243
|
+
| Deep nested config | $1.61 | $1.62 | -$0.01 |
|
|
244
|
+
| Mixed-type array | $0.36 | $0.36 | $0.00 |
|
|
245
|
+
| Reviews, long text | $19.60 | $23.92 | -$4.32 |
|
|
246
|
+
| Orders with line items | $22.65 | $28.01 | -$5.36 |
|
|
247
|
+
| Total | $234.33 | $344.41 | -$110.08 |
|
|
248
248
|
|
|
249
249
|
GPT 6.1 Sol at $2 per million input tokens, context up to 272K, on the
|
|
250
250
|
`o200k_base` counts:
|
|
251
251
|
|
|
252
252
|
| Case | Terset | JSON compact | vs JSON compact |
|
|
253
253
|
|---|---|---|---|
|
|
254
|
-
| UI text, uneven rows | $1.
|
|
255
|
-
| UI text, uniform rows | $
|
|
256
|
-
| Translation batch | $2.
|
|
257
|
-
| File list, short | $19.
|
|
258
|
-
| File list, long | $
|
|
259
|
-
| 200 users | $
|
|
260
|
-
| Metrics, numbers and booleans | $5.
|
|
261
|
-
| Deep nested config | $0.
|
|
262
|
-
| Mixed-type array | $0.
|
|
263
|
-
| Reviews, long text | $7.
|
|
264
|
-
| Orders with line items | $7.
|
|
265
|
-
| Total | $
|
|
254
|
+
| UI text, uneven rows | $1.04 | $1.39 | -$0.35 |
|
|
255
|
+
| UI text, uniform rows | $0.96 | $1.47 | -$0.51 |
|
|
256
|
+
| Translation batch | $2.28 | $2.90 | -$0.62 |
|
|
257
|
+
| File list, short | $19.22 | $25.62 | -$6.40 |
|
|
258
|
+
| File list, long | $30.08 | $39.94 | -$9.86 |
|
|
259
|
+
| 200 users | $10.81 | $18.10 | -$7.29 |
|
|
260
|
+
| Metrics, numbers and booleans | $5.88 | $9.60 | -$3.72 |
|
|
261
|
+
| Deep nested config | $0.50 | $0.50 | $0.00 |
|
|
262
|
+
| Mixed-type array | $0.13 | $0.13 | $0.00 |
|
|
263
|
+
| Reviews, long text | $7.40 | $8.62 | -$1.22 |
|
|
264
|
+
| Orders with line items | $7.80 | $9.36 | -$1.56 |
|
|
265
|
+
| Total | $86.09 | $117.62 | -$31.53 |
|
|
266
266
|
|
|
267
267
|
How it was measured:
|
|
268
268
|
|
|
269
269
|
- The `o200k_base` counts use the `o200k_base` tokenizer, run locally.
|
|
270
|
-
- The Claude counts come from Anthropic's token count endpoint for `claude-opus-5-5`, run on 2026-11-
|
|
270
|
+
- The Claude counts come from Anthropic's token count endpoint for `claude-opus-5-5`, run on 2026-11-17 UTC.
|
|
271
271
|
- Each text is sent as one user message. The same run also counts the one-letter text "a", which comes back as 9 tokens. Its 8 tokens of fixed message framing are taken off every count, assuming "a" itself is 1 token.
|
|
272
272
|
- Every Claude case was made from the same input as the o200k_base case of the same name. The build checks this by comparing the compact JSON counted locally with o200k_base.
|
|
273
|
-
-
|
|
273
|
+
- All 11 case inputs are made-up data in this repository: `apps/site/src/data/count-cases.json`, built by `pnpm cases` from seed 20261117. `pnpm numbers` recounts them with o200k_base.
|
|
274
274
|
- Every format gets the same input.
|
|
275
275
|
- JSON compact is `JSON.stringify(value)`, with no spaces. It is the main baseline.
|
|
276
276
|
- JSON pretty is `JSON.stringify(value, null, 2)`, with a 2-space indent.
|
|
277
277
|
- YAML is the `yaml` package, version 2.9.1, `stringify` with default options.
|
|
278
|
-
- TOON is the smaller of its comma and tab forms for each case.
|
|
278
|
+
- TOON is the `@toon-format/toon` package, version 4.4.0, the smaller of its comma and tab forms for each case.
|
|
279
279
|
- Terset o200k_base counts include `rulesFor(text)`, fitted to each encoded text, once per prompt. The full `RULES` reference is 73 tokens. The Claude counts include `rulesFor(text)`, a newline and the data in one message.
|
|
280
280
|
- Each Terset output is decoded and compared with its input before it is counted.
|
|
281
281
|
|
|
@@ -288,10 +288,14 @@ How the cost was worked out:
|
|
|
288
288
|
|
|
289
289
|
Where Terset does not win:
|
|
290
290
|
|
|
291
|
-
- o200k_base: Compact JSON
|
|
292
|
-
- Claude: Compact JSON
|
|
293
|
-
-
|
|
294
|
-
-
|
|
291
|
+
- o200k_base: Compact JSON is smaller than or equal to Terset on mixed-type array (63 vs 63).
|
|
292
|
+
- Claude: Compact JSON is smaller than or equal to Terset on mixed-type array (89 vs 89).
|
|
293
|
+
- o200k_base: TOON's best is smaller than or equal to Terset in 2 of 11 cases: Translation batch (1,110 vs 1,139); Reviews, long text (3,662 vs 3,700).
|
|
294
|
+
- Claude: TOON's best is smaller than or equal to Terset in 1 of 11 cases: Deep nested config (394 vs 402).
|
|
295
|
+
- o200k_base: Plain CSV is 9 to 88 tokens smaller on 2 flat, uniform lists, because it needs no rules line. It has no types, it merges null, absent and empty text, and it cannot hold nesting. On orders with line items it is larger: 4,769 vs 3,899.
|
|
296
|
+
- Claude: Plain CSV is 23 to 38 tokens smaller on 4 flat, uniform lists, because it needs no rules line. It has no types, it merges null, absent and empty text, and it cannot hold nesting. On orders with line items it is larger: 6,490 vs 5,662.
|
|
297
|
+
|
|
298
|
+
Across 1,000 seeds of the same 11 case shapes, counted with o200k_base, Terset wins or loses against JSON compact and TOON the same way on every seed.
|
|
295
299
|
|
|
296
300
|
Token counts say nothing about answer quality. Terset is about fewer tokens.
|
|
297
301
|
Other tokenizers and models will give other counts. Full benchmark with pretty JSON and CSV counts:
|
|
@@ -305,33 +309,33 @@ On 244 questions and two models, accuracy is a tie. Terset used fewer input toke
|
|
|
305
309
|
|
|
306
310
|
| Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
|
|
307
311
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
308
|
-
| Terset |
|
|
309
|
-
| Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +
|
|
310
|
-
| JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +
|
|
311
|
-
| TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +
|
|
312
|
+
| Terset | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,188,659 | $5.65 | Reference | 2026-11-17 |
|
|
313
|
+
| Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +42.4% | 2026-11-12 |
|
|
314
|
+
| JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +80.6% | 2026-11-12 |
|
|
315
|
+
| TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +12.5% | 2026-11-12 |
|
|
312
316
|
|
|
313
317
|
#### GPT 6.1 Sol
|
|
314
318
|
|
|
315
319
|
| Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
|
|
316
320
|
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
317
|
-
| Terset |
|
|
318
|
-
| Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +
|
|
319
|
-
| JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +
|
|
320
|
-
| TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +
|
|
321
|
+
| Terset | 242 of 244 | 99.2% | 97.1% to 99.8% | 842,260 | $1.84 | Reference | 2026-11-17 |
|
|
322
|
+
| Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +35.6% | 2026-11-12 |
|
|
323
|
+
| JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +93.3% | 2026-11-12 |
|
|
324
|
+
| TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +19.1% | 2026-11-12 |
|
|
321
325
|
|
|
322
326
|
JSON is indented with 2 spaces. Compact JSON has no spaces or line breaks.
|
|
323
327
|
|
|
324
328
|
Accuracy is a tie. On 244 questions, every format's 95% interval overlaps every other format's, on both models.
|
|
325
|
-
Against compact JSON, Terset cost 29.
|
|
329
|
+
Against compact JSON, Terset cost 29.8% less on Claude Opus 5.5 and 26.2% less on GPT 6.1 Sol.
|
|
326
330
|
|
|
327
|
-
Cost is at API list price: input and output tokens times list prices read on 2026-11-12. Claude Opus 5.5 input tokens were counted with Anthropic's token counting endpoint over the exact request; GPT 6.1 Sol input tokens are as the model reported them. Temperature 0 where the model accepts it. Terset was rerun on 2026-11-
|
|
331
|
+
Cost is at API list price: input and output tokens times list prices read on 2026-11-12. Claude Opus 5.5 input tokens were counted with Anthropic's token counting endpoint over the exact request; GPT 6.1 Sol input tokens are as the model reported them. Temperature 0 where the model accepts it. Terset was rerun on 2026-11-17 with fitted rules, which send only the rules each text needs. The other formats did not change. Each row names its run date.
|
|
328
332
|
|
|
329
|
-
Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy). Every answer and its score: [2026-11-12](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-12) and [2026-11-
|
|
333
|
+
Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy). Every answer and its score: [2026-11-12](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-12) and [2026-11-17](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-17).
|
|
330
334
|
|
|
331
335
|
## When not to use it
|
|
332
336
|
|
|
333
337
|
<!-- docs:when-not -->
|
|
334
|
-
- **No lists of records.** Config files and deeply nested data stay JSON
|
|
338
|
+
- **No lists of records.** Config files and deeply nested data stay JSON. Terset sends no rules for them and saves a few tokens at most.
|
|
335
339
|
- **One-row lists.** A list needs two or more objects to become a table.
|
|
336
340
|
- **Mixed arrays.** A list with any item that is not an object stays JSON.
|
|
337
341
|
- **Nested lists.** Lists of objects inside a record stay JSON inside their cell. Only the top level becomes tables.
|
|
@@ -8,10 +8,17 @@ import unittest
|
|
|
8
8
|
PACKAGE = Path(__file__).resolve().parents[1]
|
|
9
9
|
sys.path.insert(0, str(PACKAGE / "src"))
|
|
10
10
|
|
|
11
|
-
from terset import RULES, decode, rules_for
|
|
11
|
+
from terset import RULES, decode, encode, rules_for
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
class RulesTests(unittest.TestCase):
|
|
15
|
+
def test_root_values_and_id_cells(self):
|
|
16
|
+
self.assertEqual(rules_for(encode({"name": "Ada"})), "")
|
|
17
|
+
self.assertEqual(rules_for(encode({"value": "true"})),
|
|
18
|
+
"JSON quotes distinguish numeric, boolean or null text from scalars.")
|
|
19
|
+
self.assertEqual(rules_for(encode([{"id": '"true"'}, {"id": "[1]"}])),
|
|
20
|
+
"Tables use tab-separated headers and rows. A leading bare id header marks string IDs.")
|
|
21
|
+
|
|
15
22
|
def test_shared_rules(self):
|
|
16
23
|
cases = json.loads((PACKAGE.parent / "terset/tests/rules-cases.json").read_text(encoding="utf-8"))
|
|
17
24
|
for case in cases:
|
|
@@ -113,6 +113,17 @@ class ConformanceTests(unittest.TestCase):
|
|
|
113
113
|
self.assertEqual(list(decode(encode(value))), ["2", "10", "z", "01"])
|
|
114
114
|
self.assertEqual(list(decode(encode(value))["z"]), ["b", "a"])
|
|
115
115
|
|
|
116
|
+
def test_header_fits_every_row_order(self):
|
|
117
|
+
skipped = [{"key": "a", "text": "A", "maxLength": 30}, {"key": "b", "text": "B", "description": "D", "maxLength": 61}]
|
|
118
|
+
self.assertEqual(encode(skipped), "[2]\nkey\ttext\tdescription\tmaxLength\na\tA\t\t30\nb\tB\tD\t61")
|
|
119
|
+
self.assertEqual(encode([{"a": 1, "b": 2}, {"c": 3, "a": 4}]), "[2]\nc\ta\tb\n\t1\t2\n3\t4\t")
|
|
120
|
+
self.assertEqual(encode([{"a": 1, "c": 2}, {"b": 3, "c": 4}]), "[2]\na\tb\tc\n1\t\t2\n\t3\t4")
|
|
121
|
+
self.assertEqual(encode({"people": {"ada": {"a": 1, "c": 2}, "lin": {"b": 3, "c": 4}}}), "people{2}:\n@key\ta\tb\tc\nada\t1\t\t2\nlin\t\t3\t4")
|
|
122
|
+
for value in (skipped, [{"a": 1, "b": 2}, {"c": 3, "a": 4}]):
|
|
123
|
+
self.assertEqual(json.dumps(decode(encode(value))), json.dumps(value))
|
|
124
|
+
conflict = [{"a": 1, "b": 2}, {"b": 3, "a": 4}]
|
|
125
|
+
self.assertEqual(encode(conflict), json.dumps(conflict, separators=(",", ":")))
|
|
126
|
+
|
|
116
127
|
def test_shared_references_are_not_cycles(self):
|
|
117
128
|
child = {"a": 1}
|
|
118
129
|
self.assertEqual(decode(encode({"left": child, "right": child})), {"left": child, "right": child})
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|