Esc

Force a strict output contract

How to make a model response reliably machine-parseable: exact fields, no preamble, no trailing summary. For anything that downstream code reads and a person does not.

AI Place Hub 46

When the output feeds a script, you have to tell the model outright not to be conversational, because be helpful, left alone, includes a preamble and a summary that break a parser.

Say what the whole response must be

Include a JSON object is not enough. Write: your entire response must be a single JSON object matching this schema, with no text before or after it. Skip the second half and even a well-behaved model tends to add Here is the JSON you requested, and a naive parser waiting for a brace as the first character fails.

Give the exact schema

Return name, date and amount leaves field names, casing and types to guesswork. Paste an example object, or the schema definition if your language has one. A model copies structure far more reliably than it infers it from a sentence.

Say what happens with missing or uncertain data

Tell it to always return every field and it will invent a plausible value for any field it cannot determine, since an empty field looks like failure to it. State the null behaviour: use null for anything not stated in the input and never invent a value.

Validate on the receiving end anyway

A strict prompt lowers the rate of malformed output and does not remove it. Parse defensively, and treat a parse failure as a retry with the error message attached. Tools like Instructor automate exactly that. Do not trust the contract blindly in production.

The one-line version

If you add only one sentence, add this: respond with only the JSON object, nothing else, no explanation before or after.

Notes from the author

Smaller and local models hold this contract less well than big hosted ones over a long response. For them, keep the schema small and consider one field per call in place of a large nested object.

For information only, not professional advice. You use it at your own risk and we are not liable for any loss or damage. See the disclaimer.

Fork

Did this work for you?

Sign in to flag it

Related