Token LabBuscar

APPLIED AI / READ → RUN → MODIFY → TEST

Document extraction: JSON shape is not factual correctness

Build a validation boundary between a candidate extraction and a downstream business workflow.

English technical content with localized navigation. Chinese edition is also available.

Study guide & lesson outlineLEARNING CONTRACT

Understand → implement → investigate

Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.

1 · Where you encounter this

A common document-assistant task is extracting fields into a table. OpenAI’s API separately offers Structured Outputs for schema-constrained responses. That API capability is not a claim that every ChatGPT table uses the same implementation. Here you supply a candidate JSON object; the local code validates it without calling a model.

Official product / API documentation ↗

Source checked 2026-10-25. Product behavior is publicly documented; the code below is an independent teaching implementation.

2 · The engineering decision

Use two independent gates. Shape validation rejects missing, unexpected or incorrectly typed fields. Evidence validation checks whether quoted text appears in the source and supports the declared amount. Neither gate alone solves semantic ambiguity: a document can contain subtotal, tax and total, all valid numbers. Keep rejection visible instead of silently coercing a string to a number or substituting zero.

  1. The supplied candidate has the four allowed keys and a finite nonnegative amount.
  2. The invoice ID occurs in the source; the quote USD 42.50 also occurs.
  3. Formatting 42.5 to two decimals gives 42.50, which matches the evidence.
  4. Change amount to 425: shapeValid stays true but accepted becomes false. This separates formatting from grounding.

3 · Run and change the input

Runs entirely in this browser. No model call, network request, API key or upload. Input is JSON; this is a deterministic component demo, not a generative assistant.

{
  "shapeValid": true,
  "accepted": true,
  "errors": [],
  "limitation": "Exact-text checks only; conflicting totals and invoice context need further validation."
}

Initial output above is the result of the example, computed during the build. Run after editing to calculate your result.

4 · Complete source code

Requires a modern browser or Node.js 18+. Download the module, save this runner beside it and execute node demo.mjs. No npm dependencies.

Download source ↓
// Validate a candidate output supplied by a person or model. No model call.
export const example = {
  source: 'Invoice A-17. Total: USD 42.50.',
  candidate: {invoice_id: 'A-17', currency: 'USD', amount: 42.5, evidence: 'USD 42.50'}
};
export function run({source, candidate}) {
  const errors = [];
  if (typeof source !== 'string' || source.length > 20000) throw Error('Invalid source');
  if (!candidate || typeof candidate !== 'object' || Array.isArray(candidate)) throw Error('candidate must be an object');
  const keys = ['invoice_id', 'currency', 'amount', 'evidence'];
  if (Object.keys(candidate).some(key => !keys.includes(key))) errors.push('Unexpected field');
  for (const key of ['invoice_id', 'currency', 'evidence']) {
    if (typeof candidate[key] !== 'string' || !candidate[key].trim()) errors.push(`Invalid ${key}`);
  }
  if (!Number.isFinite(candidate.amount) || candidate.amount < 0) errors.push('Invalid amount');
  if (!['USD', 'EUR', 'CNY'].includes(candidate.currency)) errors.push('Unsupported currency');
  const shapeValid = errors.length === 0;
  if (shapeValid) {
    if (!source.includes(candidate.invoice_id)) errors.push('Invoice ID absent from source');
    if (!source.includes(candidate.evidence)) errors.push('Evidence quote absent from source');
    const expected = `${candidate.currency} ${candidate.amount.toFixed(2)}`;
    if (!candidate.evidence.includes(expected)) errors.push('Amount/currency not supported by quote');
  }
  return {shapeValid, accepted: errors.length === 0, errors,
    limitation: 'Exact-text checks only; conflicting totals and invoice context need further validation.'};
}
import {run, example} from './validated-extraction.mjs';
console.log(JSON.stringify(run(example), null, 2));

5 · Failure cases and production gaps

Exact substring checks are teaching constraints, not invoice understanding. Multiple invoices, contradictory amounts, localization and decimal precision require stronger handling. Financial production systems usually represent amounts as decimal or integer minor units. Handle refusal, incomplete output and parse failure separately when integrating a real model.

6 · Your implementation exercise

Add cases for an absent quote, numeric strings, unexpected fields and two conflicting totals. Extend the contract with source span and invoice identity, and explain which cases still require review.

Deliver changed code, at least one passing and one failing case, and an explanation of what the test cannot prove.

Continue with the underlying concept →
← All applied tutorials
MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

Shape validation and exact evidence checks are separate. Decimal display uses two digits but is not exact monetary arithmetic. Substrings cannot resolve conflicting invoice totals.

Implementation, invariants and declared evidence boundary reviewed; existing acceptance tests remain part of the suite.

Before running: predict a boundary case

Shape validation and exact evidence checks are separate. Decimal display uses two digits but is not exact monetary arithmetic. Substrings cannot resolve conflicting invoice totals.

Hint: isolate the assumption

Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.

Minimal executable example

Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node application-validated-extraction.mjs. No third-party runtime dependencies.

import {run,example} from './application-validated-extraction-core.mjs';
console.log(run(example));

Test and transfer

Shape validation and exact evidence checks are separate. Decimal display uses two digits but is not exact monetary arithmetic. Substrings cannot resolve conflicting invoice totals.

Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope