<@ULVA73B9P> In an automation, I have the followin...
# ask-marvin
t
@Marvin In an automation, I have the following expect entry to trigger another flow run.
Copy code
"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?
m
thought for 209.4 seconds
Short answer: there isn’t a built-in “terminal state” condition in Automations. Event triggers match on event topics, and there’s no alias like terminal you can use. Your options: - Enumerate the terminal flow-run events explicitly. The canonical terminal events are: -
prefect.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:
Copy code
"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:
Copy code
"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.
t
@Marvin Thanks. Can I work with state types instead of state names?
m
thought for 220.1 seconds
Yes. In Prefect 3.x you can (and generally should) work with state types instead of names. - Programmatic checks - Every
State
has a
type
(a
StateType
enum) and convenience helpers:
Copy code
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:
Copy code
from prefect.client.schemas.objects import TERMINAL_STATES
    s.type in TERMINAL_STATES      # or s.is_final()
- Filtering runs by state type
Copy code
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.
t
@Marvin Yes but what about this in the context of the expect field in an automation config?
m
thought for 96.8 seconds
Great question. In automations, the
trigger.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
Copy code
# 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)