Over ons 🤖

Laten we elkaar leren kennen

Vertel me de missie en visie

Leg het verhaal achter Mach8 uit

Hallo daar 👋

Hoe kunnen we je helpen?

Mijn gegevens mogen worden gebruikt om me op de hoogte te houden van relevant nieuws van Mach8

Implementation & Technology·6 min·4 May 2025

Structured output with LLMs: getting reliable JSON from a language model

Language models are trained to generate text, not to produce valid JSON. If your application depends on a structured response, you need a strategy to get that reliably, even when the model occasionally deviates.

Anyone integrating AI into an application usually wants a neatly structured object back, not free text: a JSON response with specific fields that can be forwarded to a database or a next step in a workflow. The problem is that language models naturally produce free text. This article explains how to enforce structured output.

Why JSON from an LLM is tricky

A language model generates token by token based on probabilities. It does not "know" that it is producing JSON; it simply produces text that resembles JSON. Small errors — a missing closing bracket, an extra comma, or an unexpected field — are enough to crash a JSON.parse().

This is not a theoretical problem. In practice, a naive JSON prompt succeeds about seven out of ten times for complex output. For production environments, that is unacceptable. You need a more reliable approach.

Option 1: Native structured output via the API

The simplest solution is using the native structured output functionality that most major providers offer. OpenAI has response_format: { type: "json_schema" }, Anthropic offers tool use as a way to force structured output.

You provide a JSON Schema describing the fields you expect, their types, and whether they are required. The model then generates output that conforms to that schema — not perfectly, but significantly more reliably than a free prompt.

This is the recommended approach for new integrations. It is straightforward to implement and the responsibility for validation partly rests with the provider.

Option 2: Prompt techniques

If your provider does not support native structured output, you can structure the prompt so the model produces JSON. Proven techniques:

  • Provide an example of the desired output in the prompt (few-shot)
  • Ask the model explicitly to "return only JSON, without extra text"
  • Use a system prompt that describes the output structure
  • Ask the model to begin with { to prevent free text before the JSON

But do not rely on prompting alone. This works well for simple structures but becomes unreliable with complex nested objects.

Option 3: Parsing libraries

Libraries like Instructor (Python), zod-gpt (TypeScript), or Outlines add a layer on top of the LLM API that enforces structured output through retries and schema validation. You define a schema or dataclass, and the library automatically ensures the output conforms to it — including retrying on failure.

This is a practical middle ground: you control the schema, validation is automatic, and it works with multiple providers.

Validation and error handling

Regardless of which approach you choose, always validate the output before using it. A JSON response that is syntactically correct can still be semantically wrong: a required field that is empty, a number where a string is expected.

Use schema validation (such as Zod in TypeScript or Pydantic in Python) as a final safety net. Log failed validations so you can see how often things go wrong and which errors occur most frequently.

When JSON is not the right choice

Sometimes structured output is not the best solution. If your output primarily needs to be human-readable, free text is better. If the structure varies significantly per request, a rigid schema becomes a straitjacket.

Also consider whether you can shift the structuring: let the model generate free text, then use a second, simple call or a regular expression to extract the relevant information.

Conclusion

Getting reliable JSON from a language model requires more than a good prompt. It calls for a combination of native API features, schema validation, and error handling. Mach8 applies these techniques in AI workflows that need to be genuinely scalable and reliable.

Want to know how Mach8 builds AI integrations that run stably? View our AI agents service or get in touch.

Ready to apply AI?

We help you go from strategy to implementation. Schedule a no-obligation call.

Schedule a call