<@U06RKAMK0UC> I am returning 2 variables from my ...
# ask-ai
m
@ancient-wolf-19325 I am returning 2 variables from my function and I am accepting them both as input for my next task in the workflow. I get an error o0 is a Promise. Promise objects are not iterable - can't range() over a promise. But you can use [index] or the alpha version of @eager workflows. With an example help me in accessing both output values in my workflow.
j
In Flyte, when a task returns multiple outputs, these outputs are encapsulated in
Promise
objects. You cannot directly iterate over these promises as you would with regular Python objects. Instead, you need to access each output individually by indexing into the promise. Here's an example to illustrate how you can handle multiple outputs from a task and use them as inputs for another task in a workflow:
Copy code
from flytekit import task, workflow
from typing import Tuple

@task
def task1(a: int) -> Tuple[int, str]:
    return a + 1, "hello"

@task
def task2(x: int, y: str) -> str:
    return f"{y} {x}"

@workflow
def my_workflow(a: int) -> str:
    x, y = task1(a=a)  # Access the outputs individually
    result = task2(x=x, y=y)
    return result

if __name__ == "__main__":
    print(my_workflow(a=5))
In this example,
task1
returns two outputs, an integer and a string. These outputs are accessed individually using tuple unpacking (
x, y = task1(a=a)
) and then passed as inputs to
task2
. Would you like more detail on any specific part? Sources: - Flyte documentation on task execution inside a workflow 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
m
@ancient-wolf-19325 I did exact same as described above to unpack outputs, but mine are file_path and directory_path during the unpack it still throws the error
j
It seems likely that the issue arises from handling
FlyteFile
and
FlyteDirectory
types, which may require specific handling when used as outputs in Flyte workflows. When dealing with these types, ensure that you are correctly accessing and using them in your workflow. Here's an example to demonstrate how you can handle
FlyteFile
and
FlyteDirectory
outputs:
Copy code
from flytekit import task, workflow
from flytekit.types.file import FlyteFile
from flytekit.types.directory import FlyteDirectory
from typing import Tuple

@task
def task1() -> Tuple[FlyteFile, FlyteDirectory]:
    file_path = FlyteFile("/path/to/file.txt")
    directory_path = FlyteDirectory("/path/to/directory")
    return file_path, directory_path

@task
def task2(file: FlyteFile, directory: FlyteDirectory) -> str:
    # Example operation using the file and directory
    return f"Processed {file.path} and {directory.path}"

@workflow
def my_workflow() -> str:
    file, directory = task1()  # Unpack the outputs
    result = task2(file=file, directory=directory)
    return result

if __name__ == "__main__":
    print(my_workflow())
In this example,
task1
returns a
FlyteFile
and a
FlyteDirectory
. These are unpacked and passed to
task2
, which processes them. Ensure that the paths are correctly specified and accessible in your environment. Would you like more detail on handling
FlyteFile
and
FlyteDirectory
? Sources: - Flyte documentation on task execution inside a workflow 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.