Skip to content
Eval Suites50

How to test structured LLM output beyond the JSON schema

Sources checked 2 min read · By the Eval Suites editors

In brief

A structured LLM response passes only when the response completed, the object matches its schema and the values match the user's request. Report invalid shape, incorrect fields, refusals and incomplete output as separate outcomes so you can see which part of the system needs work.

Check completion, shape and meaning in that order

A schema can require a field and limit its values. It cannot determine which allowed value the user intended unless the application supplies the expected value. Validate the object against the schema, then compare its contents with the expected result for that case.[1]

Read the response status before parsing a payload. OpenAI's Responses API guide documents refusal output and incomplete output when generation reaches the token limit. Decide how your application displays or handles each state, and test that behavior with saved responses that represent the cases you expect.[2]

Run this check at the boundary you want to ship. A UI card can consume a structured response. A tool call asks application code to perform an action. When you expose a tool, separately test the arguments and what the application actually did. OpenAI's guides document structured responses and function calls as those two uses.[2][3]

Run the Mesa Cycle Shop callback fixture

The fictional customer says “I need a callback from the East counter. Please don't book a tune-up.” The UI accepts exactly two fields. The request type allows callback or tune_up. The counter allows east or west. The successful object identifies callback and east. The fixture uses plain Python to check the constraints of this small contract. Use your schema validator for the real application.

Offline verdicts for completed, refused and incomplete responses
import json

expected = {'request_type': 'callback', 'counter': 'east'}
allowed = {'request_type': ('callback', 'tune_up'), 'counter': ('east', 'west')}

def verdict(status, output=''):
    if status in ('refusal', 'incomplete'):
        return status
    if status != 'completed':
        return 'unexpected_status'
    try:
        obj = json.loads(output)
    except json.JSONDecodeError:
        return 'invalid_json'
    if not isinstance(obj, dict) or obj.keys() != allowed.keys():
        return 'invalid_shape'
    if any(obj[field] not in values for field, values in allowed.items()):
        return 'invalid_shape'
    return 'pass' if obj == expected else 'wrong_values'

assert verdict('completed', json.dumps(expected)) == 'pass'
assert verdict('completed', '{"request_type":"callback","counter":"west"}') == 'wrong_values'
assert verdict('completed', '{"request_type":"tune_up","counter":"east"}') == 'wrong_values'
assert verdict('completed', '{"request_type":"callback"}') == 'invalid_shape'
assert verdict('completed', '{"request_type":') == 'invalid_json'
assert verdict('refusal') == 'refusal'
assert verdict('incomplete', '{"request_type":') == 'incomplete'
assert verdict('incomplete', json.dumps(expected)) == 'incomplete'
assert verdict('failed', json.dumps(expected)) == 'unexpected_status'

The status strings in this fixture represent normalized outcomes from an application adapter. Test that adapter against the provider's raw response separately. For streaming, feed the UI a valid buffered object followed by an incomplete final status. The card must stay unavailable even though the buffer parses. In a refusal case, assert the message or fallback the application was designed to show.[2]

Report failure counts against the right denominator

On cases that should return an object, report how many attempts completed and how many delivered the expected object. Among completed cases, report how many objects passed schema validation. Among schema-valid cases, record which fields were wrong. If no cases reach a stage, leave its rate undefined and show the zero denominator.

Keep refusal and truncation fixtures in the suite with their own expected handling. For a release gate, add examples where two values look plausible, a required field is missing, or the model fills a field despite ambiguous input. Use deterministic assertions for rules with a known answer.

Related suites

Related reading

Sources

  1. 1. JSON Schema object validation · JSON Schema · Official docs · Sources checked
  2. 2. Structured model outputs in the Responses API · OpenAI · Official docs · Sources checked
  3. 3. Function calling · OpenAI · Official docs · Sources checked