Output Contract

Especificación de formato

Escrito por Darjus Kazakevic · revisado el

“Return JSON” is not a specification.

A usable reply has two separate problems. The first is syntax: can code parse it? Constrained decoding (the provider only lets the model write tokens that fit a schema) solves that, so use it where it exists. The second is meaning. Is a date an ISO string or "last Tuesday"? Does a missing value come back as null, an empty string or an apology?

An output contract answers the meaning questions in the prompt. It lists each field with its type and meaning, the allowed values, the rule for missing values, and one filled-in example. Write it even when a schema is enforced. A valid shape full of guessed values is still wrong.

En esencia — Describe each field, its allowed values and what to send when a value is missing, then show one filled-in example.

Cuándo recurrir a él

Recurre a él cuando

  • Code parses the reply, not a person
  • Field names or units come back slightly different between calls
  • Constrained decoding is on and the values are still wrong

Reconsidéralo cuando

  • The reply is prose for a person; a contract makes it stiff
  • The shape really varies per request: describe rules, not one schema
  • A one-field answer that one sentence of instruction covers

The parts

The field list
Each field with its type, a short meaning, and its allowed values when the set is closed. "severity: low | medium | high" is a contract. "severity: string" lets the model choose.
The missing value
What to send when the answer is not there. Each field needs one, and your parser must accept it. If you say nothing, the only way to fill the shape is to invent a value.
The instance
One filled-in example of the whole reply. It settles casing, nesting and array contents at once, in less space than prose. Use realistic values, never "string" or "foo".

Reglas de diseño

Give every field a missing-value rule
Say what each field holds when the input does not say: null, [] or a reserved word. Without it, the model fills the gap with something plausible, because that is the only reply that fits your shape.
Type every field and close every set you can
An enum of five values removes a whole class of drift. Where the set is open, name the format and the unit instead. Most "the format changed" bugs are a field that never had one.
Show one filled-in instance
The schema says what the fields are. The instance shows what they look like. It is the cheapest way to remove ambiguity from the prompt.
Ask for the object and nothing else
No text before it, no markdown fence, no "Here is your JSON". Say so, and where your provider allows it, start the reply with "{" yourself (Response Prefill).

Errores habituales

  • Asking for JSON without naming fields, and getting new keys on each call
  • A markdown fence around the object, removed later with a fragile regex
  • Contract and parser drift apart, and production finds out first