Skip to content

Rendering custom map index before task is run. #39118

Description

@tomrutter

Description

The map_index_template field on a task is used to render a custom name for each individual sub task in a mapped task. It is generated by rendering the task context on the jinja template provided. This rendering happens after the task is completed so it does not appear during or before the task run. The new feature would be to attempt to render and display this field before the task is run (using the context available at the time), with a suitable failback if this is not possible with the context values available.

Use case/motivation

It would be useful to see the custom map index during task runs, when the context information to do so is available. The mapped task index can be used to provide human readable information to distinguish the mapped tasks from one another and would make it easier to track task progress during a dag run.

Related issues

Various issues and PRs have dealt with displaying the custom map index when the task fails.

#39065, #39092, #38902, #39087

Are you willing to submit a PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. tomrutter commented on Apr 18, 2024

    @tomrutter
    ContributorAuthor

    This is a follow on from #39065 with a narrower scope (just rendering while the task is running) as the other items in that issue have been or will be resolved by the other issues/PRs referenced.

  2. raphaelauv commented on Apr 19, 2024

    @raphaelauv
    Contributor

    related : #39092

  3. RNHTTR commented on Apr 26, 2024

    @RNHTTR
    Contributor

    @tomrutter Would you like to be assigned?

    Given the current design (using a task's context object means we have to wait for the task to start running), I think this might be trickier than meets the eye.

  4. raphaelauv commented on Apr 26, 2024

    @raphaelauv
    Contributor

    since we can define at operator definition the field map_index_template="{{ task.op_kwargs['date'] }}",, it can be render by the webserver

  5. tomrutter commented on Apr 27, 2024

    @tomrutter
    ContributorAuthor

    Happy to be assigned, but if someone has a clear idea, happy for that to go ahead too.

    I’m guessing the big issue with rendering on the webserver is picking up the context added by the task. I’d start with that for now though with failover to rendered value from the task if that exists.

  6. raphaelauv commented on Jun 22, 2024

    @raphaelauv
    Contributor

    Hi @tomrutter are you working on ?

  7. DonHaul commented on Dec 11, 2024

    @DonHaul
    Contributor

    since we can define at operator definition the field map_index_template="{{ task.op_kwargs['date'] }}",, it can be render by the webserver

    this solution does not work if the task is run in a PythonVirtualEnv task. The following wont work:

    @task.virtualenv(
                requirements=["inspire-schemas"],
                system_site_packages=False,
                map_index_template="{{task.op_kwargs['date']}}",
            )`
    

    Is there a workaround for this specific case?

  8. RNHTTR commented on Dec 11, 2024

    @RNHTTR
    Contributor

    I'm not sure, and I'm not sure if it's safe to handle the rendering on the webserver. @uranusjr Do you think it will be straightforward to implement named mapped tasks before the task completes in Airflow 3?

  9. set92 commented on May 9, 2025

    @set92

    Any news on it?

  10. uranusjr commented on May 15, 2025

    @uranusjr
    Member

    I thought I already commented on this, but maybe I was just dreaming, or the message is lost in the ether. I don’t think it’s really viable to render anywhere else than in the worker. I’m thinking, maybe we should just make the map index on UI editable, so the user can manually set the value to anything they want? Either before or after the task is run.

  11. foldvari commented on Jun 3, 2025

    @foldvari

    I'd be happy with an even simpler option: let me refer to a parameter passed to the task/operator in the template, and use that before the execute method is called. This way I'd see the named mapping as soon as my task/operator starts running.

    At the moment I do not see any way to refer to a passed parameter to a classic operator. In case of a @task I can do this, but still it does not get evaluated until the task finished: map_index_template='{{ task.op_kwargs["number"] }}'

  12. kaxil commented on Oct 24, 2025

    @kaxil
    Member

    One solution: #57208

  13. raphaelauv commented on Oct 26, 2025

    @raphaelauv
    Contributor

    Thanks @kaxil

    I guess we can also close #39092 ?

  14. added a commit that references this issue on Oct 26, 2025
    5a5dc4f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions