You need reliable JSON from the model to feed a downstream service.
You currently ask for JSON in the prompt and hope for the best.
- AAdd "return ONLY valid JSON" in bold and hope compliance improves
- BAsk for JSON, then have a second model rewrite it as JSON
- CUse the API’s structured-output / JSON-schema mode so the shape is enforced
- DRaise max tokens and add a closing «end of JSON» marker to the prompt, so the object is never cut off and is always well-formed
Answer: C
Use the API’s structured-output / JSON-schema mode so the shape is enforced
Why
When JSON must be machine-parsable, constrain generation with the API’s schema/JSON mode rather than trusting a prose instruction. Enforcement at the decoding level guarantees the shape; a stern instruction only nudges probability.
- Structured outputAbout 2% of responses come back as JSON with a trailing comma or a stray note before the object, and your pipeline crashes on them.
- Structured outputYou need three fields from the model.
- Structured outputThe model hit the token limit halfway through a JSON object.
- Structured outputYour classifier uses DeepSeek’s JSON mode.
More on Way of the Agent
- Course programThe first three modules are free: start in the browser or the app, no card needed.
- Library: guides, skills and promptsReady prompt templates, grouped into collections.
- AI ChroniclesSourced stories from the history of AI.
- ConceptsPlain-language explainers of agent engineering.
- Learn AI agentsApps and courses for learning AI agents, compared honestly.
Real situations from building agents - with the answer and why.
Solve a new one every day on the Path