Skip to content
Latchkey

Airflow "Cycle detected in DAG" circular dependency in CI

A DAG must be acyclic. When task dependencies form a loop (A waits on B and B waits on A), Airflow raises AirflowDagCycleException during parsing and refuses to register the DAG.

What this error means

DAG parsing fails with "airflow.exceptions.AirflowDagCycleException: Cycle detected in DAG. Faulty task: X" naming a task involved in the loop.

Airflow
airflow.exceptions.AirflowDagCycleException:
Cycle detected in DAG: failed to set relationship between
task 'transform' and task 'extract' (faulty task 'extract')

Common causes

Dependencies form a loop

Two tasks set each other as upstream (directly or through a chain), producing a cycle that is not a valid DAG.

A copy-paste error in set_upstream/set_downstream

A misplaced >> or << wires a task back to one of its own ancestors.

How to fix it

Break the loop in the dependency wiring

  1. Find the faulty task named in the exception.
  2. Trace its upstream chain to the task that points back to it.
  3. Remove or redirect the edge so the graph is acyclic.
dags/etl.py
# acyclic: each edge points forward only
extract >> transform >> load

Assert acyclicity in a DAG test

Loading the DagBag in a test surfaces the cycle before merge.

tests/test_dags.py
from airflow.models import DagBag
assert not DagBag().import_errors

How to prevent it

  • Wire dependencies in one direction with >> chains.
  • Add a DagBag import test to CI to catch cycles.
  • Review dependency edits carefully in shared DAGs.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →