Slackbot
11/29/2025, 2:26 PMMarvin
11/29/2025, 2:26 PMMarvin
11/29/2025, 2:29 PMFlow class does not implement __enter__/__exit__), so with flow(...): isn’t supported.
Supported ways to define flows:
- Use the decorator (recommended)
from prefect import flow, task
@task
def do_thing(x):
return x + 1
@flow(name="my-flow")
def my_flow(n: int):
return do_thing(n)
my_flow(5)
- Create a flow programmatically
from prefect.flows import Flow
def segment(n: int):
return n + 1
segment_flow = Flow(segment, name="segment-flow")
segment_flow(5)
- For “portions” of code as their own flow, define a separate function and decorate it as a subflow, then call it from another flow:
from prefect import flow
@flow(name="subflow")
def sub():
...
@flow
def parent():
sub() # runs as a nested flow run
Docs:
- Flows concepts
- Write and run workflows
- Flow API reference
If you can share what you wanted to achieve with a context manager (e.g., scoping a block of code, dynamic/inline flow creation, nesting), I can suggest the closest Prefect 3 pattern.