Nicholas Pini
11/29/2025, 2:27 PMwith flow(name="flow_name"):
# do stuff as if it was defined in a flow named "flow_name"Marvin
11/29/2025, 2:27 PMMarvin
11/29/2025, 2:32 PMwith flow(...):. The Flow class doesn’t implement __enter__/__exit__.
Supported patterns:
- Decorator (recommended)
from prefect import flow
@flow(name="flow_name")
def my_flow():
# do stuff
...
- Programmatic Flow creation
from prefect.flows import Flow
def do_stuff():
...
my_flow = Flow(fn=do_stuff, name="flow_name")
my_flow()
- Subflows (to “group” parts of a workflow with their own runs/observability)
from prefect import flow
@flow
def parent():
child()
@flow(name="flow_name")
def child():
# do stuff
...
Docs:
- Flows concept
- Flow API reference
If you share what you were hoping to achieve with a context manager (e.g., grouping a section of logic as a separately tracked run), I can suggest the closest pattern (often a subflow) with a tailored example.