What is the main benefit of using the TaskFlow API in Airflow?
The TaskFlow API makes it easier to write tasks as Python functions and automatically handles passing data between tasks using XComs, making the code cleaner and simpler.
Click to reveal answer
beginner
How does the TaskFlow API improve XCom usage compared to traditional methods?
It automatically serializes and deserializes data returned by Python functions, so you don't need to manually push or pull XComs, reducing boilerplate code.
Click to reveal answer
beginner
What decorator is used to define a task in the TaskFlow API?
The @task decorator is used to turn a Python function into an Airflow task with TaskFlow API.
Click to reveal answer
beginner
How do you pass data between tasks using the TaskFlow API?
You simply return a value from one task function and pass it as an argument to another task function. Airflow handles the XCom behind the scenes.
Click to reveal answer
beginner
What is a simple example of a TaskFlow API task returning data?
Example: @task
def get_number():
return 5
This task returns 5, which can be passed to other tasks automatically.
Click to reveal answer
Which decorator is used to create a task with the TaskFlow API?
A@xcom
B@dag
C@task
D@operator
✗ Incorrect
The @task decorator is used to define tasks in the TaskFlow API.
How does TaskFlow API handle data passing between tasks?
AManually pushing and pulling XComs
BReturning values from task functions automatically passed as arguments
CUsing global variables
DWriting data to files
✗ Incorrect
TaskFlow API automatically passes returned values from one task to another as arguments using XComs behind the scenes.
What is the main advantage of using TaskFlow API over traditional Airflow tasks?
ARequires less Python code and no manual XCom handling
BRuns tasks faster
CUses less memory
DSupports only Bash commands
✗ Incorrect
TaskFlow API simplifies code by reducing boilerplate and automating XCom management.
Which of the following is true about XComs in TaskFlow API?
AXComs are automatically created when returning values from tasks
BYou must manually push XComs
CXComs are disabled
DXComs only work with BashOperator
✗ Incorrect
TaskFlow API automatically creates XComs from returned values of task functions.
What happens if a TaskFlow API task returns None?
AAn error occurs
BXCom stores the value None
CThe DAG fails
DNo XCom is created
✗ Incorrect
If a task returns None, no XCom is created for that task.
Explain how the TaskFlow API simplifies passing data between tasks in Airflow.
Think about how functions return values and how those values get to the next task.
You got /4 concepts.
Describe the steps to create a simple DAG using TaskFlow API that passes a number from one task to another.
Focus on task definitions, return values, and how tasks connect.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using the TaskFlow API in Airflow for passing data between tasks?
easy
A. It automatically handles data sharing between tasks using return values.
B. It requires manual XCom push and pull commands for data sharing.
C. It disables task dependencies to speed up execution.
D. It only works with BashOperator tasks.
Solution
Step 1: Understand TaskFlow API purpose
The TaskFlow API simplifies task communication by using Python functions and decorators.
Step 2: Recognize automatic XCom handling
Return values from decorated tasks are automatically shared with downstream tasks via XCom without manual commands.
Final Answer:
It automatically handles data sharing between tasks using return values. -> Option A
Quick Check:
TaskFlow API = automatic XCom via return [OK]
Hint: TaskFlow API shares data automatically via return values [OK]
Common Mistakes:
Thinking manual XCom push/pull is needed
Believing it disables dependencies
Assuming it only works with BashOperator
2. Which of the following is the correct way to define a task using the TaskFlow API in Airflow?
easy
A. def my_task(): pass
B. @dag
def my_task(): pass
C. @task
def my_task(): pass
D. my_task = PythonOperator(task_id='my_task')
Solution
Step 1: Identify TaskFlow API syntax
Tasks are defined as Python functions decorated with @task to enable TaskFlow features.
Step 2: Compare options
@task
def my_task(): pass uses @task decorator correctly; others either lack decorator or use wrong decorators/operators.
Final Answer:
@task\ndef my_task(): pass -> Option C
Quick Check:
TaskFlow task = function + @task decorator [OK]
Hint: Use @task decorator above function to define TaskFlow task [OK]
Common Mistakes:
Omitting @task decorator
Confusing @dag with @task
Using PythonOperator instead of TaskFlow
3. Given the following code snippet using TaskFlow API, what will be printed?
@task
def add(x, y):
return x + y
@task
def print_result(value):
print(f'Result is {value}')
with DAG('example_dag', start_date=datetime(2023,1,1)) as dag:
sum_value = add(3, 4)
print_result(sum_value)
medium
A. No output printed
B. Result is
C. SyntaxError
D. Result is 7
Solution
Step 1: Understand TaskFlow return and XCom
The add task returns 7, which is passed automatically to print_result.
Step 2: Trace print statement execution
print_result prints the string with the value 7, so output is 'Result is 7'.
Final Answer:
Result is 7 -> Option D
Quick Check:
Returned value passed and printed = 'Result is 7' [OK]
Hint: Returned values flow automatically and print as expected [OK]
Common Mistakes:
Expecting raw XCom object printed
Confusing syntax error with correct code
Thinking no output prints without manual XCom pull
4. What is wrong with this TaskFlow API code snippet?
@task
def multiply(x, y):
print(x * y)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
result = multiply(2, 5)
print(f'Result is {result}')
medium
A. The DAG is missing the schedule_interval parameter.
B. The multiply task should return the result instead of printing it.
C. The print statement inside the DAG is invalid syntax.
D. The @task decorator is missing parentheses.
Solution
Step 1: Identify how TaskFlow passes data
TaskFlow passes data via return values, not print statements inside tasks.
Step 2: Analyze code behavior
Since multiply only prints and returns None, result is None, so printing it shows 'Result is None'.
Final Answer:
The multiply task should return the result instead of printing it. -> Option B
Quick Check:
Return value needed for XCom, not print [OK]
Hint: Return values to share data, not print inside tasks [OK]
Common Mistakes:
Thinking print shares data between tasks
Confusing DAG parameters with task errors
Believing @task needs parentheses always
5. You want to create a DAG using TaskFlow API where task A returns a list of numbers, and task B receives this list and returns the sum. Which code snippet correctly implements this pattern?
hard
A.
@task
def task_a():
return [1, 2, 3]
@task
def task_b(numbers):
return sum(numbers)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
nums = task_a()
total = task_b(nums)
B.
@task
def task_a():
print([1, 2, 3])
@task
def task_b(numbers):
print(sum(numbers))
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
nums = task_a()
total = task_b(nums)
C.
@task
def task_a():
return '1,2,3'
@task
def task_b(numbers):
return sum(numbers)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
nums = task_a()
total = task_b(nums)
D.
@task
def task_a():
return [1, 2, 3]
@task
def task_b(numbers):
return sum(numbers)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
total = task_b(task_a())
Solution
Step 1: Check task A returns a list
@task
def task_a():
return [1, 2, 3]
@task
def task_b(numbers):
return sum(numbers)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
nums = task_a()
total = task_b(nums)
returns a list [1, 2, 3] correctly;
@task
def task_a():
print([1, 2, 3])
@task
def task_b(numbers):
print(sum(numbers))
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
nums = task_a()
total = task_b(nums)
prints instead of returning;
@task
def task_a():
return '1,2,3'
@task
def task_b(numbers):
return sum(numbers)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
nums = task_a()
total = task_b(nums)
returns a string, not list.
Step 2: Check task B sums the list argument
@task
def task_a():
return [1, 2, 3]
@task
def task_b(numbers):
return sum(numbers)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
nums = task_a()
total = task_b(nums)
passes the list to task B which sums it correctly;
@task
def task_a():
return [1, 2, 3]
@task
def task_b(numbers):
return sum(numbers)
with DAG('dag', start_date=datetime(2023,1,1)) as dag:
total = task_b(task_a())
skips assigning intermediate variable but is valid but less clear.
Final Answer:
Option A correctly returns a list and passes it to sum in task B. -> Option A
Quick Check:
Return list and pass it to sum task [OK]
Hint: Return data structures, pass as args, sum in next task [OK]
Common Mistakes:
Using print instead of return in tasks
Passing string instead of list to sum
Ignoring task dependencies or variable assignment
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.