<@ULVA73B9P> How to detect that a flow was trigger...
# ask-marvin
p
@Marvin How to detect that a flow was triggered by an automation using python? I want to know the name of the automation that started the flow
m
thought for 164.0 seconds
Yes — in Prefect 3.x you can detect this at runtime from the flow run’s
created_by
field. If a flow was started by a Prefect Cloud Automation,
created_by.type
will be
AUTOMATION
and you can read the automation’s name from
created_by.display_value
(and id from
created_by.id
). Here’s a minimal example:
Copy code
from prefect import flow, get_run_context

@flow
def my_flow():
    ctx = get_run_context()
    fr = ctx.flow_run

    if fr.created_by and getattr(fr.created_by, "type", None) == "AUTOMATION":
        automation_id = fr.created_by.id
        automation_name = fr.created_by.display_value
        print(f"Triggered by automation: {automation_name} ({automation_id})")
    else:
        print("Not triggered by an automation")

if __name__ == "__main__":
    my_flow()
Notes: - This is available during orchestrated runs (Prefect Cloud/Server). Local, ad-hoc
my_flow()
runs won’t have automation metadata. - You can access the same info from tasks via
get_run_context().flow_run.created_by
. - If you need this in child processes or external systems, pass
automation_name
as a parameter to tasks/subflows after you read it from the flow context.