Your pipeline needs JSON matching a schema on every call. Compare the options for guaranteeing it, and how you’d handle the failures that remain.
Answer
Model answer, written in a candidate's voice
In short
- Prompting and examples are unreliable; JSON mode only guarantees parseable JSON, not schema conformance.
- Schema-constrained decoding/structured outputs and tool calls enforce syntax and schema for supported schemas, not semantic correctness.
- Add a validation layer and targeted retry with the validation error, with bounded attempts and a fallback.
- Explicitly handle refusals and max-token truncation instead of parsing partial output.
- Keep schemas simple, put reasoning/evidence first, and use enums for categories to avoid forced early answers.
In full
I would not rely on prompting and examples alone; they improve the odds but do not guarantee anything. JSON mode is better because it makes the output parseable, but it still does not guarantee the schema. Schema-constrained decoding or structured outputs, and tool calls with a declared schema, are the strongest options because they constrain generation to the supported schema, though they still cannot prove the content is true or semantically correct. I would still validate the parsed object against the schema and business rules, then retry with the exact validation error and a bounded attempt count, falling back to a safe default or human review if it keeps failing. I would also explicitly handle refusals and truncation at max tokens by checking finish reason and completeness, never parsing partial JSON, and retrying with a smaller schema or shorter reasoning only when that is appropriate. Finally, I would keep schemas simple, put a reasoning or evidence field before the final answer, and use enums for categories so constrained decoding does not force an early, shallow answer.
What a strong answer covers
- Prompt + examples (unreliable) → JSON mode → schema-constrained decoding/structured outputs (syntax and schema guaranteed) → tool call with schema
- Validation layer plus targeted retry with the validation error; explicit handling of refusals and truncation at max tokens
- Constrained decoding can hurt quality if the schema forces an early answer — put a reasoning field first or keep schemas simple; enums for categories
Common mistakes
- Regex “repair” as the main strategy
- answer field placed before the reasoning field