← Back

Structured LLM Output: Validate Beyond the Schema

Validate structured LLM output at syntax, schema, and business-rule boundaries. Handle unknown values, rejected proposals, and bounded retries without silent errors.

Structured LLM output gives your application a predictable shape to inspect. It does not prove that the values are true, permitted, or appropriate to execute. A perfectly formed object can name the wrong customer, invent a delivery date, or request an unauthorized operation.

Treat model output as a proposal crossing an application boundary. Parse it, validate its structure, check its meaning against trusted state, and only then decide what the application should do. Keeping these steps separate makes failures easier to handle honestly.

Define the smallest useful contract

Suppose an assistant extracts a support request into a routing proposal. It needs a category, a short summary, and whether more information is required. It does not need to invent a priority score if your support policy already computes priority from account state and issue type.

A compact illustrative JSON Schema could be:

{
  "type": "object",
  "properties": {
    "category": {"enum": ["billing", "delivery", "other"]},
    "summary": {"type": "string", "maxLength": 400},
    "needs_clarification": {"type": "boolean"}
  },
  "required": ["category", "summary", "needs_clarification"],
  "additionalProperties": false
}

JSON Schema's object reference explains required properties and restricting additional fields. Check the schema dialect and feature subset supported by your model provider and local validator; do not assume every structured-output API accepts every JSON Schema keyword.

A schema is easiest to maintain when it describes the data your application actually consumes. Adding speculative fields increases ambiguity and gives downstream code more opportunities to rely on unsupported information.

Distinguish missing, unknown, and invalid

A missing value can mean the model failed to follow the contract. An explicit unknown value can mean the source did not contain the information. Those are different conditions and should not collapse into an empty string.

For an invoice extractor, a missing invoice number might require review. An absent optional purchase-order reference might be normal. Document these semantics for each field and make the application handle them explicitly.

Avoid defaults that manufacture facts. Replacing an unknown date with today's date produces a syntactically convenient lie. If a downstream system requires a date, stop and obtain it from an authoritative source or the user.

Use enums where the domain is genuinely closed. An other category can be useful, but it needs a defined route. Otherwise, the system may silently discard every unfamiliar case.

Validate types without surprising coercion

Different validators and modes can accept or transform values differently. A string containing a number may become a number in a permissive mode. That might be acceptable for a form input and inappropriate for a security-sensitive identifier.

Pydantic's strict-mode documentation explains stricter type handling and notes differences between validating Python objects and JSON input. Test the exact path your application uses rather than assuming “strict” means the same behavior for every data type and input format.

For money, use a representation and currency rule appropriate to the application. Avoid letting a model choose whether 100 means cents or major currency units. That ambiguity belongs in the schema and domain contract, not in a prompt footnote.

Likewise, a UUID-shaped string is not proof that a record exists or belongs to the authenticated account. Structural validation is only the first boundary.

Check business rules against trusted state

After validation, look up referenced records through authorized application code. Confirm ownership, current status, and relevant constraints. A proposed refund amount should be compared with the order's actual refundable amount, not with another model-generated field.

Use the current state when the action executes. If an order shipped after the proposal was created, an address change may no longer be allowed. Return a clear stale-proposal result and ask for a revised decision if necessary.

Do not let a field called approved create its own authority. Approval should come from a trusted workflow with the appropriate actor and scope. The same applies to generated confidence values: they can be diagnostic data, but they are not permissions.

This separation complements the tool-permission boundary: the model proposes, while the application validates data and controls consequences.

Retry selectively and with a limit

A syntax or schema error may justify a repair attempt. A missing fact generally requires more evidence. An authorization denial should not trigger repeated attempts to rephrase the same forbidden action until it passes.

Classify failures and assign bounded behavior. For example, allow one formatting repair, ask a clarifying question for an ambiguous account reference, and stop immediately on a permission failure. These are illustrative policy choices; tune them to the task.

When asking for a repair, return concise validation feedback without exposing secrets or unnecessary source data. Preserve the original error for debugging. Do not silently drop unknown fields if doing so hides a model misunderstanding that matters to the workflow.

Test the boundary, not just the happy path

Include cases with extra fields, wrong types, unknown enum values, oversized strings, conflicting fields, nonexistent records, and stale state. Test what the user sees when validation fails. A robust parser with a misleading “success” message is still a broken product.

Version the schema when its meaning changes. A field rename is obvious, but changing the interpretation of an existing value can be more dangerous because old consumers may continue accepting it.

Our parse-don't-validate discussion offers related thinking about explicit data boundaries. For model-generated data, the crucial extra step is grounding values in trusted evidence before allowing them to affect the world.