Free lesson

Request constrained output locally

Request constrained output locally — Local LLMs and validated JSON. Schema and instruction agree; Save extract.mjs; Retain the candidate. Practical assignment: Lab evidence.

Demo edition. The materials are undergoing editorial review.

Topics covered

  • Schema and instruction agree
  • Save extract.mjs
  • Retain the candidate

Schema and instruction agree

Tell the model to extract only explicit facts and use null for missing values. Provide the same schema in format and the prompt.

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.

Save extract.mjs

Use a downloaded local model. Run this file with Node after saving schema.mjs. Fail on HTTP errors before parsing the model message.

There are two independent JSON layers: response.json parses the transport envelope and JSON.parse parses the model’s content string. An HTTP error can itself contain JSON, so parsing alone is not success. Check response.ok first and preserve the status for diagnostics.

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.

import {schema} from './schema.mjs';
import {pathToFileURL} from 'node:url';
export async function extract(source='Clay workshop, 2027-03-12, six seats.'){const response=await fetch('http://127.0.0.1:11434/api/chat',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({model:'llama3.2:1b',stream:false,format:schema,options:{temperature:0},messages:[{role:'user',content:'Extract explicit facts; missing = null. Schema: '+JSON.stringify(schema)+' Source: '+source}]})});
if(!response.ok) throw Error('HTTP '+response.status);
const envelope=await response.json();
const candidate=JSON.parse(envelope.message.content);
return candidate;}
if(process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href)console.log(await extract());

Retain the candidate

The output is still a candidate. Preserve source, raw message and model tag together. Constrained decoding does not establish factual correctness.

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.

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