Skip to content
Preptima
mediumDesignScenarioMidSeniorStaff

Your code needs JSON back from the model and it has to parse every time. How do you make that happen?

4 min readUpdated 2026-08-15

Answer

Short answer

Constrain the decoder to a schema instead of asking for JSON in prose, reserve enough output tokens that a complete document fits, validate at the boundary, and allow one repair retry that quotes the exact violation back.

Was this answer helpful?

Likely follow-ups

  • Constrained decoding guarantees shape but not sense. What kinds of wrong output does it still let through?
  • Your schema has a free-text `reason` field of unbounded length. What does that do to your token reservation?
  • How would you handle a case where the model legitimately cannot answer and there is no valid object to return?
  • What changes if you need the structured result streamed rather than returned whole?

Related questions

Masterclass guide

Related Topics