DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

Structured Output: Make an LLM Return Real JSON (Not Prose)

"Return the answer as JSON" — and then the model wraps it in prose, adds a


json fence, or sends a string where you wanted a number, and your `JSON.parse` throws. Structured output fixes this properly. Here's the difference, live.

🧱 **See JSON-in-prose break vs schema hold:** https://dev48v.infy.uk/prompt/day16-structured-output.html

## Don't beg for JSON — bind to a schema

The naive way (ask nicely in the prompt) fails unpredictably: markdown fences, trailing commentary, wrong types, missing fields. Three real fixes:

- **JSON mode** — the model is guaranteed to emit syntactically valid JSON.
- **Function / tool calling** — you define a named function with typed arguments; the model fills them. This is how tool use and agents work.
- **Structured outputs (schema-constrained)** — you pass a JSON Schema (or Zod/Pydantic) and the decoder is constrained to match it exactly. Always parses, always typed.

## Validate anyway

Even with a guaranteed *shape*, the model can hallucinate *values*. Define the schema (required fields, enums to constrain choices), parse it, validate, and retry on failure.

## Why it matters

This is the backbone of data extraction, classification, and every agent that calls tools — reliable JSON turns an LLM from a chatbot into a component your code can trust.

🔨 Full build (Zod/JSON Schema → response_format/tool → parse + validate → retry) on the page: https://dev48v.infy.uk/prompt/day16-structured-output.html

Part of PromptFromZero. 🌐 https://dev48v.infy.uk
Enter fullscreen mode Exit fullscreen mode

Top comments (0)