Skip to content

Mismatched Xcom Map Index when Dynamic Mapping over TaskGroup and not all mapped tasks have run #40321

Open

Description

@gavinhonl

Apache Airflow version

Other Airflow 2 version (please specify below)

If "Other Airflow 2 version" selected, which one?

2.8.3

What happened?

If a TaskGroup is dynamically mapped, the Xcom map index for downstream tasks to pull from is mismatched. This is due to the Xcom length not being the same length as the number of mapped tasks resulting in the following error:
File "/home/airflow/.local/lib/python3.11/site-packages/airflow/models/xcom.py", line 795, in __getitem__ raise IndexError(key) from None IndexError: 5

This is a simplified DAG that reproduces the problem. One mapped task is caused to reschedule instead of complete causing the shortened Xcom dict:


from airflow.decorators import task, task_group
from airflow.operators.python import get_current_context
from airflow import DAG
from airflow.operators.empty import EmptyOperator
from airflow.sensors.base import PokeReturnValue

default_args = {
    'owner': 'dev',
    'depends_on_past': False,
    'start_date': datetime(2019, 1, 1),
    'provide_context': True,
    'retries': 0,
    'retry_delay': timedelta(seconds=30)
}

@task
def parse_csv_schedule():
    items_dict = [{'A': 1}, {'B': 2}, {'C': 3}, {'D': 4}, {'E': 5}, {'F': 6}]
    return items_dict

@task_group(group_id="process_items")
def process_items(items_dict: dict):
    @task.sensor(poke_interval=90, mode="reschedule")
    def retrieve_item(item: dict) -> PokeReturnValue:
        for letter, integer in item.items():
            if integer == 3:
                print("Number is 3 - reschedule")
                return PokeReturnValue(is_done=False)
            else:
                print("Number is not 3 - Let's go")
                return PokeReturnValue(is_done=True, xcom_value=item)
    
    @task
    def process_item(item: dict):
        context = get_current_context()
        ti = context["ti"]
        map_index = ti.map_index
        print(f"Taskflow XCOM: {item}")
        xcom_dict = ti.xcom_pull("process_items.retrieve_item")[map_index]
        print(xcom_dict)
    
    item_dict = retrieve_item(item=items_dict)
    process_item(item=item_dict)

with DAG(dag_id='af041_Debugger', default_args=default_args, max_active_runs=1, schedule_interval=None):
    end = EmptyOperator(task_id='end', trigger_rule='none_failed_min_one_success')
    items_dict = parse_csv_schedule()
    items_dict >> process_items.expand(items_dict=items_dict) >> end

Resulting in:
image

image

The last task (map_index 5) fails as the Xcom list length only has 5 items instead of 6. What is more of an issue is the task with map_index=3, has pulled the xcom for map_index=4!

This issues appears to happen with both Taskflow API and traditional operators.

What you think should happen instead?

  • Mapped tasks should run independently of each other.
  • Downstream tasks should always be dependent on their associated upstream tasks
  • If a mapped task does not complete then downstream tasks should not be able to access said task's Xcom
  • Downstream tasks should only be able to access Xcoms from their related upstream tasks

Ideally a downstream task could just perform a xcom pull from a upstream task with the same map_index without needing to call ti.map_index. If the Xcom must be a list of all mapped tasks then there should be an explicit reference to the map_index or at the very least, the length of the dict should be the same as the number of mapped tasks.

How to reproduce

Run the provided DAG. You will notice that the last mapped task fails as the map_index for the upstream Xcom does not exist. This is due to the length of the Xcom dict being too short due to one of the mapped tasks not completing.

If there is a better way to access Xcoms from upstream tasks (not just the direct upstream via Taskflow) please let me know as it feels like I'm not using TaskGroups and Xcoms the way it's intended.

Operating System

Ubuntu 22.04

Versions of Apache Airflow Providers

No response

Deployment

Official Apache Airflow Helm Chart

Deployment details

AWS EKS

Anything else?

