Repository navigation
Closed
Labels
affected_version:3.0Issues Reported for 3.0affected_version:main_branchIssues Reported for main brancharea:corearea:dynamic-task-mappingAIP-42kind:bugThis is a clearly a bugpriority:highHigh priority bug that should be patched quickly but does not require immediate new release
Milestone
Description
Apache Airflow version
3.0.0
If "Other Airflow 2 version" selected, which one?
No response
What happened?
Hi,
As outlined in the documentation, I’m attempting to filter out items in my dynamic task mapping by returning None within the filter task.
However, I’m observing that None values are still being passed to the subsequent expand, which is not the expected behavior.
What you think should happen instead?
The None value must not be passed to the next expand.
How to reproduce
from airflow.decorators import dag, task
@task
def generate_data():
return [1, None, 3]
@task
def filter_item_from_mapped_task(item):
if item is None:
return None
return item
@task
def print_item(item):
assert item is not None
print(item)
@dag(
dag_id='test_bug_dag',
)
def test_bug_dag():
data = generate_data()
filtered_data = filter_item_from_mapped_task.expand(item=data)
print_item.expand(item=filtered_data)
test_bug_dag()
Operating System
MacOS 14.6 (23G80)
Versions of Apache Airflow Providers
I haven’t added any additional providers
Deployment
Docker-Compose
Deployment details
No response
Anything else?
No response
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Metadata
Metadata
Assignees
Labels
affected_version:3.0Issues Reported for 3.0affected_version:main_branchIssues Reported for main brancharea:corearea:dynamic-task-mappingAIP-42kind:bugThis is a clearly a bugpriority:highHigh priority bug that should be patched quickly but does not require immediate new release
Activity
boring-cyborg commentedon Apr 24, 2025
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
tirkarthi commentedon Apr 24, 2025
Interesting, this works for
[None]and[1, None]to run only one task but whenNonevalue is in the middle this error is present. I guess themap_indexof 0 and 2 have xcom pushed but the underlying task seems 2 upstream tasks and tries to getmap_indexfor 0 and 1 which causes 1 to have no XCom. I am checking on this.tirkarthi commentedon Apr 24, 2025
When the xcom values map 1:1 in length then the
map_indexof the downstream task can directly get the xcom value from upstream value usingmap_indexof the downstream task. But when there is a difference like upstream has 2 mapped task where None and 1 is returned formap_index=0andmap_index=1respectively then the downstream task only has to process value of upstream task at map_index=1 but the downstream task starts with map_index=0 and goes on to again fetch xcom value formap_index=0which is None. I guess some change has to be made at below code that gets the index to search in LazyXComSequence. But I guess this is a high priority issue for the next release since filter doesn't seem to be working on basic use cases.cc: @ashb for help since the dynamic task mapping was implemented by Ash and will have better idea.
airflow/task-sdk/src/airflow/sdk/definitions/_internal/expandinput.py
Lines 165 to 181 in 6a3bc01
tirkarthi commentedon Apr 25, 2025
I just tried it in Airflow 2 and 3 to see the difference. The index calculation is same in both Airflow 2 and 3 but the usage varies as below
In Airflow 2, Accessing
LazySequence[0]goes on to get all the upstream task id xcom values and add offset for the calculated index and limit the result to 1airflow/airflow-core/src/airflow/utils/db.py
Lines 1568 to 1576 in 36c8975
In Airflow 3, Accessing
LazyXComSequence[0]goes on to get the upstream task id xcom values and uses the calculated index asmap_indexto the upstream which is different compared to Airflow 2airflow/task-sdk/src/airflow/sdk/execution_time/lazy_sequence.py
Lines 151 to 157 in e2c9f9b
tirkarthi commentedon Apr 25, 2025
One approach I can think of would be to introduce
offsetand pass it from lazy xcom index access so that the similar operation is performed in Airflow 3 like Airflow 2 to useoffsetwhen present to get all xcom and then skip by offset to return the appropriate value.Ref change : tirkarthi@a6d82d0
uranusjr commentedon Apr 30, 2025
I think your change is on the right track @tirkarthi. I would change the call in
_get_itemto call the comm directly (as mentioned in the comment) with a new request type (say GetXComSequenceItem) so we skip all the XCom interface changes. This also allows us to handle 404 better since we should raise IndexError here instead of silently defaulting to None like normal XCom requests. I’ll raise a PR for this since we want this in 3.0.1.