<@ULVA73B9P> I would like to use flow parameters n...
# ask-marvin
s
@Marvin I would like to use flow parameters naming my flow run. When I use a function to define my flow name, I can call get_run_context().flow_run.parameters to get the parameters map, but this only includes the ones explicitly passed in from my deployment. Default arguments in Python are not included. Is there a way to get these as well when naming a flow run?
m
thought for 309.4 seconds
Short answer: the parameters attached to the flow run only include what was explicitly provided when the run was created. Prefect does not populate Python default argument values into
get_run_context().flow_run.parameters
, so they won’t appear there automatically. Two ways to get what you want: 1) Declare defaults in your deployment - If you put your default values into the deployment’s parameters, they’ll be present on the flow run and show up in
get_run_context().flow_run.parameters
. - This is simple but requires duplicating defaults in the deployment config. 2) Compute defaults inside your naming function (recommended) - You can introspect the flow function signature and “apply defaults” to merge the run’s provided parameters with the Python defaults. Here’s a helper you can drop into your flow module:
Copy code
import inspect
from prefect import flow, get_run_context

def params_with_defaults():
    ctx = get_run_context()
    # Get the underlying Python function for the running flow
    # In Prefect 3.x, ctx.flow.fn references the original function
    fn = getattr(ctx.flow, "fn", None) or ctx.flow
    ba = inspect.signature(fn).bind_partial(**(ctx.flow_run.parameters or {}))
    ba.apply_defaults()
    return dict(ba.arguments)

def name_from_params():
    p = params_with_defaults()
    # build whatever name you want from p (which now includes defaults)
    return f"my-flow-a={p.get('a')}-b={p.get('b')}"

@flow(flow_run_name=name_from_params)
def my_flow(a: int = 1, b: str = "x"):
    ...
Notes: -
bind_partial(...).apply_defaults()
fills in any missing arguments with the function’s Python defaults. - This avoids having to repeat defaults in your deployment and works reliably for naming. About string templates for flow_run_name: - If you use a format string like
flow_run_name="{a}-{b}"
, the placeholders are filled from the run’s parameter dict only. Values that exist only as Python defaults won’t be available for substitution. If you need defaults, prefer the callable approach above. Docs for reference: - Flow decorator (flow_run_name): prefect.flow - Run context: get_run_context If you share a snippet of your flow signature and naming function, I can tailor the helper to your exact setup.