terset 0.2.2__tar.gz → 0.2.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: terset
3
- Version: 0.2.2
3
+ Version: 0.2.4
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 shortest valid rules sentence. `RULES` keeps the full rules.
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
- # Send the fitted rules once in the system prompt, and the text as the data.
121
+ # One text: send its fitted rules right before the data.
122
122
  messages = [
123
- {"role": "system", "content": f"You read Terset data. {rules_for(text)}"},
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. Put `rules_for(text)` in the system prompt once. The data carries no legend.
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 | 507 | 695 | 802 | 846 | -27.1% |
184
- | UI text, uniform rows | 537 | 742 | 857 | 539 | -27.6% |
185
- | Translation batch | 1,228 | 1,450 | 1,623 | 1,660 | -15.3% |
186
- | File list, short | 9,661 | 12,806 | 14,453 | 10,643 | -24.6% |
187
- | File list, long | 16,487 | 19,627 | 22,129 | 17,476 | -16.0% |
188
- | 200 users | 4,721 | 6,811 | 7,983 | 5,081 | -30.7% |
189
- | Metrics, numbers and booleans | 2,572 | 4,797 | 6,115 | 2,654 | -46.4% |
190
- | Deep nested config | 432 | 371 | 456 | 442 | +16.4% |
191
- | Mixed-type array | 125 | 85 | 131 | 160 | +47.1% |
192
- | Reviews, long text | 3,804 | 4,279 | 4,787 | 3,833 | -11.1% |
193
- | Orders with line items | 3,971 | 4,585 | 5,673 | 4,419 | -13.4% |
194
- | Total | 44,045 | 56,248 | 65,009 | 47,753 | -21.7% |
195
-
196
- Terset, rules line included, is under TOON's best in 11 of 11 cases and
197
- under compact JSON in 9 of 11.
198
-
199
- Over all 11 cases, Terset totals 44,045 tokens: 21.7% fewer than JSON
200
- compact (56,248), 42.9% fewer than JSON pretty (77,071), 32.2% fewer than
201
- YAML (65,009), and 7.8% fewer than TOON's best (47,753).
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 | 1,066 | 1,463 | 1,341 | 1,387 | -27.1% |
208
- | UI text, uniform rows | 1,130 | 1,567 | 1,444 | 1,098 | -27.9% |
209
- | Translation batch | 2,267 | 2,714 | 2,519 | 2,581 | -16.5% |
210
- | File list, short | 13,164 | 18,357 | 18,820 | 14,216 | -28.3% |
211
- | File list, long | 25,229 | 30,419 | 31,925 | 26,274 | -17.1% |
212
- | 200 users | 7,001 | 10,712 | 10,509 | 7,126 | -34.6% |
213
- | Metrics, numbers and booleans | 2,860 | 6,957 | 7,314 | 2,907 | -58.9% |
214
- | Deep nested config | 674 | 610 | 613 | 602 | +10.5% |
215
- | Mixed-type array | 189 | 114 | 147 | 189 | +65.8% |
216
- | Reviews, long text | 5,181 | 6,009 | 6,313 | 5,149 | -13.8% |
217
- | Orders with line items | 5,738 | 6,795 | 7,000 | 5,691 | -15.6% |
218
- | Total | 64,499 | 85,717 | 87,945 | 67,220 | -24.8% |
219
-
220
- Terset, rules line included, is under TOON's best in 6 of 11 cases and
221
- under compact JSON in 9 of 11.
222
-
223
- Over all 11 cases, Terset totals 64,499 tokens: 24.8% fewer than JSON
224
- compact (85,717), 39.5% fewer than JSON pretty (106,601), 26.7% fewer than
225
- YAML (87,945), and 4.0% fewer than TOON's best (67,220).
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 | $4.26 | $5.85 | -$1.59 |
237
- | UI text, uniform rows | $4.52 | $6.27 | -$1.75 |
238
- | Translation batch | $9.07 | $10.86 | -$1.79 |
239
- | File list, short | $52.66 | $73.43 | -$20.77 |
240
- | File list, long | $100.92 | $121.68 | -$20.76 |
241
- | 200 users | $28.00 | $42.85 | -$14.85 |
242
- | Metrics, numbers and booleans | $11.44 | $27.83 | -$16.39 |
243
- | Deep nested config | $2.70 | $2.44 | +$0.26 |
244
- | Mixed-type array | $0.76 | $0.46 | +$0.30 |
245
- | Reviews, long text | $20.72 | $24.04 | -$3.32 |
246
- | Orders with line items | $22.95 | $27.18 | -$4.23 |
247
- | Total | $258.00 | $342.87 | -$84.87 |
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.01 | $1.39 | -$0.38 |
255
- | UI text, uniform rows | $1.07 | $1.48 | -$0.41 |
256
- | Translation batch | $2.46 | $2.90 | -$0.44 |
257
- | File list, short | $19.32 | $25.61 | -$6.29 |
258
- | File list, long | $32.97 | $39.25 | -$6.28 |
259
- | 200 users | $9.44 | $13.62 | -$4.18 |
260
- | Metrics, numbers and booleans | $5.14 | $9.59 | -$4.45 |
261
- | Deep nested config | $0.86 | $0.74 | +$0.12 |
262
- | Mixed-type array | $0.25 | $0.17 | +$0.08 |
263
- | Reviews, long text | $7.61 | $8.56 | -$0.95 |
264
- | Orders with line items | $7.94 | $9.17 | -$1.23 |
265
- | Total | $88.09 | $112.50 | -$24.41 |
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-13 UTC.
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
- - The case inputs are not in this repository. Eight are made-up data built from a fixed random seed. The UI text and translation cases use interface text from an app whose source is not public, so only their counts are published.
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 wins on deep nested config (371 vs 432) and mixed-type array (85 vs 125).
292
- - Claude: Compact JSON wins on deep nested config (610 vs 674) and mixed-type array (114 vs 189).
293
- - Claude: TOON's best is smaller than or equal to Terset in 5 of 11 cases: UI text, uniform rows (1,098 vs 1,130); Deep nested config (602 vs 674); Mixed-type array (189 vs 189); Reviews, long text (5,149 vs 5,181); Orders with line items (5,691 vs 5,738).
294
- - Plain CSV is 34 to 44 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.
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, ties 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:
@@ -303,35 +307,38 @@ On 244 questions and two models, accuracy is a tie. Terset used fewer input toke
303
307
 
304
308
  #### Claude Opus 5.5
305
309
 
306
- | Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
307
- | --- | --- | --- | --- | --- | --- | --- | --- |
308
- | Terset | 243 of 244 | 99.6% | 97.7% to 99.9% | 1,200,640 | $5.69 | Reference | 2026-11-13 |
309
- | Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +41.3% | 2026-11-12 |
310
- | JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +79.2% | 2026-11-12 |
311
- | TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +11.6% | 2026-11-12 |
310
+ | Format | Correct | Runs | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
311
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
312
+ | Terset | 243 of 244 | Mean of 3 runs: 243, 243, 243 | 99.6% | 97.7% to 99.9% | 1,188,659 | $5.65 | Reference | 2026-11-17 |
313
+ | Compact JSON | 241 of 244 | Mean of 3 runs: 241, 241, 241 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.05 | +42.4% | 2026-11-17 |
314
+ | JSON | 241 of 244 | Mean of 3 runs: 241, 241, 241 | 98.8% | 96.4% to 99.6% | 2,303,845 | $10.16 | +79.9% | 2026-11-17 |
315
+ | TOON | 241.7 of 244 | Mean of 3 runs: 242, 242, 241 (241 to 242) | 99.0% | 96.9% to 99.7% | 1,369,764 | $6.35 | +12.4% | 2026-11-17 |
312
316
 
313
317
  #### GPT 6.1 Sol
314
318
 
315
- | Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
316
- | --- | --- | --- | --- | --- | --- | --- | --- |
317
- | Terset | 241 of 244 | 98.8% | 96.4% to 99.6% | 848,844 | $1.85 | Reference | 2026-11-13 |
318
- | Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +34.8% | 2026-11-12 |
319
- | JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +92.2% | 2026-11-12 |
320
- | TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +18.3% | 2026-11-12 |
319
+ | Format | Correct | Runs | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
320
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
321
+ | Terset | 242 of 244 | Mean of 3 runs: 242, 241, 243 (241 to 243) | 99.2% | 97.1% to 99.8% | 842,260 | $1.84 | Reference | 2026-11-17 |
322
+ | Compact JSON | 235.3 of 244 | Mean of 3 runs: 236, 234, 236 (234 to 236) | 96.4% | 93.3% to 98.1% | 1,158,229 | $2.49 | +35.3% | 2026-11-17 |
323
+ | JSON | 237.3 of 244 | Mean of 3 runs: 237, 238, 237 (237 to 238) | 97.3% | 94.4% to 98.7% | 1,696,152 | $3.56 | +93.0% | 2026-11-17 |
324
+ | TOON | 242 of 244 | Mean of 3 runs: 242, 242, 242 | 99.2% | 97.1% to 99.8% | 1,020,628 | $2.20 | +19.2% | 2026-11-17 |
321
325
 
322
326
  JSON is indented with 2 spaces. Compact JSON has no spaces or line breaks.
323
327
 
328
+ On both models, every format ran three times on 2026-11-17. Each model's runs are one matched batch with the same setup. No run is picked. Each row shows the mean of its runs, and the Runs column lists each run's score.
329
+
324
330
  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.2% less on Claude Opus 5.5 and 25.8% less on GPT 6.1 Sol.
331
+ One format's score moved by up to two questions from run to run. A gap that small is noise, not a win.
332
+ Against compact JSON, Terset cost 29.8% less on Claude Opus 5.5 and 26.1% less on GPT 6.1 Sol.
326
333
 
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-13 after its format changed in 0.2.1; the other formats did not change. Each row names its run date.
334
+ Cost is at API list price: input and output tokens times list prices read on 2026-11-17. 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 ran with fitted rules, which send only the rules each text needs. Each row names its run date.
328
335
 
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-13](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-13).
336
+ 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), [2026-11-13](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-13) and [2026-11-17](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-17).
330
337
 
331
338
  ## When not to use it
332
339
 
333
340
  <!-- docs:when-not -->
334
- - **No lists of records.** Config files and deeply nested data stay JSON, so the rules line only adds tokens.
341
+ - **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
342
  - **One-row lists.** A list needs two or more objects to become a table.
336
343
  - **Mixed arrays.** A list with any item that is not an object stays JSON.
337
344
  - **Nested lists.** Lists of objects inside a record stay JSON inside their cell. Only the top level becomes tables.
@@ -343,6 +350,8 @@ Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy)
343
350
 
344
351
  <!-- docs:reference -->
345
352
  Every function, parameter, error and limit, for JavaScript and Python: [terset.dev/docs](https://terset.dev/docs).
353
+
354
+ The format is frozen at 1.0. JavaScript and Python are separate implementations that both pass the conformance suite at conformance/ in the repository.
346
355
  <!-- /docs:reference -->
347
356
 
348
357
  ## For agents
@@ -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 shortest valid rules sentence. `RULES` keeps the full rules.
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
- # Send the fitted rules once in the system prompt, and the text as the data.
98
+ # One text: send its fitted rules right before the data.
99
99
  messages = [
100
- {"role": "system", "content": f"You read Terset data. {rules_for(text)}"},
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. Put `rules_for(text)` in the system prompt once. The data carries no legend.
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 | 507 | 695 | 802 | 846 | -27.1% |
161
- | UI text, uniform rows | 537 | 742 | 857 | 539 | -27.6% |
162
- | Translation batch | 1,228 | 1,450 | 1,623 | 1,660 | -15.3% |
163
- | File list, short | 9,661 | 12,806 | 14,453 | 10,643 | -24.6% |
164
- | File list, long | 16,487 | 19,627 | 22,129 | 17,476 | -16.0% |
165
- | 200 users | 4,721 | 6,811 | 7,983 | 5,081 | -30.7% |
166
- | Metrics, numbers and booleans | 2,572 | 4,797 | 6,115 | 2,654 | -46.4% |
167
- | Deep nested config | 432 | 371 | 456 | 442 | +16.4% |
168
- | Mixed-type array | 125 | 85 | 131 | 160 | +47.1% |
169
- | Reviews, long text | 3,804 | 4,279 | 4,787 | 3,833 | -11.1% |
170
- | Orders with line items | 3,971 | 4,585 | 5,673 | 4,419 | -13.4% |
171
- | Total | 44,045 | 56,248 | 65,009 | 47,753 | -21.7% |
172
-
173
- Terset, rules line included, is under TOON's best in 11 of 11 cases and
174
- under compact JSON in 9 of 11.
175
-
176
- Over all 11 cases, Terset totals 44,045 tokens: 21.7% fewer than JSON
177
- compact (56,248), 42.9% fewer than JSON pretty (77,071), 32.2% fewer than
178
- YAML (65,009), and 7.8% fewer than TOON's best (47,753).
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 | 1,066 | 1,463 | 1,341 | 1,387 | -27.1% |
185
- | UI text, uniform rows | 1,130 | 1,567 | 1,444 | 1,098 | -27.9% |
186
- | Translation batch | 2,267 | 2,714 | 2,519 | 2,581 | -16.5% |
187
- | File list, short | 13,164 | 18,357 | 18,820 | 14,216 | -28.3% |
188
- | File list, long | 25,229 | 30,419 | 31,925 | 26,274 | -17.1% |
189
- | 200 users | 7,001 | 10,712 | 10,509 | 7,126 | -34.6% |
190
- | Metrics, numbers and booleans | 2,860 | 6,957 | 7,314 | 2,907 | -58.9% |
191
- | Deep nested config | 674 | 610 | 613 | 602 | +10.5% |
192
- | Mixed-type array | 189 | 114 | 147 | 189 | +65.8% |
193
- | Reviews, long text | 5,181 | 6,009 | 6,313 | 5,149 | -13.8% |
194
- | Orders with line items | 5,738 | 6,795 | 7,000 | 5,691 | -15.6% |
195
- | Total | 64,499 | 85,717 | 87,945 | 67,220 | -24.8% |
196
-
197
- Terset, rules line included, is under TOON's best in 6 of 11 cases and
198
- under compact JSON in 9 of 11.
199
-
200
- Over all 11 cases, Terset totals 64,499 tokens: 24.8% fewer than JSON
201
- compact (85,717), 39.5% fewer than JSON pretty (106,601), 26.7% fewer than
202
- YAML (87,945), and 4.0% fewer than TOON's best (67,220).
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 | $4.26 | $5.85 | -$1.59 |
214
- | UI text, uniform rows | $4.52 | $6.27 | -$1.75 |
215
- | Translation batch | $9.07 | $10.86 | -$1.79 |
216
- | File list, short | $52.66 | $73.43 | -$20.77 |
217
- | File list, long | $100.92 | $121.68 | -$20.76 |
218
- | 200 users | $28.00 | $42.85 | -$14.85 |
219
- | Metrics, numbers and booleans | $11.44 | $27.83 | -$16.39 |
220
- | Deep nested config | $2.70 | $2.44 | +$0.26 |
221
- | Mixed-type array | $0.76 | $0.46 | +$0.30 |
222
- | Reviews, long text | $20.72 | $24.04 | -$3.32 |
223
- | Orders with line items | $22.95 | $27.18 | -$4.23 |
224
- | Total | $258.00 | $342.87 | -$84.87 |
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.01 | $1.39 | -$0.38 |
232
- | UI text, uniform rows | $1.07 | $1.48 | -$0.41 |
233
- | Translation batch | $2.46 | $2.90 | -$0.44 |
234
- | File list, short | $19.32 | $25.61 | -$6.29 |
235
- | File list, long | $32.97 | $39.25 | -$6.28 |
236
- | 200 users | $9.44 | $13.62 | -$4.18 |
237
- | Metrics, numbers and booleans | $5.14 | $9.59 | -$4.45 |
238
- | Deep nested config | $0.86 | $0.74 | +$0.12 |
239
- | Mixed-type array | $0.25 | $0.17 | +$0.08 |
240
- | Reviews, long text | $7.61 | $8.56 | -$0.95 |
241
- | Orders with line items | $7.94 | $9.17 | -$1.23 |
242
- | Total | $88.09 | $112.50 | -$24.41 |
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-13 UTC.
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
- - The case inputs are not in this repository. Eight are made-up data built from a fixed random seed. The UI text and translation cases use interface text from an app whose source is not public, so only their counts are published.
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 wins on deep nested config (371 vs 432) and mixed-type array (85 vs 125).
269
- - Claude: Compact JSON wins on deep nested config (610 vs 674) and mixed-type array (114 vs 189).
270
- - Claude: TOON's best is smaller than or equal to Terset in 5 of 11 cases: UI text, uniform rows (1,098 vs 1,130); Deep nested config (602 vs 674); Mixed-type array (189 vs 189); Reviews, long text (5,149 vs 5,181); Orders with line items (5,691 vs 5,738).
271
- - Plain CSV is 34 to 44 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.
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, ties 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:
@@ -280,35 +284,38 @@ On 244 questions and two models, accuracy is a tie. Terset used fewer input toke
280
284
 
281
285
  #### Claude Opus 5.5
282
286
 
283
- | Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
284
- | --- | --- | --- | --- | --- | --- | --- | --- |
285
- | Terset | 243 of 244 | 99.6% | 97.7% to 99.9% | 1,200,640 | $5.69 | Reference | 2026-11-13 |
286
- | Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +41.3% | 2026-11-12 |
287
- | JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +79.2% | 2026-11-12 |
288
- | TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +11.6% | 2026-11-12 |
287
+ | Format | Correct | Runs | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
288
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
289
+ | Terset | 243 of 244 | Mean of 3 runs: 243, 243, 243 | 99.6% | 97.7% to 99.9% | 1,188,659 | $5.65 | Reference | 2026-11-17 |
290
+ | Compact JSON | 241 of 244 | Mean of 3 runs: 241, 241, 241 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.05 | +42.4% | 2026-11-17 |
291
+ | JSON | 241 of 244 | Mean of 3 runs: 241, 241, 241 | 98.8% | 96.4% to 99.6% | 2,303,845 | $10.16 | +79.9% | 2026-11-17 |
292
+ | TOON | 241.7 of 244 | Mean of 3 runs: 242, 242, 241 (241 to 242) | 99.0% | 96.9% to 99.7% | 1,369,764 | $6.35 | +12.4% | 2026-11-17 |
289
293
 
290
294
  #### GPT 6.1 Sol
291
295
 
292
- | Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
293
- | --- | --- | --- | --- | --- | --- | --- | --- |
294
- | Terset | 241 of 244 | 98.8% | 96.4% to 99.6% | 848,844 | $1.85 | Reference | 2026-11-13 |
295
- | Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +34.8% | 2026-11-12 |
296
- | JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +92.2% | 2026-11-12 |
297
- | TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +18.3% | 2026-11-12 |
296
+ | Format | Correct | Runs | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
297
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
298
+ | Terset | 242 of 244 | Mean of 3 runs: 242, 241, 243 (241 to 243) | 99.2% | 97.1% to 99.8% | 842,260 | $1.84 | Reference | 2026-11-17 |
299
+ | Compact JSON | 235.3 of 244 | Mean of 3 runs: 236, 234, 236 (234 to 236) | 96.4% | 93.3% to 98.1% | 1,158,229 | $2.49 | +35.3% | 2026-11-17 |
300
+ | JSON | 237.3 of 244 | Mean of 3 runs: 237, 238, 237 (237 to 238) | 97.3% | 94.4% to 98.7% | 1,696,152 | $3.56 | +93.0% | 2026-11-17 |
301
+ | TOON | 242 of 244 | Mean of 3 runs: 242, 242, 242 | 99.2% | 97.1% to 99.8% | 1,020,628 | $2.20 | +19.2% | 2026-11-17 |
298
302
 
299
303
  JSON is indented with 2 spaces. Compact JSON has no spaces or line breaks.
300
304
 
305
+ On both models, every format ran three times on 2026-11-17. Each model's runs are one matched batch with the same setup. No run is picked. Each row shows the mean of its runs, and the Runs column lists each run's score.
306
+
301
307
  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.2% less on Claude Opus 5.5 and 25.8% less on GPT 6.1 Sol.
308
+ One format's score moved by up to two questions from run to run. A gap that small is noise, not a win.
309
+ Against compact JSON, Terset cost 29.8% less on Claude Opus 5.5 and 26.1% less on GPT 6.1 Sol.
303
310
 
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-13 after its format changed in 0.2.1; the other formats did not change. Each row names its run date.
311
+ Cost is at API list price: input and output tokens times list prices read on 2026-11-17. 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 ran with fitted rules, which send only the rules each text needs. Each row names its run date.
305
312
 
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-13](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-13).
313
+ 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), [2026-11-13](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-13) and [2026-11-17](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-17).
307
314
 
