Sub topic 3
Building a Gold Dataset
Assume you know basic Python, JSON, language models, and introductory precision, recall, and F1. This chapter shows how to create a gold dataset, a collection of text examples with carefully checked answers. The dataset becomes the reference for evaluating prompts and trained models.
Start with a precise extraction contract
A gold dataset is useful only when annotators can agree on what counts as correct. Write the extraction schema before labeling text. A schema lists each field, its type, and the rule for when it is present.
For example, an invoice extractor might use this schema:
| Field | Type | Annotation rule |
|---|---|---|
| `vendor` | string or null | Copy the company name stated as the seller |
| `total` | number or null | Copy the final amount due, without a currency symbol |
| `currency` | string or null | Use the currency explicitly attached to the total |
Define boundary rules for spans. A span is the exact range of characters selected from the text. Decide whether a title, punctuation, or surrounding spaces belong in the span. For example, annotate Acme Ltd. rather than only Acme if the complete vendor name appears in the invoice.
Also define ambiguity rules:
- Use
nullwhen the text does not state a field. - Do not infer a value from outside knowledge.
- If two totals appear, label the one described as the final amount due.
- Preserve the source spelling unless normalization is part of the schema.
- Mark an example for review when the rule cannot resolve it.
Sample and label the data
Collect text that resembles expected production input. Include different document layouts, writing styles, languages, lengths, and levels of noise. Do not select only easy examples. If production contains scanned invoices, include OCR errors in the dataset.
A practical workflow is:
- Remove secrets and unnecessary personal information.
- Sample documents across important sources and time periods.
- Write the schema and labeling guidelines.
- Have one annotator label an initial batch.
- Review disagreements and revise the guidelines.
- Have at least two annotators label an overlap batch independently.
- Resolve disagreements with an adjudicator.
- Store the final decisions as gold labels.
Keep the original text unchanged. Store annotations separately so that character offsets remain reproducible.
import json
record = {
"id": "invoice-001",
"text": "Invoice from Acme Ltd. Total due: EUR 120.50.",
"fields": {
"vendor": "Acme Ltd.",
"total": 120.50,
"currency": "EUR",
},
"spans": [
{"start": 14, "end": 23, "label": "VENDOR"},
{"start": 36, "end": 44, "label": "CURRENCY"},
{"start": 45, "end": 51, "label": "TOTAL"},
],
}
print(json.dumps(record, indent=2))The spans list supports entity-level evaluation. The fields object supports key-value evaluation. Keep both only when the project needs both views. For a field such as total, verify that the numeric value matches the selected text and the schema's normalization rule.
Split the dataset without leakage
Create separate training, validation, and test sets when training a model. The test set is locked until final evaluation. A common starting point is 70 percent training, 15 percent validation, and 15 percent test, but the proportions depend on dataset size.
Prevent leakage. Near-duplicate invoices from the same template should remain in one split. Otherwise, a model may memorize the template instead of learning extraction behavior. Group documents by customer, source system, template, or conversation when those groups share information.
Check quality before using the labels
Measure agreement on the overlap batch. For span extraction, compare exact spans and labels, not just whether both annotators found an entity somewhere in the sentence. Investigate disagreements by category:
- boundary disagreement, such as
AcmeversusAcme Ltd. - label disagreement, such as
TOTALversusSUBTOTAL - missing-value disagreement
- normalization disagreement
- unclear or contradictory source text
Do not silently discard difficult examples. They reveal weaknesses in the schema and often resemble real production failures. Record the guideline decision that resolves each recurring disagreement, then recheck earlier labels affected by that decision.
Exercise
An invoice says “Subtotal: EUR 90.00” and “Amount due: EUR 108.00.” Which value should the total field contain under the schema in this chapter?
Show answer
It should contain 108.00, because the rule selects the final amount due rather than the subtotal.