Skip to main content

How to Pass a TaskFlow Result to a Classic Operator in Apache Airflow®

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A Dag in Apache Airflow® often mixes TaskFlow @task functions with classic operators such as BashOperator. A @task function's result can go straight into one of the operator's parameters, and Airflow links the two tasks by itself. This guide passes a file name from a @task function to a BashOperator on Airflow 3.4.0.

Syntax​

Syntax
<result> = <task function>()

<Operator>(
task_id="<task id>",
<templated parameter>=<result>,
)
PartWhat it means
<result> = <task function>()Calling a @task function in the Dag doesn't run it: it gives an object that stands for its future result
<templated parameter>A parameter of the operator that Airflow renders when the task runs. The operator's reference marks these "(templated)"
<templated parameter>=<result>The result goes into that parameter, on its own or inside a dictionary or list

Airflow's TaskFlow documentation calls that object "an object representing the XCom for the result (an XComArg)", which you "can then use as inputs to downstream tasks or operators". Its example passes a TaskFlow result to a classic operator, which then works out by itself "that it must be downstream" of the TaskFlow task.

The Example​

A Dag called pass_to_bash has two tasks. pick_file, a @task function, returns a file name, report-2026.txt. show_file, a BashOperator, gets that name in the environment variable FILE_NAME and prints it.

Prerequisites​

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

Step 1: Write the Dag​

Create dags/pass_to_bash.py:

dags/pass_to_bash.py
from airflow.providers.standard.operators.bash import BashOperator
from airflow.sdk import dag, task


@dag(schedule=None, tags=["demo"])
def pass_to_bash():
@task
def pick_file() -> str:
return "report-2026.txt"

BashOperator(
task_id="show_file",
bash_command='echo "The file is $FILE_NAME"',
env={"FILE_NAME": pick_file()},
)


pass_to_bash()

The pass_to_bash.py Dag file in an editor window: a @task function pick_file that returns report-2026.txt, and a BashOperator show_file that echoes $FILE_NAME with env FILE_NAME set to pick_file().

  • pick_file() sits inside the env dictionary. env is one of BashOperator's templated parameters, together with bash_command and cwd, so Airflow replaces pick_file() with its return value when show_file runs.
  • The command reads the value from the environment variable, $FILE_NAME, instead of having it written into the command text. The BashOperator guide recommends that, and How to Set Environment Variables for BashOperator in Apache Airflow® with env and append_env explains env.
  • There's no >> anywhere.

Trigger the Dag, open it and switch to the Graph view (How to Use the Graph View in Apache Airflow® shows the view in detail):

The pass_to_bash Dag in Graph view: pick_file, a @task, joined by a line to show_file, a BashOperator, both with the state success.

The line from pick_file to show_file came from env={"FILE_NAME": pick_file()} alone: because show_file uses pick_file's result, Airflow made it run after pick_file.

Step 3: Read the Command's Output​

Open the show_file task's log (How to Read a Task Log in the Apache Airflow® UI shows where to click):

The log of the show_file task: Running command with echo "The file is $FILE_NAME", Output:, The file is report-2026.txt, and Command exited with return code 0.

The command still reads $FILE_NAME, and bash printed The file is report-2026.txt: Airflow had put pick_file's return value into the variable before the command started.

Common Mistakes​

MistakeWhat happensDo this instead
Calling the task inside an f-string: bash_command=f'echo "The file is {pick_file()}"'Python turns the result into template text, {{ task_instance.xcom_pull(...) }}, and the two tasks aren't linked: the BashOperator had no upstream task, so nothing makes it wait for pick_filePass the result as a parameter's value, as env does here
Passing the result to a parameter that isn't templated, such as output_encodingThe task fails with TypeError: decode() argument 'encoding' must be str, not PlainXComArgUse a parameter that the reference marks "(templated)"

Troubleshooting​

SymptomLikely causeFix
... must be str, not PlainXComArgThe result went to a parameter that isn't templated, so Airflow never filled it inMove it to a templated parameter
No line between the two tasks in the Graph viewThe result was used in an f-string, not passed as a valuePass the result itself, for example in env

FAQ​

Can a classic operator's result go to a TaskFlow task?​

Yes, through the operator's .output attribute. The TaskFlow tutorial says you can "pass data using the .output attribute". With say = BashOperator(task_id="say", bash_command='echo "hi from bash"'), calling show(say.output) made say the upstream task of show, and show received hi from bash.

Which BashOperator parameters are templated?​

bash_command, env and cwd.

Conclusion​

env={"FILE_NAME": pick_file()} gave the BashOperator the @task function's result, report-2026.txt, and made Airflow run pick_file first without any >>. Pass results to templated parameters as values, never inside an f-string.

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.