Own Weights

Making LLM JSON output reliable

Make LLM JSON output reliable with schema enforcement, per-response validation, one capped retry and field-level tests on labelled data.

Updated

Reliable LLM JSON output means every response parses, matches the schema you defined, and carries the right values. Enforce the schema while the model generates, validate every response in your own code, and test field values against labelled examples from your own documents. Most failures come from loose prompts, fragile parsers, or no measurement at all.

Why LLM JSON breaks

A model writes JSON one token at a time, so it does not track brackets the way a parser does. The common syntax failures are a markdown code fence around the object, a sentence of commentary before the opening brace, a trailing comma, or an unescaped quote inside a string.

The harder failures parse cleanly. Take an invoice extractor whose schema says invoice_date is an ISO date (YYYY-MM-DD), currency is one of USD, EUR or GBP, and total is a plain decimal string. A response might read:

{"invoice_number": "INV-0412", "invoice_date": "03/04/2026", "currency": "US$", "total": "1,240.00"}

A standard parser accepts this. A schema check with a date pattern and an enum rejects it, and that catches the visible errors. The dangerous case is a date that is valid ISO but wrong. A European invoice dated 4 March 2026 is written 04/03/2026, the model reads it as April 3, and it outputs 2026-04-03. The schema passes that value, so only a field-level test against labelled invoices will catch it.

Enforce the schema while the model generates

Constrain generation so the model can only produce output that fits your schema. Many hosted APIs and serving stacks offer structured output or grammar-constrained decoding, so use that instead of asking for JSON in the prompt. A line that says “respond only with JSON” is a request, not a guarantee. Structured output speed covers how this affects latency.

Read your provider’s documentation for what it actually guarantees. Some options enforce valid syntax only. Others also enforce required fields, types and enum values. Pick the one that covers the failures you saw in your logs. If you run your own model, the decoder still needs the schema.

Enforcement fixes shape, not content. Keep the schema small, because every required field is another way for a response to fail:

  • Use enums where answers come from a known set, such as currency codes or document types.
  • Ask only for fields your code reads. A “notes” field nobody uses still costs output and can still fail.
  • Flatten nested objects when the second level adds nothing.
  • Use formats the validator can check, such as ISO dates and decimal strings, instead of free text.

Validate, retry and log every failure

Put a validator between the model and your code. Parse the output, check it against the schema, and on failure send one retry with the validation error included in the prompt. If the retry fails, route the record to a person or a fallback path. Set the retry cap in code, so the program enforces the limit and nobody has to remember it.

Log every failure with the raw output, the schema version and the input id. Grouping those logs usually shows the fix. If most failures are date formats, change the prompt or add an example. If they cluster on long documents, shorten the input or split it. If the model keeps writing a value that is not in your enum, the enum is probably missing a real category.

Avoid regex repair of broken JSON. A function that adds missing brackets will sometimes produce a valid object with the wrong contents. Nothing crashes, and the bad value flows into your database, where it is harder to find than a failed parse.

Test the values, not only the parse

Parsing is the easy metric. The hard one is whether each field holds the correct value. Build a test set from real inputs with labelled correct outputs. The step-by-step evaluation guide shows how to set one up. Split by document, not by row, so near-duplicate inputs do not appear on both sides of the split. Score each field separately, because one overall score hides the field that fails most.

Run the test on your current setup first. That gives you a baseline for validity and for field accuracy. Every later change, whether a new prompt, a different provider or a trained model, is compared against that baseline on the same test set. Keep the random seed and the split fixed so the numbers stay comparable from one run to the next. For how to split small labelled sets without leaking documents, see cross-validation for LLMs.

When a trained model is the better fix

If one task produces most of your JSON, a model trained on that task learns your field names, formats and edge cases from examples. The same applies to getting exact text out of documents. Instead of a long prompt of instructions, the behavior lives in the weights, which can shorten prompts and lower the retry rate. Measure both on your own data before you switch.

To compare, run the trained model and your current setup on the same held-out labelled set, and record the parse rate, the schema failure rate, the retry rate and the accuracy of each field side by side. A trained model earns the switch only when field accuracy holds or rises. Fewer retries with more wrong values is a worse result, not a better one.

Own Weights builds these models for one task at a time, in the customer’s own cloud account or on-prem.

Where to start

Work through these steps on one endpoint:

  1. Pull a sample of real inputs, large enough to show each field’s errors, and label the correct output for each one.
  2. Record the parse rate, the schema failure rate and the accuracy of each field.
  3. Fix the largest failure first, such as a date format or a missing enum value.
  4. Measure again on the same test set, and keep the change only if the numbers improve.

The free AI cost audit flags JSON parsing without structured output, and it uploads nothing. Book a call if you want us to review your JSON calls with you.

Questions

How do I get valid JSON from an LLM every time?

Use your provider's structured output or schema-constrained decoding, then validate every response in your own code. Retry once with the validation error attached, and log each failure with its raw output. Keep the validation step even when the schema is enforced, as a second check.

Does structured output slow down LLM responses?

The cost depends on the schema and the serving setup, so it is not the same for every workload. Time your own schema on your own inputs before you decide. Compare the total time to a finished, validated response, not just the first token.

What should I measure in LLM JSON output?

Measure whether each response parses and matches the schema, and whether each field holds the correct value. Use labelled examples from your own data, and keep them out of any training run.