pythonkedro

Kedro: How to pass "list" parameters from command line?


I'd like to control kedro parameters via command line.

According to docs, kedro can specify runtime parameters as follows:

kedro run --params key:value
> {'key': 'value'}

It works. In the same way, I try to specify list parameters like this:

kedro run --params keys:['value1']
> {'keys': '[value1]'}

It doesn't work because kedro interplets not list but str. Probably, this answer could be related.

Hope to mention a couple things to make kedro evaluates list parameters like yaml.


Solution

  • By default the kedro command line won't typecast parameters beyond simpler numeric types. More complicated parameters should be handled via the parameters.yml file.

    That said, if you really want to do this, you can modify your kedro_cli.py to support this. Specifically, you'd want to modify the _split_params callback function in the file. The easiest thing here would likely be to change the line that reads

    result[key] = _try_convert_to_numeric(value)
    

    which handles parsing simple numeric types to

    result[key] = json.loads(value)
    

    to make it parse a wider range of types. That is, parse the CLI parameter you pass in as json (so you'll also need to be mindful of quotes and making sure that you pass in valid json syntax.

    If that doesn't work, you can try adding your own syntax and parsing it in that function. However, my recommendation is to avoid depending on fragile string parameter evaluation from CLI and use the parameters.yml instead.