Output format control
Prose is forgiving and parsers are not. Any prompt whose output feeds code needs the format specified, demonstrated, and validated, because a model that returns the right answer in the wrong shape is a failure to the caller.
Method
- Use structured output features where available. Schema-enforced generation is far more reliable than asking politely for JSON, and it should be the default when supported.
- Show the exact shape. One example of the output, complete and well-formed, teaches format better than a description of it.
- Specify what goes in each field. Type, allowed values, and what to do when the value is unknown, since an unspecified null is filled with invention.
- Forbid the wrapper explicitly. Models add preamble, explanation, and code fences by default; saying return only the object is necessary and works.
- Validate and retry on failure. Parse, validate against the schema, and retry with the specific error, which fixes the vast majority of malformed outputs (see mcp-error-handling).
- Keep the schema small. Deeply nested structures with many optional fields produce more errors, so ask for what you need.
- Handle the refusal case in the schema. A field for cannot answer is better than forcing a fabricated value into a required field.
Boundaries
Format control governs shape, not correctness: a schema-valid answer can be entirely wrong. Strict formats can suppress useful nuance, so human-facing output usually should not be over-structured. Schema support and behaviour vary between providers (see prompt-testing).