A good extraction schema is a contract: it names every field, gives each one a type, and closes the sets of values that have a fixed list. The model follows the contract when the contract is precise, and invents values when it is vague. Shallow nesting, required keys, enums for closed sets and null for missing values are the habits that keep extraction reliable.
Before you start
Five sample documents of the type you will extract from, and a list of the fields your downstream code needs.
List the fields your code needs
Write down every field the downstream code reads, and nothing else. A field nobody reads is a field the model can get wrong for free.
For each field, write what it means in one sentence, with an example value. If you cannot write the example, the field is not defined well enough to extract.
Set types, enums and nulls
Give each field a type: string for names, number for amounts, integer for counts, boolean for yes or no. Use an enum for any field with a fixed list, such as document type or status.
Type a field as string or null when it can be missing, and require the key to be present. A missing key and a null value are different things, and your code should handle the null.
Test on five documents by hand
Run the schema on five real documents and check every field against the source by hand. Count the wrong values, not just the parse failures: a valid shape with a wrong amount is still a failure.
If one field is wrong more than once, rewrite its description or split it into two fields. Do not fix it by adding a longer prompt.
Practice check: fictional Maple Corner invoices, 5 documents, 6 fields each
The table below is fictional practice material, worked out by hand from the stated extraction runs. It is the expected result for the practice documents, not a run of any API.
| Field | Correct of 5 | Verdict |
|---|---|---|
| invoice_number | 5 of 5 | Pass |
| invoice_date | 5 of 5 | Pass |
| total_amount | 4 of 5 | Fail – one wrong total |
| currency enum | 5 of 5 | Pass |
| vendor_name | 5 of 5 | Pass |
| due_date or null | 4 of 5 | Fail – one invented date |
Twenty-eight of thirty fields are correct. The two failures are a wrong total and an invented due date where the document had none, so the fix is a null for missing dates and a value check on totals.
Expected result. UNTESTED: the counts in the practice table are worked out by hand, not measured by running an extraction. Run your own schema on five real documents, check every field by hand, and count the wrong values.
Limits
- A schema guarantees shape, not correctness: the model can return the right fields with the wrong values.
- Keep nesting shallow: deeply nested schemas are harder for the model to fill correctly.
