Craig Rose
11/03/2025, 1:18 AMMarvin
11/03/2025, 1:18 AMNate
11/03/2025, 1:18 AMrun_deployment returns a FlowRun object that has a state attributeMarvin
11/03/2025, 1:21 AMprefect.deployments.run_deployment() returns a FlowRun object for the triggered run. To wait for it to finish and return its final state from your parent flow, the most reliable pattern is to pair it with wait_for_flow_run.
Here are two common patterns:
- Return the child’s final State object to the parent flow
- Make the parent adopt the child’s outcome (i.e., fail the parent if the child failed)
Example:
from prefect import flow
from prefect.deployments import run_deployment
from prefect.flow_runs import wait_for_flow_run
from prefect.states import raise_state_exception
@flow
def parent():
# Trigger the deployment (it will be recorded as a subflow by default)
fr = run_deployment(
name="child-flow/deployment-name",
parameters={"x": 1},
# as_subflow=True is the default; keep it for lineage unless you need otherwise
)
# Block until the child run reaches a final state
fr = wait_for_flow_run(fr.id, log_states=True) # default timeout is 3h; pass timeout=None to wait indefinitely
# Option A: return the final State object to the caller
return fr.state
# Option B: make the parent adopt the child’s outcome
# If the child failed/crashed, this will raise and mark the parent as failed
# raise_state_exception(fr.state)
Notes:
- wait_for_flow_run returns a refreshed FlowRun with its final state.
- If you prefer to block via run_deployment alone, you can pass a timeout there as well; however, using wait_for_flow_run keeps the behavior explicit and configurable.
Docs:
- Run a deployment from code
- API: run_deployment and wait_for_flow_run
- Concepts: States
Quick clarifier: Do you want the parent flow to simply return the child’s final State, or should the parent’s own status mirror the child’s (e.g., fail if the child fails)?Nate
11/03/2025, 1:22 AMwait_for_flow_runnot necessary,
run_deployment blocks until the flow run is completed by default, unless you say timeout=0Marvin
11/03/2025, 1:22 AMMarvin
11/03/2025, 1:24 AMrun_deployment blocks by default (timeout=None) and returns once the child flow run reaches a final state.
Here’s a minimal pattern to trigger the deployment and return the child’s final state to the parent:
from prefect import flow
from prefect.deployments import run_deployment
from prefect.states import raise_state_exception
@flow
def parent():
fr = run_deployment(
name="child-flow/deployment-name",
parameters={"x": 1},
# timeout=None is the default: wait indefinitely until final state
# poll_interval=5 # adjust if needed
# as_subflow=True # default; keeps lineage
)
# fr.state is final here
return fr.state
# If you want the parent to mirror the child’s outcome:
# raise_state_exception(fr.state)
Notes:
- Set timeout=0 for non-blocking behavior (returns immediately with current state, e.g., SCHEDULED/RUNNING).
- Set timeout=<seconds> to wait up to a limit; if it’s not finished by then, you’ll still get the current (possibly non-final) state.
- as_subflow=True (default) links the child run as a subflow of the parent.
Docs:
- Run a deployment from code
- API: run_deployment
- Concepts: StatesCraig Rose
11/03/2025, 1:24 AMNate
11/03/2025, 1:24 AMCraig Rose
11/03/2025, 1:25 AMNate
11/03/2025, 1:25 AM