Hi, how could I retry a failed task from the UI? O...
# ask-community
t
Hi, how could I retry a failed task from the UI? Or skip a failed task and continue the rest of the flow?
b
Hi Tri! You can retry a flow run from the UI, but not a task run. You can, however, set "n" number of retries on the task itself (ie
@task(retries=3)
) . If you want to skip a failed task and continue on with the flow, you could: a) Use a try/except block within your flow to catch and handle task failures.
Copy code
from prefect import flow, task

@task
def my_task():
    raise ValueError()

@flow
def my_flow():
    try:
        my_task()
    except ValueError:
        print("Oh no! The task failed.")

    return True

my_flow()
b) Use the
return_state=True
option when calling a task, which allows you to check the task's state and decide how to proceed.
Copy code
from prefect import flow, task

@task
def my_task():
    raise ValueError()

@flow
def my_flow():
    state = my_task(return_state=True)

    if state.is_failed():
        print("Oh no! The task failed. Falling back to '1'.")
        result = 1
    else:
        result = state.result()

    return result + 1

result = my_flow()
assert result == 2