<@ULVA73B9P> is there a way to define the name of ...
# ask-marvin
a
@Marvin is there a way to define the name of the task ?
m
thought for 45.9 seconds
Yes — in Prefect 3.x you can define a task’s name. Static task name Use the
name
argument on `@task`:
Copy code
python
from prefect import task

@task(name="My custom task")
def do_work():
    return 1
If you don’t set it, Prefect uses the Python function name. Dynamic task run name If you want each execution of the task to have a custom name, use `task_run_name`:
Copy code
python
from prefect import flow, task

@task(name="process-order", task_run_name="process-order-{order_id}")
def process(order_id: int):
    return order_id

@flow
def my_flow():
    process(123)
That gives the task definition the name
process-order
, and the run a name like
process-order-123
. You can also use a callable for
task_run_name
if you need more control. So in short: -
name=
→ names the task itself -
task_run_name=
→ names each task run If you want, I can show the exact pattern for your use case.
a
@Marvin this is how I use, orderid will be in config , how it have the task names
@task
def execute_api(config):
api.get_api_data(config)
m
thought for 11.5 seconds
Yes — you can use
task_run_name
and reference fields from
config
. For your example:
Copy code
python
from prefect import task

@task(task_run_name="execute-api-{config[orderid]}")
def execute_api(config):
    api.get_api_data(config)
If
config
is:
Copy code
python
{"orderid": 123}
then the task run name will be:
Copy code
python
execute-api-123
A slightly safer option is to use a callable, especially if
orderid
might be missing:
Copy code
python
from prefect import task

def make_task_name(parameters):
    config = parameters.get("config", {})
    order_id = config.get("orderid", "unknown")
    return f"execute-api-{order_id}"

@task(task_run_name=make_task_name)
def execute_api(config):
    api.get_api_data(config)
That avoids errors if
config["orderid"]
is absent. So your task definition can stay:
Copy code
python
@task(name="execute_api")
or just
@task
, but for per-order names, use
task_run_name
. If you want, I can also show how this appears when the task is called from a flow.