validation_utils
Validation and type conversion utilities.
This module provides utilities for type conversion and validation, leveraging, for instance, Pydantic's validation capabilities for consistent type handling across the codebase.
Module
Functions
normalize_boolean
def normalize_boolean(value: Any) ‑> bool:Convert string booleans to actual booleans using Pydantic's logic.
This is defensive programming to handle cases where string representations of booleans (e.g., "false", "False", "true", "True") might be passed instead of actual boolean values. This can happen if template replacement doesn't work correctly, or if values come from a different code path.
Uses Pydantic's TypeAdapter for boolean validation, which handles common string representations like "true"/"false", "yes"/"no", "1"/"0", etc.
NOTE: non-empty, "non-boolean" strings will be converted to True as they are inherently truthy. This means that even "false"-like strings might be converted to True if they are not of a type that Pydantic can validate as a boolean. See Pydantic's documentation for more details.
Arguments
value: Value that should be a boolean (may be bool, str, or other type).
Returns Boolean value. Uses Pydantic's boolean validation which handles strings like "true", "false", "yes", "no", "1", "0", etc. Falls back to bool() conversion if Pydantic can't validate it.
normalize_integer
def normalize_integer(value: Any) ‑> int:Convert string/float representations of integers to actual ints.
This is defensive programming to handle cases where numeric values
arrive as strings (e.g. "3") because template replacement doesn't
enforce Python numeric types, or values come from a different code
path (such as Hub task templates, where JSON/YAML schema does not
guarantee Python int).
Uses Pydantic's TypeAdapter for integer validation, which coerces common representations like "3" or 3.0 to 3.
Arguments
value: Value that should be an integer (may be int, str, float, or other type).
Returns Integer value.
Raises
ValueError: If the value cannot be validated as an integer.