How to get structured data (JSON) from a language model reliably

Updated · 5 min read

Why free text breaks downstream systems

A language model writes sentences by default. Your database, spreadsheet or CRM needs fields: a supplier name in one column, an invoice date in another, a total and a currency in two more. A sentence such as 'the total looks like it is in the region of the usual amount' cannot be stored in a column, and a program cannot check it.

Structured output means the model returns the same fields in the same shape every time, so the next system can read them without a person copying values across. The work is to define that shape clearly, make the model fill it, and check what comes back.

Define the schema before you write the prompt

A schema is the written definition of your output. Write it before the prompt, because the prompt then describes the fields the schema already requires. Most teams use the JSON Schema standard, which names each field, its type and whether it is required.

  • Field name in plain English, for example supplier_name or invoice_date, using one naming style throughout.
  • Type: text, date, number, true or false, or a list. Ask for dates in one fixed format, such as the ISO standard, so every row sorts the same way.
  • Allowed values for anything with categories, such as a fixed list of expense types. A closed list stops the model inventing a new label.
  • Required or optional. Mark a field as required only if the business truly needs it.
  • A one-line description of each field. The model reads these descriptions when it decides what to put in each field.

Use the provider's structured output feature

Most major providers offer a way to send a schema with the request, so the output is constrained to that shape. Some offer a simpler JSON mode that guarantees valid JSON but not the right fields. The two are not the same, and the names, supported schema features and limits change, so check your provider's current documentation before you rely on one.

Even with a schema, keep your own validation. A schema check can confirm that a field is a date. It cannot confirm that the date is the one printed on the invoice.

Validate in code, then retry with the error

Treat every model output as untrusted input until your code has checked it. The check has three levels: the output parses as JSON, it matches the schema, and it passes your business rules, such as a currency code from your approved list or a date that is not in the future for a delivery.

When a check fails, the wrong response is to send the identical request again, because that usually reproduces the same failure. Send the original input, the failed output and the exact validation error, and ask for a corrected version. Limit retries to a small fixed number. After the limit, store the raw output and the error, and send the item to a person.

What to do when a field is genuinely unknown

Many failures come from forcing the model to fill a field that the source does not contain. The model then guesses, and the guess looks like data. Give the schema an explicit way to say unknown, such as a null value or an unknown option, and tell the model to use it rather than invent a value.

Ask the model to quote the short piece of source text it used for each important field. Staff can then check the quote in seconds. Route any record with a missing required field, or an unknown value in a key field, to a review queue instead of letting it flow into your reports.

Mistakes to avoid

Most broken structured output projects make the same few errors. Check for each one before you go live.

  • Asking for JSON in the prompt only, with no schema and no validation.
  • Marking every field as required, which forces guesses.
  • Letting the model choose its own field names from one call to the next.
  • Putting too many nested fields into one request. Split large extractions into smaller passes.
  • Writing keys in one language and content in another without saying so. Keep the keys in English, and state the language of the values.

How Hundred Seas helps

Hundred Seas builds AI agents and AI CRM systems on Claude and on OpenAI models. We choose the model for each task on cost, speed, quality and where the client's data must stay, and we are a registered partner in Anthropic's Claude Partner Network and an OpenAI partner.

A scope step of about one week, at a fixed price, ends in a written plan and a firm quote. The build then runs in two to four weeks of weekly releases, and a monthly run plan keeps the integration working and up to date.

Questions

Is JSON mode the same as structured output?
No. A JSON mode usually guarantees that the output parses as valid JSON, but not that it has the fields, types or values you expected. A schema-based structured output feature constrains the fields as well. You still validate in your own code, because a schema check cannot tell you whether a supplier name is correct.
Should the schema go in the prompt or in the API request?
Use both where you can. Pass the schema through the provider's structured output option where the platform supports it, and describe each field's meaning in the prompt, because the model reads those descriptions. Check your provider's current documentation before relying on one method, since support changes.
How many times should a system retry a failed output?
A small, fixed number, usually one or two, with the validation error sent back and the request changed. Retrying an identical request mostly repeats the failure. After the limit, keep the raw output and the error, and send the item to a person. A silent retry loop costs money and hides the problem.
Can a model extract data from scanned documents?
It can read clean scans and photographs, but accuracy falls with poor image quality, handwriting or unusual layouts. Run OCR or a document parser first where needed, then ask the model to extract fields from the text. Test with your own documents, including the poor ones, before you trust the output.
Should I ask for JSON or CSV?
Choose the format that the system reading the output expects. JSON suits nested or varied data and works with most APIs. CSV suits flat tables that people open in a spreadsheet. Decide the destination first, then ask the model for that exact format and validate it.

Have a project in mind?

Tell us what you want to build. We reply with a plan, not a sales call.

AboutHow to get structured data (JSON) from a language model reliably