Skip to the page
Tecyfy

LLM apps and RAG interview question · medium

How do you get reliable structured output, such as JSON, from a model?

The short answerUse the provider's structured output mode with a JSON schema, which constrains generation so the output always parses and matches the schema.

The full answer

Use the provider's structured output mode with a JSON schema, which constrains generation so the output always parses and matches the schema. Then still validate the values in code, because a valid shape can hold wrong content. Prompting "reply in JSON" alone is not reliable enough for production.

Three levels of reliability

  1. Prompt only: "Reply only with JSON like this example." This mostly works, but expect occasional extra text, missing fields or broken quotes, so you need parsing with retries.
  2. Tool calling: define a tool whose input schema is your output, and have the model "call" it. Turning on strict: true makes the call follow the schema exactly.
  3. Structured outputs: on the Claude API, output_config.format with type: "json_schema" uses constrained decoding, so the response is valid JSON that matches the schema: required fields present, types right. OpenAI offers the same idea with its structured outputs.

Design the schema well

  • Keep it flat and small, with clear field names and descriptions.
  • Use enums for fixed choices ("status": "approved" | "rejected" | "needs_review").
  • Mark fields required, and set additionalProperties: false.
  • Give the model a way out: a nullable field or a "cannot_determine" value, so it is not forced to invent one.
  • Put a reasoning or notes field before the answer fields if the task needs thought.

Know the limits (Claude API)

  • Some JSON Schema features are not supported, such as minimum/maximum and string length limits. Check those in your own validation.
  • A refusal or hitting max_tokens can produce output that does not match the schema. Check the stop reason.
  • Enum values may come back in different capitalisation, so compare case-insensitively.

Validate in code anyway

Parse the result into a typed model (Pydantic, Zod) and check what the schema cannot: ranges, ids that must exist, totals that must add up, dates in the right order. On failure, retry once with the error message, then send the item to review. OWASP's improper output handling risk applies too: model output that goes into SQL, HTML or a shell must be treated as untrusted input.

Example: extracting invoice fields

Schema: vendor (string), invoice_date (date), total (number), currency (enum), line_items (array). With structured output the JSON always parses. Code then checks that the line items add up to the total and that the date is not in the future. Mismatches go to a person.

What interviewers listen for: the difference between valid format (the schema solves it) and correct content (validation and evaluation solve it).

Last updated 16 October 2026