Sub topic 3
Fine-tuning generative models for extraction
This chapter assumes you know basic Python, can read JSON, understand precision, recall and F1, and already have a gold-labeled evaluation set and a prompt baseline.
Fine-tuning generative models for extraction
Fine-tuning continues training a pretrained language model on your own input-output pairs. For extraction, each pair is a text and the JSON object it should produce. Enough examples teach the model your schema and labeling rules without a long prompt.
Prepare the data as chat-format JSON Lines, one example per line:
import json
examples = [
{"text": "Invoice from Acme, total 120.50 EUR.",
"output": {"vendor": "Acme", "total": 120.5, "currency": "EUR"}},
{"text": "Bill from Globex for 80 USD, due soon.",
"output": {"vendor": "Globex", "total": 80, "currency": "USD"}},
]
with open("train.jsonl", "w", encoding="utf-8") as f:
for ex in examples:
record = {"messages": [
{"role": "user",
"content": "Extract vendor, total, currency as JSON.\nText: " + ex["text"]},
{"role": "assistant", "content": json.dumps(ex["output"])},
]}
f.write(json.dumps(record) + "\n")
print("wrote", len(examples), "examples")Training labels must match your gold annotation rules. Inconsistent labels in the training data are the most common reason a fine-tune underperforms.
Analyzing errors by category
Count errors by type instead of relying on one F1 score. Typical categories:
| Error category | Example | Usual fix |
|---|---|---|
| Missed field | total omitted | Prompt change or more examples |
| Wrong value | currency "$" instead of "USD" | Normalization rule and training data |
| Hallucinated field | vendor invented | Add examples with null values |
| Format failure | prose wrapped around the JSON | Validation and retry |
If format failures dominate, fix the prompt or add a validator. If wrong values dominate on clear text, add training data.
Comparing cost, latency and maintenance
Prompting needs no training, but every call sends the full prompt, which raises cost and latency. A fine-tuned model needs labeled data and training runs, but uses a shorter prompt. Schema changes also differ: a prompt edit takes minutes, while a fine-tune must be retrained and re-evaluated.
Combining both approaches
A common hybrid uses a large prompted model to label unlabeled text, has people check a sample, and trains a smaller model on the result. Outputs that fail validation go back to the large model.
Choosing between prompting and fine-tuning
- Start with prompting when the schema is still changing or labeled data is scarce.
- Fine-tune when the schema is stable, you have hundreds of verified examples, and call volume makes per-call cost matter.
- Use a hybrid when volume is high but some inputs are hard.
Exercise
A prompted extractor has precision 0.91 and recall 0.62. Most misses are the total on invoices written as "EUR 1.200,50". Which should you change first: the prompt, the data or the model?
Show answer
Change the data first. Add labeled examples that show this number format, to the prompt or the training set, then re-measure recall on held-out gold data. Recall should rise on that subset before you consider a larger change.