Skip to content

map_index_template that produces numbers fails #71586

Closed

Description

@sweco-sedalh

Under which category would you file this issue?

Airflow Core

Apache Airflow version

3.2.0

What happened and how to reproduce it?

I have a dynamic mapped task that uses map_index_template to make it easier to distinguish tasks in the UI.

However, when the result of map_index_template is convertible to int, Airflow/Jinja2 does that, which then fails downstream in the code.

Example that triggers a failure: map_index_template="123"

Error:

TypeError: object of type 'int' has no len()
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/task_runner.py", line 1279 in run
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/task_runner.py", line 1695 in _render_map_index

What you think should happen instead?

The value should not be coerced into an int so that map_index_template results that look like numbers are valid.

Operating System

Debian 12 (but presumably irrelevant)

Deployment

Official Apache Airflow Helm Chart

Apache Airflow Provider(s)

No response

Versions of Apache Airflow Providers

No response

Official Helm Chart version

Not Applicable

Kubernetes Version

Not Applicable

Helm Chart configuration

No response

Docker Image customizations

No response

Anything else?

I can confirm that the value produced by the template are strings. I've also tried adding | string to the template to force it to a string, but Jinja still forces a coercion to int. Adding some non-integer extra text to the template (e.g. map_index_template="id: {{ ... }}" works, but is an annoying workaround that doesn't add any additional value.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    on Aug 14, 2026
  2. removed
    needs-triage
    on Aug 15, 2026
  3. dondaum commented on Sep 15, 2026

    @dondaum
    Contributor

    I assume this only occurs when the option to render fields as native Python objects is enabled, i.e. render_template_as_native_obj=True is set:
    https://airflow.apache.org/docs/apache-airflow/stable/core-concepts/operators.html#rendering-fields-as-native-python-objects

    In this case, Jinja always attempts to render the value as a native object—in this instance, as an integer. If this option is not enabled, the issue does not arise. There is a relevant section on this in the Airflow documentation.

    NativeEnvironment renders values according to Python literal rules. This is useful when a template should produce a list, dict, number, or boolean, but it also means a string that looks like a number, such as "42", can be rendered as the integer 42. Keep the default string rendering, use a callable template field, or add explicit quoting if the task needs the value to stay a string.

    Using map_index_template="'{{ ... }}'" instead of map_index_template="{{ ... }}" should resolve the issue.

  4. ngnnah commented on Oct 9, 2026

    @ngnnah

    Hit this on 3.3.2 (task-sdk 1.3.1). We set render_template_as_native_obj=True for every DAG through a shared factory, and a mapped task labelled map_index_template="{{ task.file_id }}" (an int id) failed all 85 instances on its first production run.

    Re whether it's a bug: quoting the template works, but a map index label can only ever be a string. _render_map_index is typed -> str | None, and SetRenderedMapIndex.rendered_map_index is str; with the len() removed, an int still fails there (Input should be a valid string). Native rendering can't produce a valid non-string label, so for this one template it only adds a failure mode.

    It also fails late. The pre-execute render swallows the error and the post-execute render raises it, so the task's work completes and then the task is marked failed and retried.

    +1 for the str() in #71592.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:dynamic-task-mappingkind:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions