Florian Giroud
10/22/2025, 4:22 PMMarvin
10/22/2025, 4:22 PMMarvin
10/22/2025, 4:27 PM@flow decorator (keyword-only). Defaults shown as in the API:
@flow(
name=None,
description=None,
version=None,
flow_run_name=None,
task_runner=None,
timeout_seconds=None,
validate_parameters=True,
persist_result=None,
result_storage=None,
result_serializer=None,
cache_policy=None,
cache_expiration=None,
cache_key_fn=None,
on_completion=None,
on_failure=None,
on_cancellation=None,
on_crashed=None,
on_running=None,
retries=None,
retry_delay_seconds=None,
retry_jitter_factor=None,
log_prints=None,
)
Brief descriptions:
- name: Optional custom name for the flow (defaults to function name)
- description: Text description (defaults from function docstring if present)
- version: Optional version string for this flow
- flow_run_name: String or callable to name individual flow runs
- task_runner: Task runner to execute tasks (e.g., thread/process-based)
- timeout_seconds: Max runtime in seconds for the flow
- validate_parameters: Whether to validate/coerce parameters (default True)
- persist_result: Whether to persist the flow’s result (inherits setting if None)
- result_storage: Storage location/block for persisted results
- result_serializer: Serializer to use for results
- cache_policy: Policy controlling flow-level result caching
- cache_expiration: How long a cached result is valid
- cache_key_fn: Function to compute a cache key from inputs
- on_completion: List of hooks to run when the flow completes
- on_failure: List of hooks to run when the flow fails (after retries)
- on_cancellation: List of hooks to run when the flow is cancelled
- on_crashed: List of hooks to run when the flow crashes
- on_running: List of hooks to run when the flow enters Running
- retries: Number of times to retry the flow on failure
- retry_delay_seconds: Delay between retries (can be a number or sequence)
- retry_jitter_factor: Adds jitter to retry delays
- log_prints: Capture print() as logs (inherits setting if None)
References:
- API reference for flows: prefect.flows API
- State change hooks: State change hooks
- Results and persistence: Results and persistence
- Retries: RetriesFlorian Giroud
10/22/2025, 4:33 PMFlorian Giroud
10/22/2025, 4:33 PMMarvin
10/22/2025, 4:33 PMMarvin
10/22/2025, 4:37 PMprefect.flow-run.Failed event and sends a notification or runs an action. You can scope it to all flows in the workspace or filter by things like deployment, work pool, etc.
- No code changes to your flows are required.
- Manage via the Cloud UI (create/enable/disable) or API. The CLI can list/inspect/enable/disable, but not create.
- Docs:
- Creating automations
- Event triggers
- State change hooks (concepts behind flow hooks)
2) Repo-level wrapper around @flow (code-based, works anywhere)
- Define your failure handler once and wrap Prefect’s @flow to inject on_failure for every flow you define in your repo.
Example:
# global_flow.py
from prefect import flow
from prefect.flows import Flow
async def notify_failure(flow: Flow, flow_run, state):
# Your global handler logic here (send Slack, log, etc.)
print(f"[GLOBAL FAILURE] {flow.name} -> {state.type}: {state.message}")
def global_flow(*, on_failure=None, **kwargs):
# Ensure our global hook is always included (and preserve any flow-specific hooks)
hooks = [notify_failure, *(on_failure or [])]
return flow(on_failure=hooks, **kwargs)
# usage in your flows
from .global_flow import global_flow
@global_flow(name="my_flow")
def my_flow():
...
Notes:
- on_failure hooks run only after all retries are exhausted and the flow enters a final Failed state.
- If you’re on Prefect OSS (no Cloud), use the wrapper approach; if you’re on Cloud, Automations are the simplest “global” solution with no code changes.
- CLI reference for Automations (manage-only): prefect automation --help
If you tell me whether you’re on Prefect Cloud or OSS, and whether you want notifications (Slack/Email) or to run custom logic, I can give you a tailored setup.