AI applications often need values that code can process, not only a paragraph for a person to read. A structured output uses named fields in a predictable form, such as a JSON object. A support system, for example, may need a short summary and a priority for each message.
A predictable shape helps integration, but it does not prove that the values are correct. Parse the response, validate its fields, and check important claims against the original input.
Define the output fields
Decide which fields are required, what type each field holds, and which values are allowed. Keep the structure no larger than the task needs.
| Field | Rule | Example |
|---|---|---|
summary
| Required, nonempty text | Customer requests a missing charger |
priority
| Required: low, medium, or high
| low
|
Define what the labels mean before classifying real messages. In this lesson, an unstated deadline means low; another application may have different rules.
Ask for a structured response
Give the task, field names, allowed values, and a rule for missing information. The following prompt asks for exactly two fields.
Example:
Read the customer message. Return one JSON object with exactly
"summary" and "priority". Keep summary under 20 words.
Priority must be "low", "medium", or "high". If no urgency is
stated, use "low". Do not add facts not present in the message.
Message: The charger is missing from my order. Please send it.
Possible output:
{
"summary": "Customer requests the missing charger",
"priority": "low"
}
Examples show the requested shape, but the model may still produce an unexpected response. Some AI services can constrain output with a schema; applications must still handle refusals, incomplete responses, and incorrect field values.
Check JSON and field rules
Valid JSON is only the first check. A JSON object can omit a required field, contain an extra field, use the wrong data type, or make an unsupported claim. The Python example below validates a sample response after it has been received. It does not call an AI service.
Example:
import json
raw = '{"summary": "Customer requests a charger", "priority": "low"}'
record = json.loads(raw)
if not isinstance(record, dict):
raise ValueError("Expected an object")
if set(record) != {"summary", "priority"}:
raise ValueError("Fields are missing or unexpected")
if not isinstance(record["summary"], str) or not record["summary"].strip():
raise ValueError("Summary must be text")
if not isinstance(record["priority"], str) or record["priority"] not in {"low", "medium", "high"}:
raise ValueError("Invalid priority")
print(record["priority"])
print(record["summary"])
Output:
low
Customer requests a charger
The program rejects missing or extra fields, empty summary text, and an invalid priority. In a real application, catch parsing and validation errors and decide whether to retry, request human review, or reject the record.
Check meaning as well as format
Suppose the model returns "priority": "high" for a message with no deadline. The JSON may pass a type check, but the label conflicts with the rule above. A parser cannot decide whether every statement is supported by the customer message.
- Compare extracted facts with the original message.
- Test empty, ambiguous, and contradictory messages.
- Do not treat instructions inside a customer message as authority to change your output rules.
- Review or block uncertain records before they affect people, orders, or payments.
Note: A schema controls structure, not truth. Even schema-constrained output needs checks for missing evidence and domain rules.
Conclusion
Choose a small output shape, specify field rules, validate the returned data, and compare important values with their source. Reliable structured output requires both a correct format and a supported meaning.