Sub topic 6
Designing the extraction schema
This chapter assumes you can call an LLM API, read and write JSON, and write basic Python classes. It covers the schema, which is the contract between your document and your downstream code. A good schema makes the prompt, validation and evaluation steps easier. A poor one causes errors that no prompt can fix.
What a schema is for
An extraction schema lists the fields you want, their types, and which ones may be missing. You will use it three times: in the prompt (so the model knows what to return), in validation (so you can reject bad output), and in evaluation (so you can score each field).
Start from the decision your code must make, not from the document. If you only need the vendor and the total to route a payment, do not extract 30 fields. Each extra field adds cost and a chance of error.
Make missing values explicit
Documents often leave out information. If every field is required, the model has to invent a value to satisfy the schema. Mark a field optional when the document may not contain it, and tell the model to return null in that case.
from typing import Optional
from pydantic import BaseModel, Field
class Invoice(BaseModel):
vendor: str = Field(description="Name of the company issuing the invoice")
invoice_number: str
invoice_date: Optional[str] = Field(default=None, description="ISO 8601 date, or null if absent")
currency: Optional[str] = Field(default=None, description="Three-letter currency code, or null if absent")
total: Optional[float] = Field(default=None, description="Grand total as a number, or null if absent")Here vendor and invoice_number are required because an invoice without them is not usable. The other three are optional. A null is a correct answer. A guessed date is not.
Choose types that remove ambiguity
Pick types and formats that leave one valid way to write each value.
| Field | Ambiguous choice | Better choice |
|---|---|---|
| Date | `"3/4/25"` as free text | ISO 8601 string, `"2025-04-03"` |
| Amount | `"$1,200.50"` as text | Number `1200.50` plus a separate currency field |
| Currency | `"dollars"` | Three-letter code, `"USD"` |
| Status | Free text | A fixed set of allowed values |
When a field has a small set of valid answers, use an enumeration so validation can reject anything else.
from enum import Enum
class PaymentStatus(str, Enum):
PAID = "paid"
UNPAID = "unpaid"
UNKNOWN = "unknown"Write field descriptions
The description text on each field travels with the schema into the prompt. State the format, the unit, and what to do when the value is absent. "Grand total as a number, or null if absent" tells the model to skip subtotals and avoid currency symbols.
Handle ambiguity and repeated data
Some documents hold several candidates for one field, such as an invoice date and a due date. Add both as separate fields rather than hoping the model picks the right one. For repeated items such as line items, use a list of nested models, so each row has its own typed fields.
Common errors
- Making every field required, which pushes the model to hallucinate.
- Using a bare
strfor dates and amounts, which gives you inconsistent formats. - Packing several facts into one field, such as
"Acme Corp, 12 Main St". - Adding fields you never use.
Exercise
A contract has an "effective date" that is missing from some documents. Should the field be required or optional, and what should the model return when it is absent?
Show answer
Optional, with a default of None. The model should return null when the date is absent. A required field would push the model to invent a date to pass validation.