New Tool Radar

AI Concepts

How to Constrain a Model Output Field to a Fixed Set of Enum Values

Put an enum in the schema for a field your code branches on, then check no other value ever comes back.

Schematic with three steps: Write the enum into the schema; Send a test input that invites a; Check the code path for each value.

When downstream code branches on a field, such as a priority or a status, a free-text value breaks it: the model might write High, HIGH or very urgent for the same thing. An enum in the schema fixes the set of allowed values, and Structured Outputs constrains the model to those values, so the code can trust the field. The enum is one of the schema benefits the OpenAI docs name explicitly: required fields and enum values follow the supported JSON Schema you supply.

What you need

  • A field your code branches on, such as priority or status
  • The fixed list of values that field may take
  • A schema that includes the enum

Step 1: Write the enum into the schema

Define the field with an enum listing every allowed value, for example low, medium and high, and mark the field required. Keep the list short so the model can hold it.

Step 2: Send a test input that invites a wrong value

Send an input that would tempt a free-text answer, such as a ticket described as extremely urgent, and check the reply uses one of the enum values and nothing else.

Step 3: Check the code path for each value

Run each enum value through the code that reads the field and confirm every branch works. A value the code does not handle is a schema bug, not a model bug.

Worked example: synthetic enum field test

The table below is a synthetic example. It shows the expected result of an enum test; no API call was made.

Input wording Returned value Allowed?
Extremely urgent ticket high Yes
Minor typo report low Yes
Needs an answer today medium Yes
Free-text value None returned Pass – enum held

Expected result (UNTESTED). every reply uses one of low, medium or high, and no free-text value appears. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • An enum constrains shape, not correctness; the model can pick the wrong allowed value confidently.
  • Keep enums short; a long list is harder for the model to choose from and harder for code to handle.
  • Schema adherence covers required fields and enum values; it does not check that the chosen value matches the input.
  • If a new value is ever needed, change the schema and the code together.

Sources

See all