100% reproducible

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 Jun 19, 2024
  2. added and removed
    needs-triage
    on Jun 19, 2024
  3. gavinhonl commented on Jun 26, 2024

    @gavinhonl
    Author

    A workaround is to use the Taskflow API and pull the Xcom as the task's argument like so:

    from datetime import datetime, timedelta
    
    from airflow.decorators import task, task_group
    from airflow import DAG
    from airflow.operators.empty import EmptyOperator
    from airflow.sensors.base import PokeReturnValue
    
    default_args = {
        'owner': 'dev',
        'depends_on_past': False,
        'start_date': datetime(2019, 1, 1),
        'provide_context': True,
        'retries': 0,
        'retry_delay': timedelta(seconds=30)
    }
    
    @task
    def parse_csv_schedule():
        items_dict = [{'A': 1}, {'B': 2}, {'C': 3}, {'D': 4}, {'E': 5}, {'F': 6}]
        return items_dict
    
    @task_group(group_id="process_items")
    def process_items(items_dict: dict):            
        @task.sensor(poke_interval=90, mode="reschedule")
        def ignore_3(picked_item: dict) -> PokeReturnValue:
            for letter, integer in picked_item.items():
                if integer == 3:
                    print("Number is 3 - reschedule")
                    return PokeReturnValue(is_done=False)
                else:
                    print("Number is not 3 - Let's go")
                    not_three = picked_item
                    return PokeReturnValue(is_done=True, xcom_value=not_three)
        
        @task
        def process_item(item: dict):
            print(f"Taskflow XCOM: {item}")
    
        not_three = ignore_3(picked_item=items_dict)
        process_item(item=not_three)
    
    with DAG(dag_id='af041_Debugger', default_args=default_args, max_active_runs=1, schedule_interval=None):
        end = EmptyOperator(task_id='end', trigger_rule='none_failed_min_one_success')
        items_dict = parse_csv_schedule()
        items_dict >> process_items.expand(items_dict=items_dict) >> end
    

    Resulting in the expected behaviour:
    image
    image
    This approach is less desirable as there is an additional task dependency which affects triggering.

  4. potiuk commented on Jul 2, 2024

    @potiuk
    Member

    cc: @RNHTTR -> does it ring a bell ?

  5. RNHTTR commented on Jul 3, 2024

    @RNHTTR
    Contributor

    It does not, but I can take a look.

  6. self-assigned this
    on Jul 3, 2024
  7. github-actions commented on Jul 4, 2025

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 365 days without any activity. There has been several Airflow releases since last activity on this issue. Kindly asking to recheck the report against latest Airflow version and let us know if the issue is reproducible. The issue will be closed in next 30 days if no further activity occurs from the issue author.

  8. removed their assignment
    on Jul 7, 2025
  9. added
    needs-triage
    and removed on Jul 8, 2025
  10. isaiahiruoha commented on Aug 20, 2025

    @isaiahiruoha
    Contributor

    Reproduced on main, I believe it is a result of LazyXComSelectSequence executing a query and returning a dense array from a sparse dataset.

  11. added
    affected_version:3.0
    and removed
    _eol_affected_version:2.8
    needs-triage
    on Aug 21, 2025
  12. added
    priority:medium
    on Sep 2, 2025
  13. zameji commented on Nov 3, 2025

    @zameji

    The suggested workaround has the issue of introducing dependecies between tasks and bloating the data you are propagating downstream.

    Until a proper fix arrives, we work around the issue by getting the statuses of all the mapped tasks and then remapping the result array before getting the map_index value out of it.

    In pseudocode (can't publish actual code):

    xcom_values = ti.xcom_pull(task_ids=task_id, key=key_id)
    
    mapped_tasks = [ti for ti in dag_run.get_task_instances() if is_task_instance(ti, xcom_task_id)]
    sorted_mapped_tasks = sort_mapped_tasks(mapped_tasks)
    mapped_tasks_statuses = [get_status(ti) for ti in sorted_mapped_tasks]
    
    xcom_value_corrected = insert_placeholder_for_unsuccessful_ti(xcom_values, mapped_tasks_statuses)
    
    return xcom_value_corrected[ti.map_index]
  14. github-actions commented on Nov 6, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 365 days without any activity. There has been several Airflow releases since last activity on this issue. Kindly asking to recheck the report against latest Airflow version and let us know if the issue is reproducible. The issue will be closed in next 30 days if no further activity occurs from the issue author.

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

    Stale Bug Reportaffected_version:3.0area:corearea:dynamic-task-mappingkind:bugpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions