Hi all! How would you achieve typed parameters now...
# questions
k
Hi all! How would you achieve typed parameters now in Kedro (e.g. via dataclasses / pydantic)? I found this old issue and this implementation, but maybe there is a better way now to do it using the
OmegaConfigLoader
?
j
hi @Kacper Leśniara! there's some recent discussion about that in https://github.com/kedro-org/kedro/discussions/3788 and https://github.com/kedro-org/kedro/discussions/3808, please have a look there. we're seeing very strong demand for this feature so we might add it to our roadmap soon
k
Thanks for linking this 🙌 Is there a quick and easy solution to add the typing to the existing kedro project? I can think of just adding a node that takes a subset of parameters and parses it to e.g. pydantic - basically transforming untyped parameters to typed catalog entry. But maybe there is a different / better way?
s
Hi @Kacper Leśniara your approach does sound like the quick and easy way to do this. For maybe a more robust approach you could also try to validate configuration parameters as they are loaded by updating the
settings.py
to use
OmegaConfigLoader
and validate parameters using the model there. You'll then have to update the nodes to use these typed parameters.
k
Hi @Sajid Alam. I'm already using
OmegaConfigLoader
, what do you mean by "validate parameters using the model there"?
s
What i meant was creating a custom context, and doing something like this in your settings.py:
Copy code
from pydantic import ValidationError
from YOUR_PYDANTIC_MODEL import MyConfig

def load_and_validate_parameters(config_loader):
    config_data = config_loader.get("parameters*")
    try:
        return MyConfig(**config_data)
    except ValidationError as e:
        raise ValueError(f"Invalid configuration: {e}")
Copy code
from kedro.framework.context import KedroContext

class ProjectContext(KedroContext):
    def _load_parameters(self):
        config_loader = self.config_loader
        return load_and_validate_parameters(config_loader)
CONTEXT_CLASS = ProjectContext
k
Interesting! Thanks for sharing that