Pydantic utilities

Functions and classes to handle the validation of hyperparameters using Pydantic.

exception metatrain.utils.pydantic.MetatrainValidationError(model: Any, errors: list[dict])[source]

Bases: Exception

This class transforms Pydantic validation errors into a more user-friendly format.

Parameters:
  • model (Any) – The Pydantic model class or TypedDict that was used for validation.

  • errors (list[dict]) – The list of Pydantic error dictionaries.

get_error_string(error: dict) str[source]

Given an individual error from Pydantic, return a user-friendly string.

Parameters:

error (dict) – The Pydantic error dictionary.

Returns:

The formatted error string to display to the user.

Return type:

str

default_pydantic(err: dict) str[source]

Default Pydantic error formatting.

Parameters:

err (dict) – The Pydantic error dictionary.

Returns:

The formatted error string to display to the user.

Return type:

str

metatrain.utils.pydantic.validate(model_cls: Any, data: dict, **kwargs: Any) None[source]

Validate with pydantic, raising custom metatrain errors.

Parameters:
  • model_cls (Any) – The Pydantic model class to use for validation. If it is not a pydantic model, it will be adapted to pydantic using pydantic.TypeAdapter.

  • data (dict) – The data to validate.

  • **kwargs (Any) – Additional keyword arguments to pass to the validation method.

Raises:

MetatrainValidationError – If validation fails.

Return type:

None

metatrain.utils.pydantic.validate_architecture_options(options: dict, model_hypers: type, trainer_hypers: type) None[source]

Validate architecture-specific options using Pydantic.

Parameters:
  • options (dict) – The architecture options to validate.

  • model_hypers (type) – The ModelHypers class of the architecture.

  • trainer_hypers (type) – The TrainerHypers class of the architecture.

Return type:

None

metatrain.utils.pydantic.validate_base_options(options: dict) None[source]

Validate base options using Pydantic.

Parameters:

options (dict) – The base options to validate.

Raises:

ValueError – If the options are invalid.

Return type:

None