I apologize if this has been asked before. Is ther...
# random
t
I apologize if this has been asked before. Is there any way that I can return a dictionary from one Task and use it as the input kwargs for another task? More specifically I want a task that returns a Dictionary and I want to pass its result to another task using variable unpacking...
dict_result = some_task()
some_other_task(**dict_result)
Is this doable? Is there a more Prefect-y way to accomplish this without creating a Parameter for eahc element of the dictionary? I ultimately want to pass as an input a list of dictionaries which I can then operate over using Prefect's mapping, so being able to define the collection of arguments together is important.
k
Hey @Tim Wright, no worries but next time this would be better in the #prefect-community channel. You can’t do this because dictionary access is evaluated during flow registration time instead of flow execution time but the value does not exist there.
t
Thanks @Kevin Kho, sorry about the wrong channel.
k
So you need a task that pulls the appropriate item so that it happens during runtime, or list of items in the case of mapping
The list order is respected anyway when mapping
t
So basically a task to fetch each argument from the dictionary during runtime and then pass those explicitly to the downstream task?
k
Think like this:
Copy code
@task
def get_item(raw: List[Dict], key):
    res = []
    for r in raw:
        res.append(r[key])
    return res

with Flow(...) as flow:
    items = get_all_inputs()
    field1 = get_item(items, "field1")
    field2 = get_item(items, "field2")
    next_task.map(field1, field2)
🙌 1
t
Thats way slicker than I was thinking. Thanks @Kevin Kho
k
You can compress it too
Copy code
@task(nout=2)
def get_item(raw: List[Dict], key1, key2):
    res1 = []
    res2 = []
    for r in raw:
        res1.append(r[key1])
        res2.append(r[key2])
    return res1, res2
Copy code
with Flow(...) as flow:
    items = get_all_inputs()
    field1, field2 = get_item(items, "field1", "field2")
    next_task.map(field1, field2)
🙌 1
t
That's great. Thanks @Kevin Kho
k
Of course!
112 Views