Skip to main content

How to Return Several Values from a Task with multiple_outputs in Apache Airflow®

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A TaskFlow task in Apache Airflow® can return a dictionary, and normally the next task then gets the whole dictionary. With multiple_outputs=True, Airflow also stores each key as an XCom of its own, so you can hand single values to different tasks or parameters. This guide returns a city and its country on Airflow 3.4.0, passes them on separately, and shows a type hint that turns the same behaviour on by itself.

Syntax​

Syntax
@task(multiple_outputs=True)
def <task name>():
return {"<key 1>": <value 1>, "<key 2>": <value 2>}

<result> = <task name>()
<next task>(<result>["<key 1>"], <result>["<key 2>"])
PartWhat it means
multiple_outputs=TrueStore each key of the returned dictionary as its own XCom, as well as the whole dictionary
return {...}A dictionary whose keys are strings
<result> = <task name>()The object that stands for the task's output in the Dag
<result>["<key 1>"]Just that key's value, passed to the next task

Airflow's TaskFlow tutorial says multiple_outputs=True "tells Airflow that the function returns a dictionary of values that should be split into individual XComs. Each key in the returned dictionary becomes its own XCom entry". An XCom is a small value Airflow stores so another task can use it.

The Example​

A Dag called city_facts has two tasks. get_city returns a city's name and country as a dictionary, and describe takes the two as separate parameters and logs a sentence with them.

Prerequisites​

  • Apache Airflow® running, for example with airflow standalone, and its dags folder in your AIRFLOW_HOME.

Step 1: Return a Dictionary with multiple_outputs=True​

Create dags/city_facts.py with the imports, a logger, the Dag function and the first task inside it:

dags/city_facts.py
import logging

from airflow.sdk import dag, task

log = logging.getLogger(__name__)


@dag(schedule=None, tags=["demo"])
def city_facts():
@task(multiple_outputs=True)
def get_city():
return {"name": "Lisbon", "country": "Portugal"}

@task(multiple_outputs=True) passes the setting to the decorator; the function itself is ordinary Python.

Step 2: Pass Each Value On​

Inside the Dag function, add the second task and call both. Below the function, call the Dag function:

dags/city_facts.py
@task
def describe(name: str, country: str):
log.info("%s is in %s.", name, country)

city = get_city()
describe(city["name"], city["country"])


city_facts()

city["name"] doesn't read a dictionary now: city stands for get_city's output, and ["name"] picks the XCom stored under that key when describe runs. The complete file:

dags/city_facts.py
import logging

from airflow.sdk import dag, task

log = logging.getLogger(__name__)


@dag(schedule=None, tags=["demo"])
def city_facts():
@task(multiple_outputs=True)
def get_city():
return {"name": "Lisbon", "country": "Portugal"}

@task
def describe(name: str, country: str):
log.info("%s is in %s.", name, country)

city = get_city()
describe(city["name"], city["country"])


city_facts()

The city_facts.py Dag file in an editor window: get_city with @task(multiple_outputs=True) returning name Lisbon and country Portugal, describe logging the two, and describe(city["name"], city["country"]).

Step 3: See One XCom per Key​

Trigger the Dag, open the get_city task and its XCom tab (How to See a Task's XComs in the Apache Airflow® UI walks through it):

The XCom tab of the get_city task in the city_facts Dag: 3 XCom Entries, country Portugal, name Lisbon, and return_value with name Lisbon and country Portugal.

There are three XComs: country and name, one per key, and return_value with the whole dictionary.

Step 4: Check the Values Arrived Separately​

Open the describe task's log:

The log of the describe task: INFO - Lisbon is in Portugal. and INFO - Done. Returned value was: None.

Lisbon is in Portugal. shows that name and country reached describe as two separate values.

Step 5: Let a Type Hint Turn It On​

A second Dag, city_facts_typed, is the same except for get_city: it has no multiple_outputs, and a return type hint instead:

dags/city_facts_typed.py
@task
def get_city() -> dict[str, str]:
return {"name": "Lisbon", "country": "Portugal"}

Its XCom tab after a run:

The XCom tab of the get_city task in the city_facts_typed Dag: the same 3 XCom Entries, country Portugal, name Lisbon, and the whole dictionary as return_value.

The same three XComs. When multiple_outputs isn't set and the return type hint is a dictionary type, Airflow 3.4.0 sets it to True by itself, and @task(multiple_outputs=False) turns that off: a task with -> dict[str, str] and multiple_outputs=False stored only return_value. The Airflow 3 documentation doesn't describe this, and the TaskFlow tutorial says the whole dictionary is stored as one XCom when you leave multiple_outputs=True out. I reported the gap as apache/airflow#78126.

Common Mistakes​

MistakeWhat happensDo this instead
Returning a list with multiple_outputs=TrueThe task fails with TypeError: Returned output was type <class 'list'> expected dictionary for multiple_outputsReturn a dictionary, with a key for each value
Using numbers as keys: {1: "Lisbon", 2: "Portugal"}The task fails with TypeError: Returned dictionary keys must be strings when using multiple_outputs, found 1 (<class 'int'>) insteadUse strings as keys
Misspelling a key: city["town"]Nothing fails: the next task gets NoneUse a key the function returns, exactly as it's written
Adding -> dict[str, str] and expecting one XComThe dictionary is split anywayAdd multiple_outputs=False if the next task needs the dictionary as one value

Troubleshooting​

SymptomLikely causeFix
The XCom tab shows only return_valuemultiple_outputs isn't on, and the function has no dictionary return type hintAdd multiple_outputs=True
The next task gets NoneThe key in result["..."] doesn't match a key the function returnsCompare the key with the XCom tab of the first task

FAQ​

Can the next task still get the whole dictionary?​

Yes. return_value is stored as well, and passing city itself passes it: a task called with city got {'name': 'Lisbon', 'country': 'Portugal'}.

How big can the values be?​

Small. XComs "are only designed for small amounts of data", says the XCom documentation. Save large data to a file or a database and return its location.

Conclusion​

@task(multiple_outputs=True) stored get_city's dictionary as one XCom per key, and describe(city["name"], city["country"]) received the two values separately. A -> dict[str, str] return type hint did the same without the flag, and multiple_outputs=False turns it off.

Additional Resources​

Apache®, Apache Airflow and Airflow® are either registered trademarks or trademarks of The Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.