Skip to content

prefect.runtime.flow_run

Access attributes of the current flow run dynamically.

Note that if a flow run cannot be discovered, all attributes will return empty values.

You can mock the runtime attributes for testing purposes by setting environment variables prefixed with PREFECT__RUNTIME__FLOW_RUN.

Available attributes
  • id: the flow run's unique ID
  • tags: the flow run's set of tags
  • scheduled_start_time: the flow run's expected scheduled start time; defaults to now if not present
  • name: the name of the flow run
  • flow_name: the name of the flow
  • parameters: the parameters that were passed to this run; note that these do not necessarily include default values set on the flow function, only the parameter values explicitly passed for the run
  • parent_flow_run_id: the ID of the flow run that triggered this run, if any
  • parent_deployment_id: the ID of the deployment that triggered this run, if any
  • run_count: the number of times this flow run has been run

__getattr__(name)

Attribute accessor for this submodule; note that imports also work with this:

from prefect.runtime.flow_run import id
Source code in src/prefect/runtime/flow_run.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def __getattr__(name: str) -> Any:
    """
    Attribute accessor for this submodule; note that imports also work with this:

        from prefect.runtime.flow_run import id
    """

    func = FIELDS.get(name)

    # if `name` is an attribute but it is mocked through environment variable, the mocked type will be str,
    # which might be different from original one. For consistency, cast env var to the same type
    env_key = f"PREFECT__RUNTIME__FLOW_RUN__{name.upper()}"

    if func is None:
        if env_key in os.environ:
            return os.environ[env_key]
        else:
            raise AttributeError(f"{__name__} has no attribute {name!r}")

    real_value = func()
    if env_key in os.environ:
        mocked_value = os.environ[env_key]
        # cast `mocked_value` to the same type as `real_value`
        try:
            cast_func = type_cast[type(real_value)]
            return cast_func(mocked_value)
        except KeyError:
            raise ValueError(
                "This runtime context attribute cannot be mocked using an"
                " environment variable. Please use monkeypatch instead."
            )
    else:
        return real_value