在Kubeflow管道中,如何向轻量级python组件发送元素列表?

在Kubeflow管道中,如何向轻量级python组件发送元素列表?,kubeflow,kubeflow-pipelines,Kubeflow,Kubeflow Pipelines,我正在尝试将元素列表作为PipelineParameter发送到轻量级组件。 这是一个重现问题的示例。以下是函数: def my_func(my_list: list) -> bool: print(f'my_list is {my_list}') print(f'my_list is of type {type(my_list)}') print(f'elem 0 is {my_list[0]}') print(f'elem 1 is {my_list[1

我正在尝试将元素列表作为PipelineParameter发送到轻量级组件。
这是一个重现问题的示例。以下是函数:

def my_func(my_list: list) -> bool:
    print(f'my_list is {my_list}')
    print(f'my_list is of type {type(my_list)}')
    print(f'elem 0 is {my_list[0]}')
    print(f'elem 1 is {my_list[1]}')
    return True
如果我用这个来执行它:

test_data = ['abc', 'def']
my_func(test_data)
它的行为符合预期:

my_list is ['abc', 'def']
my_list is of type <class 'list'>
elem 0 is abc
elem 1 is def
然后运行管道:

import kfp

my_op = kfp.components.func_to_container_op(my_func)

@kfp.dsl.pipeline()
def my_pipeline(my_list: kfp.dsl.PipelineParam = kfp.dsl.PipelineParam('my_list', param_type=kfp.dsl.types.List())):
    my_op(my_list)

kfp.compiler.Compiler().compile(my_pipeline, 'my_pipeline.zip')
client = kfp.Client()
experiment = client.create_experiment('Default')
client.run_pipeline(experiment.id, 'my job', 'my_pipeline.zip', params={'my_list': test_data})
然后似乎在某个时候我的列表被转换成了一个字符串

my_list is ['abc', 'def']
my_list is of type <class 'str'>
elem 0 is [
elem 1 is '
我的清单是['abc','def']
我的清单是
元素0是[
元素1是'

我发现了一个变通方法,将参数序列化为json字符串。不确定这是否是最好的方法

基本功能变为:

def my_func(json_arg_str: str) -> bool:
    import json
    args = json.loads(json_arg_str)
    my_list = args['my_list']
    print(f'my_list is {my_list}')
    print(f'my_list is of type {type(my_list)}')
    print(f'elem 0 is {my_list[0]}')
    print(f'elem 1 is {my_list[1]}')
    return True
只要将参数作为json字符串而不是列表传递,它仍然有效:

测试数据=“{”我的列表“:[“abc”,“def”]}” my_func(测试数据)

这将产生预期的结果:

my_list is ['abc', 'def']
my_list is of type <class 'list'>
elem 0 is abc
elem 1 is def
这样执行时:

client = kfp.Client()
experiment = client.create_experiment('Default')
client.run_pipeline(experiment.id, 'my job', 'my_pipeline.zip', params={'json_arg_str': test_data})
产生相同的结果:

my_list is ['abc', 'def']
my_list is of type <class 'list'>
elem 0 is abc
elem 1 is def
我的清单是['abc','def']
我的清单是
元素0是abc
元素1是def

尽管它可以工作,但我仍然觉得这种解决方法很烦人。如果不允许使用PipelineParam作为列表,那么它的意义是什么呢?

目前最好的选择似乎是序列化参数。与此相关的一个问题是:

my_list is ['abc', 'def']
my_list is of type <class 'list'>
elem 0 is abc
elem 1 is def