Theom
04/02/2026, 4:39 PM"expect": [
"prefect.flow-run.Cached",
"prefect.flow-run.Completed",
"prefect.flow-run.Failed",
"prefect.flow-run.Crashed",
"prefect.flow-run.TimedOut"
],
Is it possible to have a more general "terminal state" condition?Marvin
04/02/2026, 4:39 PMMarvin
04/02/2026, 4:42 PMprefect.flow-run.Completed
- prefect.flow-run.Failed
- prefect.flow-run.Crashed
- prefect.flow-run.Cancelled
- Keep (optional) name variants if you care about them specifically:
- prefect.flow-run.TimedOut (a Failed-type state with a custom name)
- prefect.flow-run.Cached (a Completed-type state with a custom name)
So your list should at least include Cancelled, which is missing right now:
"expect": [
"prefect.flow-run.Completed",
"prefect.flow-run.Failed",
"prefect.flow-run.Crashed",
"prefect.flow-run.Cancelled",
"prefect.flow-run.TimedOut", // optional, name variant
"prefect.flow-run.Cached" // optional, name variant
]
- If you’re okay with matching any flow-run state (not just terminal), you can use a wildcard:
"expect": ["prefect.flow-run.*"]
Note: this will also match non-terminal events like Running, Scheduled, etc.
There isn’t currently a shorter pattern that safely matches only terminal events without enumerating them.Theom
04/03/2026, 2:29 PMMarvin
04/03/2026, 2:29 PMMarvin
04/03/2026, 2:33 PMState has a type (a StateType enum) and convenience helpers:
from prefect.client.schemas.objects import StateType
from prefect.states import Completed
s = Completed()
s.type == StateType.COMPLETED # True
s.is_completed() # True (recommended)
- Terminal checks:
from prefect.client.schemas.objects import TERMINAL_STATES
s.type in TERMINAL_STATES # or s.is_final()
- Filtering runs by state type
from prefect import get_client
from prefect.client.schemas.objects import StateType
from prefect.client.schemas.filters import (
FlowRunFilter, FlowRunFilterState, FlowRunFilterStateType
)
async with get_client() as client:
runs = await client.read_flow_runs(
flow_run_filter=FlowRunFilter(
state=FlowRunFilterState(
type=FlowRunFilterStateType(any_=[StateType.COMPLETED, StateType.FAILED])
)
)
)
- Automations
- Actions that change run state accept StateType (e.g., marking “zombie” flows as CRASHED). If you’re targeting automations specifically, I can share a minimal example for your setup.
References
- StateType enum (API reference)
- FlowRunFilterStateType (API reference)
- API client guide
- Detect zombie flows (automation example)
If you had a specific place in mind (filters, automations, custom logic), tell me and I’ll tailor the snippet.Theom
04/03/2026, 2:39 PMMarvin
04/03/2026, 2:39 PMMarvin
04/03/2026, 2:40 PMtrigger.expect field matches on event names (strings), not state types.
- What to put in expect
- Use event names like prefect.flow-run.Completed, prefect.flow-run.Failed, etc.
- You can use trailing wildcards, e.g. prefect.flow-run.*
- Do not use StateType enums (e.g., StateType.COMPLETED) or bare type strings like COMPLETED
- Naming pattern
- Flow runs: prefect.flow-run.{StateName}
- Task runs: prefect.task-run.{StateName}
- StateName is capitalized (e.g., Completed, Failed, Cancelled, Crashed, Running, Scheduled, Pending, Paused, Suspended, Late, Cancelling)
- Examples
# React to any flow-run state change
expect:
- prefect.flow-run.*
# React only to failures and crashes
expect:
- prefect.flow-run.Failed
- prefect.flow-run.Crashed
# React to terminal outcomes
expect:
- prefect.flow-run.Completed
- prefect.flow-run.Failed
- prefect.flow-run.Cancelled
- prefect.flow-run.Crashed
# For task runs instead of flow runs
expect:
- prefect.task-run.Failed
If you share the exact behavior you’re targeting, I can suggest the minimal expect patterns for it.
References
- Automations concepts
- Creating automations
- EventTrigger (API reference)
- Flow run events (list of event names)