<@ULVA73B9P> by default, if a subflow fails, will ...
# ask-marvin
k
@Marvin by default, if a subflow fails, will the parent flow fail? if so, describe how to override this behavior
m
By default, if a subflow fails, the parent flow will also fail. To override this behavior, you can use exception handling within your flow. Below is an example code snippet demonstrating how to handle subflow failures gracefully:
Copy code
python
from prefect import flow, task

@task
def my_task():
    raise ValueError("An error occurred!")

@flow
def my_subflow():
    my_task()

@flow
def my_parent_flow():
    try:
        my_subflow()
    except Exception as e:
        print(f"Subflow failed with error: {e}")
        # Handle failure or continue execution

if __name__ == "__main__":
    my_parent_flow()
This ensures that the parent flow catches the exception and handles it without failing entirely. For more details, you can check Prefect documentation or useful examples at this link.
n
hey @KG in general for flows or tasks you call from a parent flow you can add
return_state=True
to trap failed states like
Copy code
from prefect import flow, task

@task
def bad():
  raise ValueError("asdf")

@flow
def ok_with_failure():
  state = bad(return_state=True)
  assert state.is_failed()
  return "its fine"

ok_with_failure()
Copy code
...
12:08:54.113 | INFO    | Flow run 'fiery-jackal' - Finished in state Completed()
Out[3]: 'its fine'