microsoft/qlib · error · TypeError

The instance of {type(object)} is not a valid `{type(cls)}`!

Error message

The instance of {type(object)} is not a valid `{type(cls)}`!

What it means

Error "The instance of {type(object)} is not a valid `{type(cls)}`!" thrown in microsoft/qlib.

Source

Thrown at qlib/utils/serial.py:154

    def load(cls, filepath):
        """
        Load the serializable class from a filepath.

        Args:
            filepath (str): the path of file

        Raises:
            TypeError: the pickled file must be `type(cls)`

        Returns:
            `type(cls)`: the instance of `type(cls)`
        """
        with open(filepath, "rb") as f:
            object = cls.get_backend().load(f)
        if isinstance(object, cls):
            return object
        else:
            raise TypeError(f"The instance of {type(object)} is not a valid `{type(cls)}`!")

    @classmethod
    def get_backend(cls):
        """
        Return the real backend of a Serializable class. The pickle_backend value can be "pickle" or "dill".

        Returns:
            module: pickle or dill module based on pickle_backend
        """
        # NOTE: pickle interface like backend; such as dill
        if cls.pickle_backend == "pickle":
            return pickle
        elif cls.pickle_backend == "dill":
            return dill
        else:
            raise ValueError("Unknown pickle backend, please use 'pickle' or 'dill'.")

    @staticmethod

View on GitHub (pinned to 79633dd950)

Solutions

  1. Pass an object that is an instance of the expected serializable class.
  2. Reconstruct the object via the class's load/from_config path instead of passing a raw value.

When it happens

Trigger: This error is raised at qlib/utils/serial.py:154 when the guard condition fails: "instance is not a valid type". It triggers when the code path receives input or a state that violates the precondition checked at this point — e.g. an unimplemented abstract method being called, an unsupported argument type/value, or an invalid configuration. To avoid it, satisfy the documented precondition before this call: implement the required method in your subclass, or pass a supported value of the expected type as described by the message.

Common situations: See trigger scenarios.


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/a547109d3fffff11. Report an issue: GitHub.