<@U06RKAMK0UC> how can i create a workflow that ta...
# ask-ai
c
@ancient-wolf-19325 how can i create a workflow that takes the output of a task to determine if the task succeeded or failed based on a conditional
a
You can create a Flyte workflow that uses the output of a task to determine success or failure by using the conditional construct. The output (e.g., a boolean) from the first task is passed to a conditional, which then branches to a "success" or "failure" task accordingly. Use the .is_true() method on the task output within the conditional to check for success. Example:
Copy code
from flytekit import task, workflow, conditional

@task
def check_status() -> bool:
    # Your logic here
    return True

@task
def success_task() -> str:
    return "Success"

@task
def failure_task() -> str:
    return "Failure"

@workflow
def my_workflow() -> str:
    status = check_status()
    return (
        conditional("status_check")
        .if_(status.is_true())
        .then(success_task())
        .else_()
        .then(failure_task())
    )
This pattern is documented in the official Flyte conditionals guide and examples. Would you like more detail or a step-by-step explanation? Sources: - Flyte conditionals documentation - Flytesnacks conditional example 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
c
@ancient-wolf-19325 does the logic for the conditional need to be wrapped in a task? I can't have the logic to return a boolean be present in the conditional definition?
a
Yes, in Flyte workflows, the logic that produces the boolean for a conditional must be wrapped in a task. You cannot place arbitrary Python logic directly in the conditional definition; the conditional operates on the outputs (Promises) of tasks or workflow inputs, not on inline Python expressions. This is because workflow code is compiled into an execution graph, and only task outputs or workflow inputs are valid operands for conditionals, not arbitrary Python code or values. This requirement is explicitly stated in the documentation: "Within workflows, the inputs of workflow and the outputs of tasks function are promises under the hood, so you can't access and operate on them like typical Python function outputs. You can only pass promises into tasks, workflows, and other Flyte constructs. Regular Python conditionals won't work as intended in workflows: you need to use the conditional construct" (source). Would you like a step-by-step example or further explanation? Sources: - https://github.com/flyteorg/flyte/blob/master/docs/user_guide/flyte_fundamentals/tasks_workflows_and_launch_plans.md 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.