Airflow

Example

The following agent will use Airflow to save and read a DAG file.

1from kern.agent import Agent
2from kern.tools.airflow import AirflowTools
3
4agent = Agent(
5 tools=[AirflowTools(dags_dir="dags", save_dag=True, read_dag=True)], markdown=True
6)
7
8dag_content = """
9from airflow import DAG
10from airflow.operators.python import PythonOperator
11from datetime import datetime, timedelta
12default_args = {
13 'owner': 'airflow',
14 'depends_on_past': False,
15 'start_date': datetime(2024, 1, 1),
16 'email_on_failure': False,
17 'email_on_retry': False,
18 'retries': 1,
19 'retry_delay': timedelta(minutes=5),
20}
21# Using 'schedule' instead of deprecated 'schedule_interval'
22with DAG(
23 'example_dag',
24 default_args=default_args,
25 description='A simple example DAG',
26 schedule='@daily', # Changed from schedule_interval
27 catchup=False
28) as dag:
29 def print_hello():
30 print("Hello from Airflow!")
31 return "Hello task completed"
32 task = PythonOperator(
33 task_id='hello_task',
34 python_callable=print_hello,
35 dag=dag,
36 )
37"""
38
39agent.run(f"Save this DAG file as 'example_dag.py': {dag_content}")
40
41agent.print_response("Read the contents of 'example_dag.py'")

Toolkit Params

ParameterTypeDefaultDescription
dags_dirPath or strNoneDirectory for DAG files
enable_save_dag_fileboolTrueEnables functionality to save Airflow DAG files
enable_read_dag_fileboolTrueEnables functionality to read Airflow DAG files
allboolFalseEnables all functionality when set to True

Toolkit Functions

FunctionDescription
save_dag_fileSaves python code for an Airflow DAG to a file
read_dag_fileReads an Airflow DAG file and returns the contents

Developer Resources