Operators and TaskFlow
Tasks in Airflow are built from operators, ready-made templates for a kind of work, or from plain Python functions with the TaskFlow API. This lesson shows both styles, how they fit together, and which to choose.
Pipelines in production · Airflow · Bronze · Tasks, lesson 1 of 2
On this page
Read first: A DAG file, line by line (skip if you know it)
In plain words first
A hardware shop sells ready-made tools (a drill, a saw) and also lets you build your own jig for an unusual job. Operators are the ready-made tools: run a shell command, a SQL query, a Spark job. TaskFlow lets you turn your own Python function into a tasktask: One step of a DAG, such as running a query or calling an API. Each run of a task for one date is a task instance. Learn more → with one decorator.
Operators and tasks
An operator is a template for one kind of work: run a shell command, call a Python function, run a SQL statement, submit a Spark job. Creating one inside a DAGDAG: A pipeline in Airflow: a set of tasks and the order they run in, defined in a Python file and run on a schedule. Learn more → makes a task. Each run of the DAG then gets a task instancetask instance: One run of one task for one DAG run, with its own state (queued, running, success, failed) and its own log. Learn more → of every task: that is what has a state, tries and logs.
| Need | Use | From |
|---|---|---|
| Run a command or script | BashOperator or @task.bash | airflow.providers.standard.operators.bash |
| Run Python logic | @task (TaskFlow) or PythonOperator | airflow.sdk / the standard provider |
| A placeholder or join point | EmptyOperator | airflow.providers.standard.operators.empty |
| Talk to a system: SQL, Spark, cloud services | That system's provider operator | apache-airflow-providers-* packages |
TaskFlow: Python functions as tasks
@task turns a function into a task. Calling it inside the DAG creates the task; passing one task's result into another creates the dependency and moves the value through XComXCom: Cross-communication: small values one task saves for later tasks to read, like a file path or a row count. Not for the data itself. Learn more →. The code reads like ordinary Python.
import pendulum from airflow.sdk import dag, task @dag( schedule="@daily", start_date=pendulum.datetime(2026, 3, 1, tz="UTC"), catchup=True, ) def top_artists(): @task # context values arrive as named parameters def count_plays(ds=None): plays = {"2026-03-01": 120, "2026-03-02": 95} return plays.get(ds, 0) @task def classify(n): return "busy" if n >= 100 else "quiet" classify(count_plays()) # count_plays >> classify, value included top_artists()
What the schedulerscheduler: The Airflow process that reads DAG files, creates runs when they are due, and queues the tasks whose upstream tasks are done. Learn more → does · deployed Mar 1, 00:00 UTC · grid as of Mar 2, 01:00 UTC · @daily (CronTriggerTimetable)
| task \ run | Mar 1 00:00 | Mar 2 00:00 |
|---|---|---|
| count_plays | ||
| classify |
success· the bar on top is the run's state; a number is how many tries
Return value of classify, run by run
Mar 1, 00:00 'busy' Mar 2, 00:00 'quiet'
Two things to notice: ds is filled in because the function names it (any context variable works this way), and the return values stay small. XCom is for results like counts and file paths, never the data itself; XCom covers why.
Commands: BashOperator and @task.bash
publish = BashOperator( task_id="publish", bash_command="publish_charts --day {{ ds }}" ) @task.bash def export_csv(table: str) -> str: # the returned string is the command return f"export_table {table} --day {{{{ ds }}}}"
- The command is a template.
{{ ds }}is replaced with the run's date when the task runs, so the same command processes a different day each run. - Watch out for
.sh. If the whole command is a script name ending in.sh, Airflow tries to load that file as a template and fails if it cannot find it. Add a space after the name ("run.sh ") or pass an argument, and it runs as a normal command. - The exit code decides success. 0 means the task succeeded; anything else means it failed.
- The last line printed becomes the task's return value, which downstream tasks can read through XCom.
Providers: the operators for everything else
Integrations ship as provider packages: apache-airflow-providers-postgres, -amazon, -google, -databricks, -apache-spark, -snowflake and many more, each with operators, hooks (clients for the system) and sensorssensor: A task that waits until something is true, such as a file arriving, before downstream tasks may run. Learn more →. Prefer them to calling a client library inside @task: they handle connections stored in Airflow, retries of the remote call, and cancelling remote work when a task is stopped.
Common mistakes
- Processing large data inside @task — The workerworker: The process that actually runs a task instance, on whatever machine the executor sends it to. Learn more → runs out of memory. Hand the work to Spark or the warehouse.
- bash_command="run.sh" with nothing after it — Airflow tries to load run.sh as a template file and fails.
- Hand-written API clients where a provider exists — You re-implement connections, retries and cancellation.
- Returning a DataFrame from a task — XCom stores it in the metadata databasemetadata database: The database where Airflow records every DAG run, task instance, state and setting. Learn more →, or fails to serialize it.
Key takeaways
- An operator instance in a DAG is a task; each run has a task instance of it.
- @task makes Python functions tasks; passing results wires dependencies and XCom.
- Context variables arrive as named parameters.
- Use provider operators for external systems; keep data out of tasks.
Interview prep
THE QUESTION
"When would you use a provider operator rather than a @task that calls a client library?"
Avoid: "TaskFlow functions run when the DAG file is loaded". Calling a @task function in the DAG only creates a task; the function body runs on a worker during a run.
Follow-ups · answer out loud, then open
Follow-up"How does a TaskFlow function receive logical_date or ti?"
Scenario"A PythonOperator loads a 5 GB CSV with pandas and keeps getting killed. What do you change?"
Trap"Every operator's arguments are rendered as Jinja templates."
Check yourself
3 questionsWhat does classify(count_plays()) do inside a DAG?
Show the answer
Creates two tasks, count_plays before classify, passing the value via XCom. TaskFlow calls create tasks and wire their dependency.
How does a @task function get the run's ds?
Show the answer
Name a parameter ds. Airflow passes context variables whose names match parameters.
bash_command="load.sh" fails with a template error. Why?
Show the answer
A command ending in .sh is read as a template file. Add a space or arguments so it is treated as a command.
Keep going
Up next · Tasks · lesson 2 of 2 · 5 min readTemplates and idempotent reruns
Jinja in task arguments, the context Airflow 3 provides, macros, and reruns that redo the same day.
Related lessons
Previous: Catchup, backfill and end_date
Primary sources: TaskFlow · Operators · BashOperator