Let’s Get Certified
Module 10 / 12 · Prompt engineering and structured output

Validating extractions and reviewing changes

Recover from extraction failures, schedule batches, and verify work in a fresh context.

Process a folder of receipts

An expense service extracts printed totals and line items. Several receipts have faded subtotals. The intake team needs both a fast interactive path and an overnight reconciliation path.

4.4 · Make validation feedback specific

Preserve the stated total and independently calculate a total from the extracted lines, discounts, and tax. Record a conflict when the two disagree. On a recoverable failure, return the source, prior extraction, and the precise failing check, for example “printed total 38.50, extracted lines plus tax 36.50”. Ask for evidence for the disputed field. Bound retries. If the receipt refers to a missing attachment, record the missing source or seek human review. Another pass on the same pixels cannot supply the attachment.

For code review, attach a detected_pattern value to each finding. Join dismissals to these values. A spike in dismissed timezone warnings can point to a missing exception in the rubric. Check changes against labeled examples before rolling them out.

Decide whether another extraction can help

A receipt prints items of 10.00 and 6.00, a 2.00 handling fee and a stated total of 18.00. The first extraction omits the fee and returns a calculated total of 16.00. A second receipt says “see attachment for tax” and supplies no attachment. Are both cases retryable?

Prepare the retry payload

For the first, pass the original source, failed record and arithmetic discrepancy into a bounded retry. Ask the extractor to identify the printed fee and preserve the independently computed total. Keep the original attempt in the audit. A new result passes only if the values and their cited locations reconcile.

For the second, return a missing-source status, preserve the other verified fields and request the attachment or route to review. Repeated inference cannot establish the tax value. Record the failure pattern separately from the outcome so the team can distinguish a systematic layout problem from genuinely missing evidence.

Video background · freeCodeCamp at 10:38:02 · Passing validation errors into a bounded retry

4.5 · Budget for queue time and incomplete work

Use synchronous requests when a person or a merge is waiting. Independent offline documents can use Message Batches at discounted rates. Plan for a processing window of up to 24 hours, plus ingestion, retries, and delivery. With a 30-hour delivery allowance, collecting work for six hours consumes all the remaining slack. A four-hour collection interval leaves two hours for other stages. Expiration and per-item errors still need a recovery path.

Assign a stable custom_id to each request. Poll until the batch ends and reconcile results by ID because output order can differ. Preserve successes, inspect errored or expired items, and resubmit only affected inputs after correction. Chunk an oversized record at meaningful boundaries. A batch request cannot pause to run a client tool and accept the result mid-request. Use separate stages for dependent turns. Pilot the prompt on a labeled sample before queuing thousands of items.

Plan the retry before the overnight run

An archive delivers records continuously. Its target is 32 hours from arrival to a usable result. Allow 24 hours for batch processing, three hours for downstream delivery and one hour of contingency. After a pilot, one batch returns IDs K3 and K1 as successes, K2 as too long and K4 as expired. Choose a collection interval and recovery policy.

Balance the clock and reconcile the manifest

The collection interval can be at most four hours under those planning assumptions. A record arriving just after a submission waits nearly the whole interval. The processing window includes the possibility of expiry. A four-hour cadence does not guarantee every item succeeds or reserve another 24 hours for a second batch. Plan an alternate path for items approaching the deadline.

Join outputs to the submitted manifest by custom ID. Persist K1 and K3. Split K2 at meaningful boundaries and preserve its parent record. Reconsider K4 against its remaining deadline. The November guide uses a 50% discount for batch model usage. At an illustrative $40 of equivalent synchronous usage, eligible batch work would cost $20 before separate tool, storage or recovery costs. A blocking merge check or mid-turn tool exchange needs a different execution path.

Video background · freeCodeCamp at 11:25:41 · Correlating asynchronous batch results by request ID

4.6 · Separate authorship from verification

Give a second instance the artifact, source, and rubric in a clean context. For a large change, examine each file for local mistakes, then check interfaces across files, including producers and consumers of extracted records. Preserve the evidence for each finding, collect confidence estimates in a verification pass, and calibrate them before routing review. Agreement between two reviewers who both saw the same speculative rationale is weak corroboration.

Find a bug that neither per-file reviewer saw

The producer emits duration_ms=1200. The consumer treats 1200 as seconds. Both files parse, and their local tests use fixtures created by the same author. A self-review restates the original assumption. What should the next review instance receive?

Run an independent integration pass

Send a fresh instance both interfaces, representative payloads, the external unit contract, and the relevant tests. Hold back the generator’s speculative rationale. Have local passes report the unit each file assumes with exact source locations. An integration pass should trace the same value from producer through consumer and calculate the expected 1.2 seconds.

Add an independent contract fixture that fails on 1200 seconds. Record confidence with each finding and compare it against labeled reviews before using it to prioritize human attention. Two reviewers agreeing on a unit supplied by the same erroneous fixture still share the same mistake.

Video background · freeCodeCamp at 10:36:25 · Reviewing a generated result in a fresh context

Reconcile an unordered batch locally

This Python 3 fixture represents outcomes after a batch has ended. Predict which records can be delivered, which need a corrected submission, and which must be reconciled before a new request is sent.

manifest = ["r11", "r12", "r13", "r14", "r15"]
results = [
    ("r13", "expired"),
    ("r12", "succeeded"),
    ("r14", "too_long"),
    ("r11", "succeeded"),
]
by_id = dict(results)
for record in manifest:
    status = by_id.get(record, "missing")
    action = {
        "succeeded": "deliver",
        "expired": "check remaining deadline",
        "too_long": "resize before retry",
        "missing": "reconcile source results",
    }[status]
    print(record, action)
Check the output and explain the result
r11 deliver
r12 deliver
r13 check remaining deadline
r14 resize before retry
r15 reconcile source results

Here too_long is the application’s classification of an item error. Remove r12 from the results and rerun. Only its disposition changes. In a production importer, also reject unknown IDs and inconsistent duplicates, persist delivered IDs, and check the complete result stream before declaring an entry missing.

Further reading · Structured outputs · Message Batches API

Knowledge check

Validating extractions and reviewing changes quiz

Answer 9 questions. Check each answer for an explanation.