Skip to main content

How to Get AI to Output in Your Format

Guide · about 6 min · refs checked 2026-11-03 (reference links and dates only)

When a program has to consume AI output, "please output JSON" often isn't enough. Three levels, from prompt-level constraints to structured outputs.

How to
Level one: a format spec plus one complete sample in the prompt. Enough for most cases, but missing fields and stray commentary still happen. Level two: use the API's structured-output / tool-calling capability — server-side constrained decoding guarantees valid format, far more reliable than asking nicely. Define schemas with only the fields you need and tighten types: fewer fields, smaller failure surface. Validate on the receiving end regardless of level: on schema-validation failure, retry or degrade — never feed downstream code unchecked. Format constraints and task quality pull against each other: forcing a format can degrade content, so evaluate the two separately. Don't let format tricks substitute for content evaluation: valid output is not correct output.

Example and interpretation

A teaching scenario, not a measured or executed site test.

For output such as an amount of 12 and currency CNY, validate required fields, numeric types and allowed currencies at the receiving end. Missing fields, words instead of numbers or extra prose need an explicit retry/failure path.

How to check your practice

Explain the example in your own words, inspect input constraints, then verify against the source. Revisit concepts if you cannot explain why; supply missing dates, fields or permissions; withdraw unsupported conclusions.

For this topic, start with missing input · See all six scenarios and their reasoning

Sources
  • OpenAI Structured outputs guide ↗
  • Anthropic Tool use overview ↗
  • JSON Schema specification ↗