<@ULVA73B9P> Using run_deployment() in a flow how ...
# ask-marvin
c
@Marvin Using run_deployment() in a flow how can I run a deployment and then return the final flow run state to the parent flow run
m
thought for 218.5 seconds
n
tdlr
run_deployment
returns a
FlowRun
object that has a
state
attribute
m
Great question. In Prefect 3.x,
prefect.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:
Copy code
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)?
n
@Marvin
wait_for_flow_run
not necessary,
run_deployment
blocks until the flow run is completed by default, unless you say
timeout=0
m
thought for 88.8 seconds
You’re right — in Prefect 3.x,
run_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:
Copy code
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: States
c
thanks @Nate
n
catjam
c
your job is safe from AI
n
hopefully the "spot-checking SDK QA" part of it is not! i need to rewrite marvin again lol