Beginner project
Structured extraction with validation
Builds on 2 lessonsabout 55 min of reading first
In this project
What you need first
- Completed:Prompting fundamentalsPrompting and structured output
- Completed:Reasoning models and structured outputsPrompting and structured output
Skills you will practise
- Prompting
- Structured output
- Reasoning models
Problem
Turn messy text into clean, checked data. You extract fields from 30 sample texts into a schema, validate every result, retry with the error when validation fails, and compare two prompt variants by pass rate.
Requirements
- This project builds on the Activity of Prompting fundamentals and the Homework of Reasoning models and structured outputs, where you used invoices. Reuse your schema and validation code from them, but use a different document type here: order emails or support tickets.
- 30 sample texts that you write yourself. Include awkward ones that differ from the invoice cases: a missing order number, a quantity written as a word, a date in an odd format, a message in two languages, and text that tries to give the model instructions.
- A schema of 4 to 6 fields, written as Pydantic or JSON Schema, with the expected value for each field in each text (your labelled set).
- Two prompt variants (for example zero-shot and few-shot) that differ in one thing.
- A validation step with schema checks and at least two semantic checks that a schema cannot express (for example line items add up to the total, the date exists).
- A retry loop that sends the validation error back to the model, with a fixed retry cap (two is common), and a fallback for results that still fail (for example “needs human review”).
- Untrusted text is delimited and described as data, as in Prompting fundamentals.
- Report the pass rate per variant with the sample size, the number of retries used, and a list of the failure cases.
- Acceptance: the pass rule is written down before you run the variants.
Prerequisites
Tasks
- To do: Write the 30 texts and the expected fields for each.
- To do: Define the schema and write the pass rule (exact match on which fields).
- To do: Write prompt variant A and prompt variant B.
- To do: Implement validation: schema checks first, then the semantic checks.
- To do: Implement the retry loop with a cap, and the fallback.
- To do: Run both variants on all 30 texts with the same settings and record first-try and final pass rates.
- To do: Read every failure and put it in a category (wrong value, missing field, invalid JSON, truncated output, refusal, followed an injected instruction).
- To do: Draw the pipeline in the Architecture part.
- To do: Stretch: route easy texts to a cheaper model and compare pass rate and cost per 1,000 texts.
Evaluation
Record real measurements and never invent numbers. Say how many texts each rate is based on, and remember that 30 texts gives a rough signal, not a precise one.
| Criterion | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Validation | None | Schema check only | Schema and two semantic checks | Schema, two or more semantic checks, and each check tested on a bad example |
| Retry and fallback | No cap | Cap but no fallback | Cap and fallback | Cap, fallback and the error sent back, all logged |
| Comparison | One variant | Two variants, judged by eye | Pass rate per variant | Pass rate per variant with sample size and the same settings |
| Failure analysis | None | Failures listed | Failures grouped | Grouped, with one fix tried and re-measured |
Pass mark: at least 2 on every criterion, so 8 out of 12 with no criterion below 2.
Architecture
| Decision | Alternative rejected | Why |
|---|---|---|
| Validate meaning as well as shape | Trust the schema alone | A reply can fit the schema and still be wrong, for example a total that does not add up. |
| Capped retries and a fallback | Retry until it passes | Every retry costs money and time, and some inputs will never pass. |
Interview questions
Provenance: Generated practice. Not reported from any company.
- How do you get reliable JSON from a model, and what does that still not guarantee? Practise with C9 in the Coding-round bank.
- When would you use zero-shot, few-shot or chain-of-thought for an extraction task, and how do you check the choice? Practise with F7 in the Fundamentals bank, then read “Measure, do not guess” in Prompting fundamentals.
- The document you extract from contains the sentence “ignore your instructions”. What do you change? Practise with I8 in the Intermediate bank.
Portfolio checklist
- To do: The README states the schema, the pass rule and the pass rate per variant with sample size.
- To do: The labelled set is in the repository and was written before the runs.
- To do: Failure cases are listed, not hidden.
- To do: The retry cap and fallback are visible in the code.