<@ULVA73B9P> I have a task that take a param, and I want to use this param to name the task run... W...
a
@Marvin I have a task that take a param, and I want to use this param to name the task run... What is the more idiomatic way to do that?
m
thought for 72.9 seconds
The idiomatic way in Prefect 3.x is to use
task_run_name=
on the
@task
decorator. For a task parameter, the simplest pattern is:
Copy code
python
from prefect import task

@task(task_run_name="process-{customer_id}")
def process_customer(customer_id: str):
    ...
If you call:
Copy code
python
process_customer("abc-123")
the task run name will be
process-abc-123
. Recommended approach - Use a string template when the name is just based on task parameters - Use a callable if you need more logic Example with a callable:
Copy code
python
from prefect import task

def make_name(parameters):
    return f"process-{parameters['customer_id']}"

@task(task_run_name=make_name)
def process_customer(customer_id: str):
    ...
A couple of notes: -
task_run_name
names the task run, not the task definition itself - The template uses task function parameters, so
{customer_id}
must match the argument name exactly - This is the most idiomatic Prefect 3.x solution Docs/code-backed behavior from Prefect 3.x:
task_run_name
supports either a string template or a callable. If you want, I can also show the equivalent pattern for flow run names using
flow_run_name=
.
a
And how to make a task fail without trowing an exception?
@Marvin
m
thought for 7.3 seconds
Example for flow runs:
Copy code
python
from prefect import flow

@flow(flow_run_name="etl-{dataset}")
def etl(dataset: str):
    ...

etl("customers")
That creates a flow run named
etl-customers
. And together with a task:
Copy code
python
from prefect import flow, task

@task(task_run_name="extract-{table}")
def extract(table: str):
    ...

@flow(flow_run_name="pipeline-{table}")
def pipeline(table: str):
    extract(table)

pipeline("orders")
This gives you: - flow run name:
pipeline-orders
- task run name:
extract-orders
If you need more complex naming logic,
flow_run_name
can also be a callable, similar to
task_run_name
. Relevant docs/source-backed behavior: - Prefect supports dynamic
task_run_name
and
flow_run_name
via templates/callables in 3.x - Prefect docs: https://docs.prefect.io/ If you'd like, I can give you the exact pattern for your task signature.