Sub topic 2
Defining the Schema and Labeling Rules
Assume you know basic Python, JSON, language models, and introductory precision, recall, and F1. This chapter focuses on the contract that an extraction system must follow before you prompt or train a model.
Start with a precise extraction schema
An extraction schema defines what the system should find and how it should return each result. It is the interface between source text, annotations, model output, and evaluation.
For an invoice task, the schema might contain:
vendor: the supplier name as written in the documenttotal: the final amount duecurrency: the currency code or symbolinvoice_id: the invoice identifier
Do not begin with vague instructions such as “extract useful invoice information.” List every field, its type, whether it is required, and what counts as evidence.
| Field | Type | Required | Evidence rule |
|---|---|---|---|
| `vendor` | string or null | No | Supplier named on the invoice |
| `total` | number or null | No | Final amount explicitly labeled as due |
| `currency` | string or null | No | Currency shown beside the total |
| `invoice_id` | string or null | No | Identifier labeled invoice number or equivalent |
A field should be null when the text does not state it. The extractor must not infer a currency from the vendor's country or calculate a total unless the schema explicitly allows those operations.
Separate extraction from normalization
Extraction copies evidence from the text. Normalization changes that evidence into a standard form. Keep these operations separate so that evaluation can distinguish a missed value from a formatting decision.
For example, the text might contain €1,250.00. Extraction could return:
{
"total": "1,250.00",
"currency": "EUR"
}A later normalization step could convert total to the number 1250.0. If the model directly returns a number, preserve the original text separately when auditability matters.
Write labeling rules before annotation
Labeling rules explain how humans create the gold dataset. Gold data is trusted reference data used to train or evaluate a system. The rules should resolve boundary, ambiguity, and overlap decisions before annotators encounter them.
For named entities, specify the exact character or token span:
- Include all words that belong to the entity.
- Exclude labels, punctuation, and surrounding whitespace unless the task requires them.
- Label
Acme Corporationas oneVENDOR, not as separate words. - Label
INV-1042asINVOICE_ID. - Do not label a generic word such as “invoice” as an identifier.
- If the same entity appears twice, label both mentions when the task is mention extraction.
For key-value fields, define whether the label covers only the value or the complete key-value pair. Usually, label only the value. In Total due: €1,250.00, the amount is the value span and Total due: is contextual evidence.
Handle ambiguity explicitly
A useful rulebook states what to do when evidence conflicts or is incomplete:
- If two totals appear, label the one explicitly marked as the final amount due.
- If a field has several valid values, return a list or define a priority rule.
- If a value is partly unreadable, mark it as uncertain rather than guessing.
- If a field is absent, use
null, not an empty string or invented value. - If entities overlap, define whether nested spans are allowed.
The same rules must apply to human labels, prompted outputs, and fine-tuned model predictions.
Represent the contract in data
A written schema can be translated into JSON-like documentation or validation code. Keep field names stable. Changing total to amount_due halfway through annotation creates incompatible data.
Document at least:
- field names and types
- allowed labels
- required versus optional fields
- null behavior
- span boundary rules
- normalization rules
- handling of conflicting or repeated values
Before collecting many examples, have two annotators label the same small sample. Compare their spans and decisions. Revise the rules where disagreements occur, then freeze the version used for the gold dataset.
Exercise
In the text “Subtotal: €90.00. Total due: €108.00,” which amount should a schema for the final invoice total label, and why?
Show answer
It should label €108.00 because the rule selects the amount explicitly marked as the final amount due, not the subtotal.