308
315
  ## When not to use it
309
316
 
310
317
  <!-- docs:when-not -->
311
- - **No lists of records.** Config files and deeply nested data stay JSON, so the rules line only adds tokens.
318
+ - **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
319
  - **One-row lists.** A list needs two or more objects to become a table.
313
320
  - **Mixed arrays.** A list with any item that is not an object stays JSON.
314
321
  - **Nested lists.** Lists of objects inside a record stay JSON inside their cell. Only the top level becomes tables.
@@ -320,6 +327,8 @@ Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy)
320
327
 
321
328
  <!-- docs:reference -->
322
329
  Every function, parameter, error and limit, for JavaScript and Python: [terset.dev/docs](https://terset.dev/docs).
330
+
331
+ The format is frozen at 1.0. JavaScript and Python are separate implementations that both pass the conformance suite at conformance/ in the repository.
323
332
  <!-- /docs:reference -->
324
333
 
325
334
  ## For agents
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "terset"
7
- version = "0.2.2"
7
+ version = "0.2.4"
8
8
  description = "Encode JSON records as compact, typed tables."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -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
- _BASE_RULES = ". ".join(_SENTENCES[:5]) + "."
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
- sentences = [_BASE_RULES]
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 _table(value: list[JsonObject], keyed: bool, entry_keys: list[str] | None = None, max_bytes: int | float = math.inf) -> str | None:
209
- if len(value) < 2:
210
- return None
211
- rows = [{_unicode(key): child for key, child in row.items()} for row in value]
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
- if key not in positions:
218
- positions[key] = len(fields)
226
+ index = positions.get(key)
227
+ if index is None:
228
+ index = len(fields)
229
+ positions[key] = index
219
230
  fields.append(key)
220
- index = positions[key]
221
- if index <= previous:
222
- return None
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.2
3
+ Version: 0.2.4
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 shortest valid rules sentence. `RULES` keeps the full rules.
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
- # Send the fitted rules once in the system prompt, and the text as the data.
121
+ # One text: send its fitted rules right before the data.
122
122
  messages = [
123
- {"role": "system", "content": f"You read Terset data. {rules_for(text)}"},
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. Put `rules_for(text)` in the system prompt once. The data carries no legend.
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 | 507 | 695 | 802 | 846 | -27.1% |
184
- | UI text, uniform rows | 537 | 742 | 857 | 539 | -27.6% |
185
- | Translation batch | 1,228 | 1,450 | 1,623 | 1,660 | -15.3% |
186
- | File list, short | 9,661 | 12,806 | 14,453 | 10,643 | -24.6% |
187
- | File list, long | 16,487 | 19,627 | 22,129 | 17,476 | -16.0% |
188
- | 200 users | 4,721 | 6,811 | 7,983 | 5,081 | -30.7% |
189
- | Metrics, numbers and booleans | 2,572 | 4,797 | 6,115 | 2,654 | -46.4% |
190
- | Deep nested config | 432 | 371 | 456 | 442 | +16.4% |
191
- | Mixed-type array | 125 | 85 | 131 | 160 | +47.1% |
192
- | Reviews, long text | 3,804 | 4,279 | 4,787 | 3,833 | -11.1% |
193
- | Orders with line items | 3,971 | 4,585 | 5,673 | 4,419 | -13.4% |
194
- | Total | 44,045 | 56,248 | 65,009 | 47,753 | -21.7% |
195
-
196
- Terset, rules line included, is under TOON's best in 11 of 11 cases and
197
- under compact JSON in 9 of 11.
198
-
199
- Over all 11 cases, Terset totals 44,045 tokens: 21.7% fewer than JSON
200
- compact (56,248), 42.9% fewer than JSON pretty (77,071), 32.2% fewer than
201
- YAML (65,009), and 7.8% fewer than TOON's best (47,753).
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 | 1,066 | 1,463 | 1,341 | 1,387 | -27.1% |
208
- | UI text, uniform rows | 1,130 | 1,567 | 1,444 | 1,098 | -27.9% |
209
- | Translation batch | 2,267 | 2,714 | 2,519 | 2,581 | -16.5% |
210
- | File list, short | 13,164 | 18,357 | 18,820 | 14,216 | -28.3% |
211
- | File list, long | 25,229 | 30,419 | 31,925 | 26,274 | -17.1% |
212
- | 200 users | 7,001 | 10,712 | 10,509 | 7,126 | -34.6% |
213
- | Metrics, numbers and booleans | 2,860 | 6,957 | 7,314 | 2,907 | -58.9% |
214
- | Deep nested config | 674 | 610 | 613 | 602 | +10.5% |
215
- | Mixed-type array | 189 | 114 | 147 | 189 | +65.8% |
216
- | Reviews, long text | 5,181 | 6,009 | 6,313 | 5,149 | -13.8% |
217
- | Orders with line items | 5,738 | 6,795 | 7,000 | 5,691 | -15.6% |
218
- | Total | 64,499 | 85,717 | 87,945 | 67,220 | -24.8% |
219
-
220
- Terset, rules line included, is under TOON's best in 6 of 11 cases and
221
- under compact JSON in 9 of 11.
222
-
223
- Over all 11 cases, Terset totals 64,499 tokens: 24.8% fewer than JSON
224
- compact (85,717), 39.5% fewer than JSON pretty (106,601), 26.7% fewer than
225
- YAML (87,945), and 4.0% fewer than TOON's best (67,220).
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 | $4.26 | $5.85 | -$1.59 |
237
- | UI text, uniform rows | $4.52 | $6.27 | -$1.75 |
238
- | Translation batch | $9.07 | $10.86 | -$1.79 |
239
- | File list, short | $52.66 | $73.43 | -$20.77 |
240
- | File list, long | $100.92 | $121.68 | -$20.76 |
241
- | 200 users | $28.00 | $42.85 | -$14.85 |
242
- | Metrics, numbers and booleans | $11.44 | $27.83 | -$16.39 |
243
- | Deep nested config | $2.70 | $2.44 | +$0.26 |
244
- | Mixed-type array | $0.76 | $0.46 | +$0.30 |
245
- | Reviews, long text | $20.72 | $24.04 | -$3.32 |
246
- | Orders with line items | $22.95 | $27.18 | -$4.23 |
247
- | Total | $258.00 | $342.87 | -$84.87 |
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.01 | $1.39 | -$0.38 |
255
- | UI text, uniform rows | $1.07 | $1.48 | -$0.41 |
256
- | Translation batch | $2.46 | $2.90 | -$0.44 |
257
- | File list, short | $19.32 | $25.61 | -$6.29 |
258
- | File list, long | $32.97 | $39.25 | -$6.28 |
259
- | 200 users | $9.44 | $13.62 | -$4.18 |
260
- | Metrics, numbers and booleans | $5.14 | $9.59 | -$4.45 |
261
- | Deep nested config | $0.86 | $0.74 | +$0.12 |
262
- | Mixed-type array | $0.25 | $0.17 | +$0.08 |
263
- | Reviews, long text | $7.61 | $8.56 | -$0.95 |
264
- | Orders with line items | $7.94 | $9.17 | -$1.23 |
265
- | Total | $88.09 | $112.50 | -$24.41 |
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-13 UTC.
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
- - The case inputs are not in this repository. Eight are made-up data built from a fixed random seed. The UI text and translation cases use interface text from an app whose source is not public, so only their counts are published.
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 wins on deep nested config (371 vs 432) and mixed-type array (85 vs 125).
292
- - Claude: Compact JSON wins on deep nested config (610 vs 674) and mixed-type array (114 vs 189).
293
- - Claude: TOON's best is smaller than or equal to Terset in 5 of 11 cases: UI text, uniform rows (1,098 vs 1,130); Deep nested config (602 vs 674); Mixed-type array (189 vs 189); Reviews, long text (5,149 vs 5,181); Orders with line items (5,691 vs 5,738).
294
- - Plain CSV is 34 to 44 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.
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, ties 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:
@@ -303,35 +307,38 @@ On 244 questions and two models, accuracy is a tie. Terset used fewer input toke
303
307
 
304
308
  #### Claude Opus 5.5
305
309
 
306
- | Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
307
- | --- | --- | --- | --- | --- | --- | --- | --- |
308
- | Terset | 243 of 244 | 99.6% | 97.7% to 99.9% | 1,200,640 | $5.69 | Reference | 2026-11-13 |
309
- | Compact JSON | 241 of 244 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.04 | +41.3% | 2026-11-12 |
310
- | JSON | 239 of 244 | 98.0% | 95.3% to 99.1% | 2,303,845 | $10.20 | +79.2% | 2026-11-12 |
311
- | TOON | 242 of 244 | 99.2% | 97.1% to 99.8% | 1,369,764 | $6.35 | +11.6% | 2026-11-12 |
310
+ | Format | Correct | Runs | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
311
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
312
+ | Terset | 243 of 244 | Mean of 3 runs: 243, 243, 243 | 99.6% | 97.7% to 99.9% | 1,188,659 | $5.65 | Reference | 2026-11-17 |
313
+ | Compact JSON | 241 of 244 | Mean of 3 runs: 241, 241, 241 | 98.8% | 96.4% to 99.6% | 1,766,161 | $8.05 | +42.4% | 2026-11-17 |
314
+ | JSON | 241 of 244 | Mean of 3 runs: 241, 241, 241 | 98.8% | 96.4% to 99.6% | 2,303,845 | $10.16 | +79.9% | 2026-11-17 |
315
+ | TOON | 241.7 of 244 | Mean of 3 runs: 242, 242, 241 (241 to 242) | 99.0% | 96.9% to 99.7% | 1,369,764 | $6.35 | +12.4% | 2026-11-17 |
312
316
 
313
317
  #### GPT 6.1 Sol
314
318
 
315
- | Format | Correct | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
316
- | --- | --- | --- | --- | --- | --- | --- | --- |
317
- | Terset | 241 of 244 | 98.8% | 96.4% to 99.6% | 848,844 | $1.85 | Reference | 2026-11-13 |
318
- | Compact JSON | 236 of 244 | 96.7% | 93.7% to 98.3% | 1,158,229 | $2.50 | +34.8% | 2026-11-12 |
319
- | JSON | 237 of 244 | 97.1% | 94.2% to 98.6% | 1,696,152 | $3.56 | +92.2% | 2026-11-12 |
320
- | TOON | 239 of 244 | 98.0% | 95.3% to 99.1% | 1,020,628 | $2.19 | +18.3% | 2026-11-12 |
319
+ | Format | Correct | Runs | Accuracy | 95% interval | Input tokens | Cost | Costs more than Terset | Run date |
320
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- |
321
+ | Terset | 242 of 244 | Mean of 3 runs: 242, 241, 243 (241 to 243) | 99.2% | 97.1% to 99.8% | 842,260 | $1.84 | Reference | 2026-11-17 |
322
+ | Compact JSON | 235.3 of 244 | Mean of 3 runs: 236, 234, 236 (234 to 236) | 96.4% | 93.3% to 98.1% | 1,158,229 | $2.49 | +35.3% | 2026-11-17 |
323
+ | JSON | 237.3 of 244 | Mean of 3 runs: 237, 238, 237 (237 to 238) | 97.3% | 94.4% to 98.7% | 1,696,152 | $3.56 | +93.0% | 2026-11-17 |
324
+ | TOON | 242 of 244 | Mean of 3 runs: 242, 242, 242 | 99.2% | 97.1% to 99.8% | 1,020,628 | $2.20 | +19.2% | 2026-11-17 |
321
325
 
322
326
  JSON is indented with 2 spaces. Compact JSON has no spaces or line breaks.
323
327
 
328
+ On both models, every format ran three times on 2026-11-17. Each model's runs are one matched batch with the same setup. No run is picked. Each row shows the mean of its runs, and the Runs column lists each run's score.
329
+
324
330
  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.2% less on Claude Opus 5.5 and 25.8% less on GPT 6.1 Sol.
331
+ One format's score moved by up to two questions from run to run. A gap that small is noise, not a win.
332
+ Against compact JSON, Terset cost 29.8% less on Claude Opus 5.5 and 26.1% less on GPT 6.1 Sol.
326
333
 
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-13 after its format changed in 0.2.1; the other formats did not change. Each row names its run date.
334
+ Cost is at API list price: input and output tokens times list prices read on 2026-11-17. 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 ran with fitted rules, which send only the rules each text needs. Each row names its run date.
328
335
 
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-13](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-13).
336
+ 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), [2026-11-13](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-13) and [2026-11-17](https://github.com/stackdeckhq/terset/tree/main/bench/published/2026-11-17).
330
337
 
331
338
  ## When not to use it
332
339
 
333
340
  <!-- docs:when-not -->
334
- - **No lists of records.** Config files and deeply nested data stay JSON, so the rules line only adds tokens.
341
+ - **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
342
  - **One-row lists.** A list needs two or more objects to become a table.
336
343
  - **Mixed arrays.** A list with any item that is not an object stays JSON.
337
344
  - **Nested lists.** Lists of objects inside a record stay JSON inside their cell. Only the top level becomes tables.
@@ -343,6 +350,8 @@ Method and limits: [terset.dev/benchmark](https://terset.dev/benchmark#accuracy)
343
350
 
344
351
  <!-- docs:reference -->
345
352
  Every function, parameter, error and limit, for JavaScript and Python: [terset.dev/docs](https://terset.dev/docs).
353
+
354
+ The format is frozen at 1.0. JavaScript and Python are separate implementations that both pass the conformance suite at conformance/ in the repository.
346
355
  <!-- /docs:reference -->
347
356
 
348
357
  ## For agents
@@ -13,7 +13,7 @@ from terset import decode, encode
13
13
 
14
14
  class DecoderErrorTests(unittest.TestCase):
15
15
  def test_specific_errors(self):
16
- cases = json.loads((PACKAGE.parent / "terset/tests/rejection-errors.json").read_text(encoding="utf-8"))
16
+ cases = json.loads((PACKAGE.parents[1] / "conformance/rejection-errors.json").read_text(encoding="utf-8"))
17
17
  for case in cases:
18
18
  with self.subTest(case=case["name"]), self.assertRaises(ValueError) as caught:
19
19
  decode(case["text"])
@@ -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:
@@ -20,7 +27,7 @@ class RulesTests(unittest.TestCase):
20
27
  self.assertEqual(cases[-1]["rules"], RULES)
21
28
 
22
29
  def test_same_rejections(self):
23
- cases = json.loads((PACKAGE.parent / "terset/tests/rejection-errors.json").read_text(encoding="utf-8"))
30
+ cases = json.loads((PACKAGE.parents[1] / "conformance/rejection-errors.json").read_text(encoding="utf-8"))
24
31
  cases.append({"text": "{2}\n@key\tname\na\tAda\nb\tLin"})
25
32
  for case in cases:
26
33
  with self.subTest(text=case["text"]):
@@ -9,6 +9,7 @@ import sys
9
9
  import unittest
10
10
 
11
11
  PACKAGE = Path(__file__).resolve().parents[1]
12
+ CONFORMANCE = PACKAGE.parents[1] / "conformance"
12
13
  sys.path.insert(0, str(PACKAGE / "src"))
13
14
 
14
15
  from terset import RULES, decode, encode # noqa: E402
@@ -42,6 +43,16 @@ def expand_case(case):
42
43
 
43
44
 
44
45
  class ConformanceTests(unittest.TestCase):
46
+ def assert_value_and_key_order(self, actual, expected):
47
+ self.assertEqual(actual, expected)
48
+ if isinstance(expected, dict):
49
+ self.assertEqual(list(actual), list(expected))
50
+ for key in expected:
51
+ self.assert_value_and_key_order(actual[key], expected[key])
52
+ elif isinstance(expected, list):
53
+ for actual_item, expected_item in zip(actual, expected):
54
+ self.assert_value_and_key_order(actual_item, expected_item)
55
+
45
56
  def test_counted_tables(self):
46
57
  value = {"people": [{"name": "Ada"}, {"name": "Lin"}]}
47
58
  text = "people[2]:\nname\nAda\nLin"
@@ -59,16 +70,16 @@ class ConformanceTests(unittest.TestCase):
59
70
  def test_shared_cases(self):
60
71
  # Fixture numbers describe JavaScript values. Parse them as binary64,
61
72
  # including integral decimal tokens outside Python's exact-int range.
62
- cases = json.loads((PACKAGE.parent / "terset/tests/cases.json").read_text(encoding="utf-8"), parse_int=float)
73
+ cases = json.loads((CONFORMANCE / "cases.json").read_text(encoding="utf-8"), parse_int=float)
63
74
  for case in cases:
64
75
  with self.subTest(case=case["name"]):
65
76
  value, encoded = expand_case(case)
66
77
  self.assertEqual(encode(value), encoded)
67
- self.assertEqual(decode(encoded), value)
78
+ self.assert_value_and_key_order(decode(encoded), value)
68
79
  self.assertEqual(encode(decode(encoded)), encoded)
69
80
 
70
81
  def test_shared_integer_inputs(self):
71
- cases = json.loads((PACKAGE.parent / "terset/tests/cases.json").read_text(encoding="utf-8"), parse_int=fixture_integer)
82
+ cases = json.loads((CONFORMANCE / "cases.json").read_text(encoding="utf-8"), parse_int=fixture_integer)
72
83
  boundary = next(case for case in cases if case["name"] == "number formatting boundaries")
73
84
  self.assertIs(type(boundary["input"]["values"][0]), int)
74
85
  self.assertIs(type(boundary["input"]["values"][3]), int)
@@ -76,7 +87,7 @@ class ConformanceTests(unittest.TestCase):
76
87
  with self.subTest(case=case["name"]):
77
88
  value, encoded = expand_case(case)
78
89
  self.assertEqual(encode(value), encoded)
79
- self.assertEqual(decode(encoded), value)
90
+ self.assert_value_and_key_order(decode(encoded), value)
80
91
 
81
92
  def test_equal_size_fallback(self):
82
93
  rows = [{"a": 1}, {"b": 1}, {"c": 1}, {"d": 1}, {"e": 1}, {}]
@@ -113,6 +124,17 @@ class ConformanceTests(unittest.TestCase):
113
124
  self.assertEqual(list(decode(encode(value))), ["2", "10", "z", "01"])
114
125
  self.assertEqual(list(decode(encode(value))["z"]), ["b", "a"])
115
126
 
127
+ def test_header_fits_every_row_order(self):
128
+ skipped = [{"key": "a", "text": "A", "maxLength": 30}, {"key": "b", "text": "B", "description": "D", "maxLength": 61}]
129
+ self.assertEqual(encode(skipped), "[2]\nkey\ttext\tdescription\tmaxLength\na\tA\t\t30\nb\tB\tD\t61")
130
+ self.assertEqual(encode([{"a": 1, "b": 2}, {"c": 3, "a": 4}]), "[2]\nc\ta\tb\n\t1\t2\n3\t4\t")
131
+ self.assertEqual(encode([{"a": 1, "c": 2}, {"b": 3, "c": 4}]), "[2]\na\tb\tc\n1\t\t2\n\t3\t4")
132
+ 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")
133
+ for value in (skipped, [{"a": 1, "b": 2}, {"c": 3, "a": 4}]):
134
+ self.assertEqual(json.dumps(decode(encode(value))), json.dumps(value))
135
+ conflict = [{"a": 1, "b": 2}, {"b": 3, "a": 4}]
136
+ self.assertEqual(encode(conflict), json.dumps(conflict, separators=(",", ":")))
137
+
116
138
  def test_shared_references_are_not_cycles(self):
117
139
  child = {"a": 1}
118
140
  self.assertEqual(decode(encode({"left": child, "right": child})), {"left": child, "right": child})
@@ -164,7 +186,7 @@ class ConformanceTests(unittest.TestCase):
164
186
  self.assertEqual(decode("x: (()=>7)()"), {"x": "(()=>7)()"})
165
187
 
166
188
  def test_shared_rejections(self):
167
- texts = json.loads((PACKAGE.parent / "terset/tests/rejections.json").read_text(encoding="utf-8"))
189
+ texts = json.loads((CONFORMANCE / "rejections.json").read_text(encoding="utf-8"))
168
190
  for text in texts:
169
191
  with self.subTest(text=text), self.assertRaises(ValueError):
170
192
  decode(text)
File without changes
File without changes
File without changes
File without changes
File without changes