Value-oriented output
Models whose dumped data is sent to JSON APIs or persistence layers.
Pydantic · Pipe
use_enum_values=True makes Pydantic populate enum fields with their underlying values rather than enum members. Optional enum defaults need validate_default=True for this conversion to apply.
Set use_enum_values=True in ConfigDict to make validated enum fields hold their underlying values instead of enum members. For an optional enum default, add validate_default=True to its Field, because conversion happens during validation.
ConfigDict(use_enum_values=True)The setting changes populated field values; the documented example shows model_dump() containing underlying enum values.
value instead of the enum object.model_dump() should contain serialization-friendly enum values.model_dump() returning strings such as 'bar' and 'foo' after the option is enabled.validate_default=True; otherwise its default is not converted.from enum import Enum
from typing import Optional
from pydantic import BaseModel, ConfigDict, Field
class Flavor(Enum):
VANILLA = "vanilla"
CHOCOLATE = "chocolate"
class Order(BaseModel):
model_config = ConfigDict(use_enum_values=True)
flavor: Flavor
fallback: Optional[Flavor] = Field(
default=Flavor.VANILLA, validate_default=True
)
order = Order(flavor=Flavor.CHOCOLATE)
assert order.model_dump() == {"flavor": "chocolate", "fallback": "vanilla"}
print(order.model_dump())The model stores enum values and validates the optional default so it is converted as well.
Models whose dumped data is sent to JSON APIs or persistence layers.
Schemas containing optional enum fields with defaults.
Applications that compare or transform dumped data using primitive values.
Set model_config = ConfigDict(use_enum_values=True) on the model. Enum inputs are then populated using each enum member's value, which is also convenient when calling model_dump().
Follow-up: Why might an optional enum default remain an enum member? Because defaults are not validated automatically in this case; enable validate_default=True on the field.
use_enum_values?The setting defaults to False, so the model otherwise retains raw enum members. With it enabled, a member such as SomeEnum.BAR is represented by its underlying value, such as 'bar'.
Follow-up: Where does the conversion occur? During validation, rather than during serialization, which explains the need to validate relevant defaults.
Add Field(default=..., validate_default=True) to an optional enum field that has a default. This causes the default to pass through validation and receive the same value extraction as supplied input.
Follow-up: What output should be expected from model_dump() after enabling the option? Enum fields appear as their underlying values, including validated defaults.
ConfigDictFieldvalidate_defaultBaseModel.model_dumpEnum