Sub topic 5
Extracting with Zero-Shot and Few-Shot Prompts
This chapter assumes Basic Python, familiarity with JSON, a basic idea of language models, and introductory precision, recall, and F1. The goal is to use a general-purpose language model for information extraction without training a new model.
Prompting as an extraction method
Information extraction turns free text into a defined structure. For example, an invoice extractor might return a vendor, total, and currency.
A zero-shot prompt gives the model instructions but no completed examples. It asks the model to apply the task description directly.
A few-shot prompt includes a small number of input-and-output examples. The examples demonstrate the expected labels, boundaries, and output format.
A prompt should define three things:
- The fields or entity types to extract.
- What to do when a value is absent.
- The exact output format.
For example, use null for an unstated value rather than asking the model to infer it. This makes missing information different from a value that was found.
Building a few-shot prompt
Examples should use the same schema as the expected production output. They should also cover realistic variation, such as different wording and missing fields.
PROMPT = """Extract invoice fields from the text below.
Return only one JSON object with these keys:
- "vendor": a string or null
- "total": a number or null
- "currency": a string or null
Use null when a field is not stated. Do not guess.
Example 1
Text: Invoice from Acme Supplies. Amount due: EUR 120.50.
JSON: {"vendor": "Acme Supplies", "total": 120.50, "currency": "EUR"}
Example 2
Text: Bill to Northwind for 75 dollars.
JSON: {"vendor": "Northwind", "total": 75, "currency": "USD"}
Now extract from this text:
Text: {text}
JSON:"""
def build_prompt(text):
return PROMPT.replace("{text}", text)
print(build_prompt("Receipt issued by Contoso. Total: 42 GBP."))The examples teach the model that “dollars” maps to USD, that the total should be numeric, and that the output should contain only the requested keys. They do not guarantee that the model will follow those rules every time.
Choosing useful examples
Few-shot examples consume input tokens, so select them deliberately. An example is useful when it represents a case the model might otherwise misunderstand.
| Example type | What it teaches |
|---|---|
| Typical complete record | The normal schema and output shape |
| Missing field | The null rule |
| Different wording | Which phrases express the same field |
| Ambiguous value | When to avoid guessing |
| Boundary case | How much text belongs in an extracted span |
Place examples that are most similar to the current input near the prompt or select them dynamically. Dynamic example selection can improve accuracy, but it adds retrieval logic and another source of failure.
Common prompting errors
A vague instruction such as “extract the important information” leaves the schema undefined. Different model responses may then contain different fields.
A prompt can also fail when:
- An example contradicts the written instructions.
- The model copies a value from an example instead of the input.
- The requested field is not present, but the model guesses a plausible value.
- Entity boundaries are unclear, such as whether to extract
AcmeorAcme Supplies. - The model adds commentary around the JSON.
- The input contains instructions that attempt to change the extraction task.
Treat the model response as untrusted data. Parse it, check its keys and types, and send invalid results to a retry or review path. Prompting improves the model's behavior, but validation determines what your application accepts.
Exercise
When should a few-shot example use a null field instead of omitting that field from the JSON object?
Show answer
It should use a null field when the schema requires the field but the input does not state a value. Consistent keys make downstream validation and evaluation predictable.