lenskit.pipeline.Component#

class lenskit.pipeline.Component(config=None, **kwargs)#

Bases: ABC, Generic[COut, CArgs]

Base class for pipeline component objects. Any component that is not just a function should extend this class.

Pipeline components support configuration (e.g., hyperparameters or random seeds) through Pydantic models or Python dataclasses; see Configuring Components for further details. If the pipeline’s configuration class is C, it has the following:

  1. The configuration is exposed through an instance variable config.

  2. The constructor accepts the configuration object as its first parameter, also named config, and saves this in the member variable.

The base class constructor handles both of these, so long as you declare the type of the config member:

class MyComponent(Component):
    config: MyComponentConfig

    ...

If you do not declare a config attribute, the base class will assume the pipeline uses no configuration.

To work as components, derived classes also need to implement a __call__ method to perform their operations.

Parameters:
  • config (Any) – The configuration object. If None, the configuration class will be instantiated with kwargs.

  • kwargs (Any)

Stability:
Full (see Stability Levels).
__init__(config=None, **kwargs)#
Parameters:

Methods

__init__([config])

config_class([return_any])

dump_config()

Dump the configuration to JSON-serializable format.

validate_config([data])

Validate and return a configuration object for this component.

Attributes

config

The component configuration object.

config: Any = None#

The component configuration object. Component classes that support configuration must redefine this attribute with their specific configuration class type, which can be a Python dataclass or a Pydantic model class.

dump_config()#

Dump the configuration to JSON-serializable format.

Return type:

dict[str, JsonValue]

classmethod validate_config(data=None)#

Validate and return a configuration object for this component.

Parameters:

data (Mapping[str, JsonValue] | None)

Return type:

object | None