<@U06RKAMK0UC> Flytekit does not support Unary exp...
# ask-ai
m
@ancient-wolf-19325 Flytekit does not support Unary expressions or performing truth value testing, This is a limitation in python. For logical use 'and\or' instead. I want to return a boolean from a task which is status of the task if it got completed or failed back to workflow to decide if next task should execute or abort
j
In Flyte, you can handle boolean outputs from tasks and use them in conditionals within workflows. However, Flytekit does not support unary operations directly on promises, which are placeholders for task outputs. Instead, you should use methods like
is_true
,
is_false
, and
is_none
to evaluate boolean promises in conditionals. Here's how you can achieve this: 1. Task Definition: Define a task that returns a boolean indicating success or failure. 2. Workflow Conditional: Use the
conditional
construct in the workflow to decide the next steps based on the boolean output. Here's an example:
Copy code
from flytekit import task, workflow, conditional

@task
def check_status() -> bool:
    # Logic to determine if the task was successful
    return True  # or False based on your logic

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

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

@workflow
def decision_workflow() -> str:
    status = check_status()
    return (
        conditional("status_check")
        .if_(status.is_true())
        .then(success_task())
        .else_()
        .then(failure_task())
    )
In this example,
check_status
returns a boolean, and the workflow uses a conditional to decide which task to execute next based on the boolean value. Would you like more details on this or other potential solutions? Sources: - Flyte Conditionals 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
m
@ancient-wolf-19325 For above it provides following error. Flytekit only supports Comparison(<,<=,>,>=) or Conjunction expressions, Received var <bound method Promise.is_true of Promise(node:n5.o0.[]> in condition_status_check.if_
j
The error you're encountering is due to the fact that Flytekit's conditionals require comparison or conjunction expressions, and the
is_true
method is not directly supported in the way you are using it. Instead, you should use comparison operators to evaluate the boolean promise. Here's how you can modify your workflow to use a comparison expression:
Copy code
from flytekit import task, workflow, conditional

@task
def check_status() -> bool:
    # Logic to determine if the task was successful
    return True  # or False based on your logic

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

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

@workflow
def decision_workflow() -> str:
    status = check_status()
    return (
        conditional("status_check")
        .if_(status == True)  # Use comparison operator
        .then(success_task())
        .else_()
        .then(failure_task())
    )
In this example, the
if_
condition uses a comparison operator (
==
) to evaluate the boolean promise. This should resolve the error you encountered. Would you like more details or further assistance? Sources: - Flyte Conditionals 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.