Hi Everyone, I have a question about conditionals,...
# ask-the-community
y
Hi Everyone, I have a question about conditionals, I trying to implement a workflow with a task that is ran if an input flag is set to True, otherwise this task is skipped. I attempted to use conditional for this, but it seems like the workflow terminates after executing the conditional and does not carry on to next tasks. Code:
Copy code
import logging
import time
import typing
import uuid
from typing import Tuple

from flytekit import task, workflow, conditional

logger = logging.getLogger()

@task
def create_dataset() -> uuid.uuid4:
    print("Creating dataset")
    time.sleep(1)
    dataset_id = uuid.uuid4()
    print(f"Dataset created with id {dataset_id}")
    return dataset_id

@task
def sort_dataset(dataset_id: uuid.uuid4) -> None:
    print(f"Sorting dataset with id {dataset_id}")
    time.sleep(1)
    print(f"Dataset is sorted")

@task
def train_model(dataset_id: str) -> uuid.uuid4:
    print(f"Training model using dataset with id {dataset_id}")
    model_id = uuid.uuid4()
    time.sleep(1)
    print(f"Trained model with id {model_id}")
    return model_id

@workflow
def train_workflow(sort_dataset_flag: bool) -> None:
    dataset_id = create_dataset()
    conditional("Sorting").if_(sort_dataset_flag.is_true()).then(sort_dataset(dataset_id=dataset_id))
    train_model(dataset_id=dataset_id)

def test_workflow():
    train_workflow(sort_dataset_flag=True)
Output:
Copy code
Creating dataset
Dataset created with id bba83be5-8b15-417d-bd48-5cce55d1856f
Sorting dataset with id bba83be5-8b15-417d-bd48-5cce55d1856f
Dataset is sorted
Notice how last task is not executed. How can I make this work?
s
What's your flytekit version?
y
v1.1.1
s
Can you install the latest? flytekit==1.3.2. Also, I think you need to specify
else
in conditional. Not sure why you aren't seeing an error, though. cc @Kevin Su
y
I can get it to work using a do_nothing task in the else branch, is it necessary to have this non-task task to accomplish a conditional with only one actionable outcome?
s
Yeah, it is for now. We've been contemplating a workaround if there's only one outcome. cc @Niels Bantilan
y
Thanks, I would appreciate this workaround but for now, I can make do with the do_nothing task 🙂
n
@Samhita Alla do we have an existing issue for the workaround?
s
Hey @Yubo Wang! Have you had a chance to create an issue for this? Reference Slack thread: https://flyte-org.slack.com/archives/CP2HDHKE1/p1676300832112329?thread_ts=1675811803.560819&cid=CP2HDHKE1
y
oh I never did
I can create one if needed @Samhita Alla
s
Yeah, please do, @Yubo Wang!
y
https://github.com/flyteorg/flyte/issues/3533 sorry for the delay, here is the issue