How to Pass a TaskFlow Result to a Classic Operator in Apache Airflow®
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
<result> = <task function>()
<Operator>(
task_id="<task id>",
<templated parameter>=<result>,
)
| Part | What 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 itsdagsfolder in yourAIRFLOW_HOME.
Step 1: Write the Dag
Create 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()

pick_file()sits inside theenvdictionary.envis one of BashOperator's templated parameters, together withbash_commandandcwd, so Airflow replacespick_file()with its return value whenshow_fileruns.- 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 explainsenv. - There's no
>>anywhere.
Step 2: See the Link in the Graph
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 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 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
| Mistake | What happens | Do 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_file | Pass the result as a parameter's value, as env does here |
Passing the result to a parameter that isn't templated, such as output_encoding | The task fails with TypeError: decode() argument 'encoding' must be str, not PlainXComArg | Use a parameter that the reference marks "(templated)" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
... must be str, not PlainXComArg | The result went to a parameter that isn't templated, so Airflow never filled it in | Move it to a templated parameter |
| No line between the two tasks in the Graph view | The result was used in an f-string, not passed as a value | Pass 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
- TaskFlow — XComArgs and mixing TaskFlow with operators.
- TaskFlow tutorial — "Mixing with Traditional Tasks" and
.output. - BashOperator reference — the templated parameters.
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.
