Free lesson

Define the extraction contract

Define the extraction contract — Local LLMs and validated JSON. Missing is explicit; Write schema.mjs; Create boundary cases. Practical assignment: Lab evidence.

Demo edition. The materials are undergoing editorial review.

Topics covered

  • Missing is explicit
  • Write schema.mjs
  • Create boundary cases

Missing is explicit

Use null for an unspecified date or capacity. Zero seats means a stated zero, not missing information. The workshop title remains required.

The training record describes one workshop with a title, a date and an optional capacity. Decide the meaning of each field before asking the model to fill it. Missing information must remain missing; a syntactically correct number can still be invented. Keep the original source alongside each candidate so a reviewer can verify the extracted value without trusting the model’s explanation.

Write schema.mjs

Save the schema below. Required fields must appear even when null is allowed. Additional properties are rejected to detect accidental invented fields.

The schema makes a deliberate policy choice: known capacity must be a positive integer, while unknown capacity is null. Consequently zero is rejected rather than interpreted as unknown. Document that rule beside the fixture so the reviewer can distinguish missing data from an invalid stated capacity.

Use a separate directory and save JavaScript examples as .mjs files before running them with Node. Reuse the schema and sample input exactly during a comparison. The JSON received from HTTP is an envelope; the model’s message is a second string that must be parsed independently. Catch both parsing failures and validation failures, and preserve their different error categories in your report.

export const schema = {type:'object',properties:{title:{type:'string'},date:{type:['string','null']},seats:{type:['integer','null'],minimum:1}},required:['title','date','seats'],additionalProperties:false};

Create boundary cases

Write three sources: a complete record, a missing date and a contradictory capacity. Record expected review decisions before calling a model.

A passing schema means only that the candidate has the allowed structure. Acceptance also requires checking values against the source and applying the project’s business rules. Keep accepted, rejected and review-needed records separate. Do not silently repair dates, currencies or names by guessing. A failed automated attempt can be a useful review item when the original text and error remain available.

// fixtures.mjs
// Fictional fixtures
export const cases=[{text:'Clay workshop, 2027-03-12, six seats.',expected:{title:'Clay workshop',date:'2027-03-12',seats:6}},{text:'Clay workshop, date not set.',expected:{title:'Clay workshop',date:null,seats:null}},{text:'Six seats. Correction: eight seats; approval unclear.',review:true}];

Practical assignment

Lab evidence

Continue to learning

Course outline

  1. Define the extraction contract
  2. Request constrained output locally
  3. Validate with a real schema validator
  4. Check meaning after structure
  5. Bound retries and preserve failures
  6. Deliver a reproducible extraction project