This message was deleted.
# ask-marvin
s
This message was deleted.
m
thought for 182.6 seconds
Short answer: no. In Prefect 3.x there isn’t a context-manager API for flows (the
Flow
class does not implement
__enter__/__exit__
), so
with flow(...):
isn’t supported. Supported ways to define flows: - Use the decorator (recommended)
Copy code
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
Copy code
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:
Copy code
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.