Skip to content

Runtime parameters

RuntimeParameter = Annotated[Union[_T, None], Field(default=None), _RUNTIME_PARAMETER_ANNOTATION] module-attribute

Used to mark the attributes of a Step as a runtime parameter.

RuntimeParametersMixin

Bases: BaseModel

Mixin for classes that have RuntimeParameters attributes.

Attributes:

Name Type Description
_runtime_parameters Dict[str, Any]

A dictionary containing the values of the runtime parameters of the class. This attribute is meant to be used internally and should not be accessed directly.

Source code in src/distilabel/mixins/runtime_parameters.py
class RuntimeParametersMixin(BaseModel):
    """Mixin for classes that have `RuntimeParameter`s attributes.

    Attributes:
        _runtime_parameters: A dictionary containing the values of the runtime parameters
            of the class. This attribute is meant to be used internally and should not be
            accessed directly.
    """

    _runtime_parameters: Dict[str, Any] = PrivateAttr(default_factory=dict)

    @property
    def runtime_parameters_names(self) -> RuntimeParametersNames:
        """Returns a dictionary containing the name of the runtime parameters of the class
        as keys and whether the parameter is required or not as values.

        Returns:
            A dictionary containing the name of the runtime parameters of the class as keys
            and whether the parameter is required or not as values.
        """

        runtime_parameters = {}

        for name, field_info in self.model_fields.items():  # type: ignore
            is_runtime_param, is_optional = _is_runtime_parameter(field_info)
            if is_runtime_param:
                runtime_parameters[name] = is_optional
                continue

            attr = getattr(self, name)
            if isinstance(attr, RuntimeParametersMixin):
                runtime_parameters[name] = attr.runtime_parameters_names

        return runtime_parameters

    def get_runtime_parameters_info(self) -> List[Dict[str, Any]]:
        """Gets the information of the runtime parameters of the class such as the name and
        the description. This function is meant to include the information of the runtime
        parameters in the serialized data of the class.

        Returns:
            A list containing the information for each runtime parameter of the class.
        """
        runtime_parameters_info = []
        for name, field_info in self.model_fields.items():  # type: ignore
            if name not in self.runtime_parameters_names:
                continue

            attr = getattr(self, name)
            if isinstance(attr, RuntimeParametersMixin):
                runtime_parameters_info.append(
                    {
                        "name": name,
                        "runtime_parameters_info": attr.get_runtime_parameters_info(),
                    }
                )
                continue

            info = {"name": name, "optional": self.runtime_parameters_names[name]}
            if field_info.description is not None:
                info["description"] = field_info.description
            runtime_parameters_info.append(info)
        return runtime_parameters_info

    def set_runtime_parameters(self, runtime_parameters: Dict[str, Any]) -> None:
        """Sets the runtime parameters of the class using the provided values. If the attr
        to be set is a `RuntimeParametersMixin`, it will call `set_runtime_parameters` on
        the attr.

        Args:
            runtime_parameters: A dictionary containing the values of the runtime parameters
                to set.
        """
        for name, value in runtime_parameters.items():
            if name not in self.runtime_parameters_names:
                continue

            attr = getattr(self, name)
            if isinstance(attr, RuntimeParametersMixin):
                attr.set_runtime_parameters(value)
                self._runtime_parameters[name] = value
                continue

            # Handle settings values for `_SecretField`
            field_info = self.model_fields[name]
            inner_type = _extract_runtime_parameter_inner_type(field_info.annotation)
            if inspect.isclass(inner_type) and issubclass(inner_type, _SecretField):
                value = inner_type(value)

            setattr(self, name, value)
            self._runtime_parameters[name] = value

runtime_parameters_names: RuntimeParametersNames property

Returns a dictionary containing the name of the runtime parameters of the class as keys and whether the parameter is required or not as values.

Returns:

Type Description
RuntimeParametersNames

A dictionary containing the name of the runtime parameters of the class as keys

RuntimeParametersNames

and whether the parameter is required or not as values.

get_runtime_parameters_info()

Gets the information of the runtime parameters of the class such as the name and the description. This function is meant to include the information of the runtime parameters in the serialized data of the class.

Returns:

Type Description
List[Dict[str, Any]]

A list containing the information for each runtime parameter of the class.

Source code in src/distilabel/mixins/runtime_parameters.py
def get_runtime_parameters_info(self) -> List[Dict[str, Any]]:
    """Gets the information of the runtime parameters of the class such as the name and
    the description. This function is meant to include the information of the runtime
    parameters in the serialized data of the class.

    Returns:
        A list containing the information for each runtime parameter of the class.
    """
    runtime_parameters_info = []
    for name, field_info in self.model_fields.items():  # type: ignore
        if name not in self.runtime_parameters_names:
            continue

        attr = getattr(self, name)
        if isinstance(attr, RuntimeParametersMixin):
            runtime_parameters_info.append(
                {
                    "name": name,
                    "runtime_parameters_info": attr.get_runtime_parameters_info(),
                }
            )
            continue

        info = {"name": name, "optional": self.runtime_parameters_names[name]}
        if field_info.description is not None:
            info["description"] = field_info.description
        runtime_parameters_info.append(info)
    return runtime_parameters_info

set_runtime_parameters(runtime_parameters)

Sets the runtime parameters of the class using the provided values. If the attr to be set is a RuntimeParametersMixin, it will call set_runtime_parameters on the attr.

Parameters:

Name Type Description Default
runtime_parameters Dict[str, Any]

A dictionary containing the values of the runtime parameters to set.

required
Source code in src/distilabel/mixins/runtime_parameters.py
def set_runtime_parameters(self, runtime_parameters: Dict[str, Any]) -> None:
    """Sets the runtime parameters of the class using the provided values. If the attr
    to be set is a `RuntimeParametersMixin`, it will call `set_runtime_parameters` on
    the attr.

    Args:
        runtime_parameters: A dictionary containing the values of the runtime parameters
            to set.
    """
    for name, value in runtime_parameters.items():
        if name not in self.runtime_parameters_names:
            continue

        attr = getattr(self, name)
        if isinstance(attr, RuntimeParametersMixin):
            attr.set_runtime_parameters(value)
            self._runtime_parameters[name] = value
            continue

        # Handle settings values for `_SecretField`
        field_info = self.model_fields[name]
        inner_type = _extract_runtime_parameter_inner_type(field_info.annotation)
        if inspect.isclass(inner_type) and issubclass(inner_type, _SecretField):
            value = inner_type(value)

        setattr(self, name, value)
        self._runtime_parameters[name] = value