Implementing Enums for Controlled LLM Choices
Learn how to use JSON Schema enums to restrict LLM classification outputs and eliminate invalid data tags in small engineering teams.
Force an LLM to select from a predefined list by using JSON Schema enum constraints in your API call payload. Plain text prompts allow the model to invent custom labels like urgent or critical when you only want high. Explicit schema definitions enforce data normalization directly at the API boundary, guaranteeing reliable downstream processing.
Define standard tags using property enum arrays
To constrain text choices, map your targeted key inside the JSON Schema properties block and attach an enum array containing your allowed values. In JSON Schema syntax, keys are always strings that map directly to specific schema definitions. By explicitly listing allowed values inside the schema, you prevent the language model from generating variations like high-priority or P1 JSON Schema object reference.
Your application passes this declaration inside the structured output configuration of your model request. The API enforces strict field validation against these declared keys, blocking unstructured text generation. Ensure your schema includes all valid operational categories so the model does not fail during classification.
Execute schema constraints on ticket input
Consider a support ticket classification system where incoming messages must receive one of three designated priority tags. You supply a payload schema containing a priority key restricted to the string values high, medium, and low.
Input Text: The main checkout page returns a 500 error when users submit payment.
Model Schema Configuration:
{
“type”: “object”,
“properties”: {
“priority”: {
“type”: “string”,
“enum”: [“high”, “medium”, “low”]
}
},
“required”: [“priority”]
}
Expected Output:
{
“priority”: “high”
}
The model outputs exact string values without additional markdown text or non-standard synonyms.
Handle model refusals and schema boundary errors
A common failure occurs when an input ticket does not fit any configured enum value, or when the system receives ambiguous text. If a customer writes to say thank you for the fast resolution, forcing a high, medium, or low assignment can cause unpredictable classification. The model might pick a bad label or fail schema validation.
Resolve this boundary issue by expanding the enum array to include neutral options such as low or non-actionable. Alternatively, provide clear instructions in your system prompt on how to map unlisted categories. Do not attempt to update enum choices dynamically during an active API call without modifying the code contract.
Verify runtime payloads against structural rules
Validate model outputs using standard JSON Schema validator libraries in your application pipeline. Check that returned objects match required properties and that string values exist within the expected enum list. If your system flags a parsing error, log the raw response body alongside the original prompt to review model behavior.
Set up automated assertions to confirm that every parsed record routes correctly to your database. If a record requires elevated rights or triggers external account changes, insert a human review step before running downstream automation.