Pydantic Policies & Validation¶
To make development reliable and prevent silent runtime crashes, mlflow-experience validates all configuration settings using Pydantic v2 (src/core/context/contexts.py and src/core/context/policies.py).
Why Use Pydantic?¶
In traditional ML projects with YAML files, a simple typo (such as strategy: meaan or omitting a required column) would only fail deep inside Scikit-Learn code after 10 minutes of execution.
With Pydantic: - Instant Failure: Your configuration is checked before any data is loaded or processed. - Helpful Error Messages: It tells you the exact line, field name, and expected values if something is wrong. - Automatic Type Conversion: String numbers are parsed into integers or floats automatically where appropriate.
How It Works¶
When main.py runs, it calls:
- Pydantic parses the dictionary against
RunContext. - A model validator (
cast_all_preprocessing_params) checks each step inpreprocessing. - The step's
typelooks up the corresponding policy class fromPreprocessingRegistry.policy_class. - The dictionary is instantiated into a strongly-typed policy object (e.g.
MissingValuePolicy,ScalingPolicy,EncodingPolicy).
Policy Reference¶
RenamePolicy¶
Validates column rename mappings:
- mapping (dict[str, str]): Must not be empty.
MissingValuePolicy¶
Validates missing value strategies:
- strategy: Must be one of "mean", "median", "mode", or "constant".
- fill_value: Required if strategy: constant.
EncodingPolicy¶
Validates encoding configurations:
- strategy: Must be one of "one_hot", "ordinal", "label", or "target".
- order: A list of strings specifying the exact order for ordinal encoding (e.g. ['Low', 'Medium', 'High']).
ScalingPolicy¶
Validates scaling strategies:
- strategy: Must be one of "standard", "minmax", or "robust".
Example Error Output¶
If you provide an invalid strategy in your YAML:
Pydantic immediately catches it: