Build a local AI feature
Request structured output
Use a JSON schema to constrain the model response, then validate the returned value before the application trusts it.
Asking for “valid JSON” in a prompt is weaker than providing a schema.
Ollama accepts a JSON schema in the format field:
const summarySchema = {
type: 'object',
properties: {
summary: { type: 'string' },
confidence: { enum: ['low', 'medium', 'high'] },
},
required: ['summary', 'confidence'],
additionalProperties: false,
}
Send that schema with stream: false. Put the same output requirements in the prompt so the task and structure agree.
A constrained response is still untrusted input. Parse the JSON, check both fields, enforce the sentence length, and compare factual claims with the source data where possible.
A schema can guarantee shape. It cannot guarantee truth.
Use a low temperature when consistency matters, then test whether the chosen model follows the schema reliably.
Lesson completed