AlgoViz

Extraction, ETL and interchange formats

medium 6 min

An extraction pipeline turns versioned source records into auditable structured data.

Video lesson · Chapter 1 of 4

The same receipt arrives twice

0:00 / 3:28

Theory

Read or listen first. About 2 min.

What it is

An extraction pipeline turns versioned source records into auditable structured data. Identity and provenance travel with every field through decoding, validation and loading. A compact wire encoding changes how a record is represented, while a database key determines what happens if it is delivered twice.

Why it exists

Evaluate both the round trip through the encoding and the effect of reprocessing a revision. A pipeline can achieve excellent extraction accuracy and still double-count revenue if the loader treats every retry as new data.

Import the same revised receipt twice

Document revision → Typed rows (read version). Typed rows → Idempotent load (upsert validated rows)
Document revision → Typed rows (read version). Typed rows → Idempotent load (upsert validated rows)

Inside this chapter

Chapter sources
Structured Extraction Patterns
Structured extraction converts source text into typed fields alongside evidence. For a purchase order, record which line supports the amount and currency. Evaluate both exact field accuracy and correct abstentions. This exposes a model that fills every field plausibly when the source omits the answer.

Asking for a line reference alongside each value makes extraction auditable and testable. A reviewer can check the cited line in seconds, and you can run perturbation tests: edit the evidence line and see whether the value follows, or delete it and see whether the model abstains. A model that keeps answering EUR after the line changed is reading priors, not the document.

Example. Extract 19.95 and EUR from a receipt, together with the line number for each. Change only the currency line and verify the output changes accordingly.

Values travel with their evidence

RunamountcurrencyEvidence lines
Original receipt19.95EURamount line 7, currency line 8
Line 8 edited to GBP19.95GBPamount line 7, currency line 8
Currency line removed19.95nullamount line 7, currency none
Perturbation tests: change one line and only the dependent field should move.
Building ETL Pipelines with LLMs
In an extraction pipeline, persist the source identity, transformation version and validated output separately. Make loading idempotent by keying writes to the document revision. Reprocessing a corrected document should update its record and preserve provenance without duplicating the original transaction.

Connectors retry, queues redeliver and operators rerun backfills, so every load must be safe to repeat. Key the write on the source identity and revision rather than on an auto-increment ID, and store the extraction prompt or model version beside the output. When a better extractor ships, you can then reprocess on purpose and see exactly which records changed.

Example. Replay document 19 revision 3 twice. A unique source-and-revision key produces one committed revision while retaining two delivery attempts in logs.

Another example. A contracts pipeline reruns 10,000 documents after a prompt upgrade. Because rows are keyed by contract ID and revision, totals stay correct and a diff report lists the 312 records whose renewal date changed.

A replay finds the same key

Both deliveries appear in the logs. One revision is committed.
TOON (Token-Oriented Object Notation)
Token-Oriented Object Notation can encode uniform rows with a shared field header instead of repeating the same keys. The receiving side must implement the same format and escaping rules. Measure token counts with the deployed tokenizer, then verify that decoding reconstructs the original data.

The saving comes from stating the keys once and the row count up front, then listing values. That only helps when rows share the same fields. Both ends must implement the same quoting and escaping rules, otherwise a value containing a comma or a leading space can shift columns. Measure with your deployed tokenizer, because the saving depends on how it splits punctuation.

Example. Encode three rows sharing the keys sku and quantity using one shared header, decode them, and compare all three original records for equality.

Text
JSON: keys repeated on every row{"items":[{"sku":"A1","quantity":2},{"sku":"B7","quantity":1},{"sku":"C3","quantity":5}]}TOON: one header, then rowsitems[3]{sku,quantity}:  A1,2  B7,1  C3,5
Illustrative encoding of the three rows in the example. Decode the TOON back and compare with the original JSON before trusting it.
TOON vs JSON: When to Use Each
Uniform tables can benefit from a compact notation; deeply nested or irregular objects can erase the saving. JSON also has broad tooling and native schema support. Run a round-trip equivalence test and a task-quality test alongside the token measurement before changing an interchange contract.

Token savings are one input to the decision, alongside tooling, schema validation and the model's accuracy when reading or writing the format. Run three checks on real data: round-trip equality, token count with your tokenizer, and task quality with the new format in the prompt. Adopt it only where all three are acceptable.

Example. Test a table of uniform rows alongside a deeply nested record with optional fields. Compare valid round trips and token counts for both formats.

Where a tabular notation fits

  • Long lists of rows with identical fields
  • Read-only context for the model, such as catalogues or logs
  • Round-trip test passes on your real records

Where JSON is the safer contract

  • Deeply nested or irregular objects, optional fields
  • Payloads other systems validate with JSON Schema
  • Cases where null versus empty string must survive
Human corrections as evidence
Log the source region, extracted value, confidence signal and reviewer correction together. Track repeated failure by document layout. A correction can be a useful label after review, and a fixture should stay out of the data used to tune the same model.

A reviewer's fix tells you what the right answer was and, if you keep the source region, what kind of mistake happened: misread characters, wrong column, wrong row. Track corrections by document layout to find templates worth special handling. Keep a clean separation between cases used to tune the system and cases used to test it, or your evaluation will grade the model on its own homework.

Example. A reviewer moves 12.50 from tax to total. The pixels were legible, so the case should exercise column association rather than character recognition.

A correction becomes a labelled case
  1. 1Source regiontotals block, page 1
  2. 2Extracted12.50 placed in tax
  3. 3Reviewer fix12.50 moved to total
  4. 4Log with layoutvendor template V3
  5. 5Use oncelabel or test fixture, not both

Grouping corrections by layout shows whether one template causes most failures.

How it works

Step through the idea, or press Play.

Step 1 of 3

Document revision. receipt 19 · revision 3

Understand it

Worked example

Import the same revised receipt twice

  1. 1

    receipt 19 · revision 3

    A connector receives the same revision twice after reconnecting. Keep the document identity with both deliveries.

  2. 2

    2 rows with evidence spans

    Validate amounts and provenance; changing the interchange encoding must preserve both row contents and missing-value semantics.

  3. 3

    1 committed revision

    Use a unique document-and-revision key for the load. The retry finds the same record. A later revision can be processed as a distinct correction.

Follow the handoffs

Evaluate both the round trip through the encoding and the effect of reprocessing a revision. A pipeline can achieve excellent extraction accuracy and still double-count revenue if the loader treats every retry as new data.

Two deliveries, one committed revision

EventLoad keyWarehouse actionCurrent rows for receipt 19
Delivery 1 of revision 319 / r3insert2
Delivery 2 of revision 319 / r3match found, no change2
Later: revision 419 / r4correction replaces r3 as current2 (r3 kept in history)
Row counts stay stable on replay. A new revision is a deliberate change, not a duplicate.

Summary

The whole topic in one minute.

The wire format is part of the data contract. Lower cost cannot compensate for changing the meaning of a stored field.

Evaluate both the round trip through the encoding and the effect of reprocessing a revision. A pipeline can achieve excellent extraction accuracy and still double-count revenue if the loader treats every retry as new data.

Sources