MIVORA Start learning free

Controlling the Output

If a program will consume the answer, you need structure you can rely on — not prose you hope to parse.

Building with AI · Lesson 7 · 11 min read

Your app sends model output straight into a database. One day it returns `Sure! Here is the data:` before the JSON, and your parser crashes in production. A friendly preamble just broke your pipeline. You need output that’s reliably structured — same shape, every time, nothing extra. This lesson is how.

Ask for a schema, forbid the extras

To consume output programmatically, specify the exact structure (e.g. a JSON object with named, typed keys) AND explicitly forbid anything around it (“return only the JSON, no preamble”). Many APIs also offer a structured-output / JSON mode that constrains the model to valid JSON. The combination — a named schema plus “nothing else” — is what makes the output safe to parse and easy to validate.

Worked example
Weak: “List the action items.” → prose, sometimes with a greeting.
Strong: “Return ONLY a JSON object: {\"items\": string[], \"owner\": string}. No text outside the JSON.” → a shape your code can parse and check every time.

Constraints make output predictable

Beyond shape, pin the constraints: allowed values (an enum like "low"|"medium"|"high"), lengths, required vs optional fields, units. The tighter the contract, the less your downstream code has to defend against. Then validate the output against the schema and reject or retry on a miss — never trust it blindly.

An everyday analogy

It’s the difference between a handwritten note and a fill-in-the-blank form. A note might say anything, anywhere; a form has labeled boxes you can read at a glance and check for completeness. Structured output is handing the model the form and saying “fill only these boxes.”

Worked example
Hardening a tagging feature:
1. Ask for {"tags": string[], "priority": "low"|"medium"|"high"}.
2. Add “return only JSON, no commentary.”
3. In code, parse and validate: is priority one of the three? are tags an array?
4. On a miss, retry or flag — so a malformed reply never reaches the database. Reliable by construction.

This is the reading. The interactive version — active-recall quiz, a hands-on experiment you run in your own AI, and an earned mastery check — is free in the app.

Start this lesson free →