<@ULVA73B9P> In Prefect 3, is it possible to defin...
# ask-marvin
n
@Marvin In Prefect 3, is it possible to define a portion of code as a flow using a context manager? Something like
Copy code
with flow(name="flow_name"):
    # do stuff as if it was defined in a flow named "flow_name"
m
thought for 305.5 seconds
Short answer: No—Prefect 3 does not support defining a flow via a context manager like
with flow(...):
. The
Flow
class doesn’t implement
__enter__/__exit__
. Supported patterns: - Decorator (recommended)
Copy code
from prefect import flow

  @flow(name="flow_name")
  def my_flow():
      # do stuff
      ...
- Programmatic Flow creation
Copy code
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)
Copy code
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.