Stop Hoping for Valid JSON
The default way most people get structured output from an LLM is: ask for JSON, get something back, try to parse it, catch the exception, maybe retry with a sterner prompt. That whole loop exists because the model is free, at every step, to emit whatever token it wants. It just usually cooperates. Constrained generation removes the "usually." Instead of asking nicely and validating afterward, you make the invalid output impossible to produce in the first place.
- Your JSON parser has a retry loop because the model occasionally adds a trailing comma or wraps the output in prose
- You're running an open-weight model locally and don't have access to a hosted structured-output feature
- A downstream SQL generator occasionally produces syntactically invalid SQL that only fails at execution time
- You've tuned the prompt five different ways and the failure rate on malformed output still isn't zero

