Sub topic 7
How Prompted Extraction Fails
This chapter assumes you know basic Python, JSON, language-model concepts, and introductory precision, recall, and F1. The focus is diagnosis: identifying whether a prompted extractor failed because of the prompt, the input, the model, or the validation layer.
Prompting can produce useful structured data without training a new model. However, a prompt is a request, not a guarantee. A model may invent missing values, select the wrong span, change a label, or return invalid JSON. Treat every response as an untrusted prediction.
Common failure modes
Hallucinated values
A model may fill a field that the text does not state. For example, if an invoice says Total: 120, the model might infer that the currency is USD from the vendor or document context.
The schema should define a null rule clearly:
- Return
nullwhen the field is not stated. - Do not infer values from common business practices.
- Preserve the source text when normalization could change meaning.
Even with these instructions, measure hallucinations on examples where fields are deliberately absent.
Boundary and normalization errors
Extraction can fail even when the model recognizes the right concept. It might return New York instead of New York City, or include surrounding words such as in Paris.
For dates, amounts, and names, decide whether the expected answer is:
- The exact text span, such as
$120.50. - A normalized value, such as
120.50. - Both the original span and the normalized value.
A span-level evaluator usually requires exact start and end boundaries. Therefore, a semantically acceptable answer can still count as incorrect.
Wrong labels and relationships
A model may extract the correct text but assign the wrong field. In Acme purchased 20 servers, it could label Acme as a customer when the schema calls it a vendor.
Relation extraction adds another failure: the model may identify both entities but connect them in the wrong direction. Store relationships in a canonical form, such as (buyer, purchased, item), and test direction separately.
Missing and repeated entities
Models often omit low-salience entities, especially when many similar items occur. They may also merge two mentions into one or return the same entity twice.
Tell the model whether to return every occurrence or one canonical value. If the answer is “every occurrence,” include an array in the schema and define how duplicates are handled.
Format and type errors
A response may contain valid JSON wrapped in commentary, use "120.50" where a number is required, or omit a required key. Parsing and schema validation must happen before downstream processing.
import json
REQUIRED = {"vendor", "total", "currency"}
def validate(raw):
try:
value = json.loads(raw)
except json.JSONDecodeError:
return False, "invalid JSON"
if not isinstance(value, dict) or set(value) != REQUIRED:
return False, "wrong keys"
if value["total"] is not None and not isinstance(
value["total"], (int, float)
):
return False, "total is not numeric"
return True, valueThe validator should reject unsafe output rather than silently repairing it. A repair step can hide a recurring prompt failure and make evaluation misleading.
Diagnose failures with a labeled error log
Run the same fixed evaluation set after each prompt change. Record the input, expected output, actual output, and one primary error category.
| Error category | Typical symptom | Useful response |
|---|---|---|
| Hallucination | Value is absent from the text | Strengthen the null rule and add negative examples |
| Boundary | Correct concept, wrong span | Define span rules and show exact examples |
| Label | Correct text, wrong field | Clarify field definitions and contrasts |
| Omission | Expected item is missing | Specify cardinality and add crowded examples |
| Format | JSON or type validation fails | Use constrained output and reject invalid results |
A practical debugging loop
Change one factor at a time. If you change the instructions, examples, schema, and model together, you cannot tell which change helped. After each change, compare exact-match span F1, field-level accuracy, validation failure rate, and hallucination rate.
A prompt is suitable for production only when its errors are understood and its invalid outputs have a safe handling path. If failures remain concentrated in a stable domain, fine-tuning or a hybrid fallback may be more predictable than adding more prose to the prompt.
Exercise
An extractor returns the correct vendor and total but invents a currency for invoices that omit one. Which failure category is this, and what should you change first?
Show answer
It is a hallucination. Strengthen the rule to return null for unstated fields, add examples with missing currencies, and measure the result on held-out invoices.