Module 1 · Lesson 4 of 6

Output your code can check

Three checks before your code acts

A reply meant for code goes through three checks: does it parse, does it match the schema, and are its facts allowed.

Scene 1 of 5 · The model replies

Asked to sort an email, the model sends back text meant to hold three named fields.

Model replyModel replyParseParseShape checkShape checkFact checkFact checkYour code actsYour code acts
  • Model reply — healthy
  • Parse — healthy
  • Shape check — healthy
  • Fact check — healthy
  • Your code acts — healthy
One reply through the checks
StepThis reply
The reply (highlighted){"intent":"refund","order":"A1042","urgent":false}
Parse the textPass
Check the shapePass
Check the factsPass
  1. The model replies. Asked to sort an email, the model sends back text meant to hold three named fields.
  2. Parse the text. Code turns the text into data, and anything that is not valid JSON stops right here.
  3. Check the shape. Each field must be there, have the right type and use a value from the allowed list.
  4. Check the facts. Order A1042 belongs to this customer, which your records know and no schema can.
  5. Only then act. With all three passed, your code opens the refund; any failure sends it back or to a person.
  6. Now you try. Pick a reply the model sends, and see which check stops it. For example: Stopped at the parse check: the text is not valid JSON. Text around the JSON breaks parsing. Your code asks once more, quoting the error.

Scene 1 of 5: The model replies. Asked to sort an email, the model sends back text meant to hold three named fields.

Key points

  1. Parse first: a sentence wrapped around the JSON already fails.
  2. Then check every field, its type and its allowed values.
  3. Then check facts only your own data knows, such as who owns an order.
  4. Act only on a reply that passes all three checks.

Often your code needs data from the model, not prose: a category, an order number, a yes or no. So the asks for with named fields, and lists the values each field may take.

A model can still return broken JSON, wrap it in a sentence, leave a field out or invent a value. Some APIs can force the reply to match a you give. That fixes most format problems, but a reply cut off by the limit is still incomplete.

A reply in the right shape can still be wrong, such as a real-looking order number that belongs to someone else. So code checks the values and the permissions before it acts.

When a check fails, ask once more with the error included. If that fails too, fall back, such as passing the email to a person.

Example. Illustrative: a support inbox has the model sort each email into refund, exchange or question, with its order number. If 2 replies in 100 fail a check, those 2 emails go to a person, not to the refund system.

In system design: Treat model output like user input: parse it, check it against a schema, and check permissions before any code acts on it.

Check yourself

The model returns valid JSON in the right shape, with an order number. What must your code still check?