Skip to main content

Pydantic models for Apache Airflow

Project description

airflow-pydantic

Pydantic models for Apache Airflow

Build Status codecov License PyPI

Overview

Pydantic models of Apache Airflow data structures.

Primary Use Case: This library is designed to enable declarative DAG definitions using airflow-config or other YAML/JSON-based configuration frameworks. By representing Airflow constructs as Pydantic models, DAGs can be defined in configuration files rather than Python code, enabling better separation of concerns, easier testing, and configuration-driven workflows.

Core

Operators

Sensors

Other

Usage

Declarative DAGs with airflow-config (Recommended)

The primary use of airflow-pydantic is to build declarative, configuration-driven DAGs using airflow-config or similar YAML/JSON-based frameworks:

# config/my_dag.yaml
default_args:
  _target_: airflow_pydantic.TaskArgs
  owner: data-team
  retries: 3

default_dag_args:
  _target_: airflow_pydantic.DagArgs
  schedule: "@daily"
  start_date: "2024-01-01"
  catchup: false

This approach allows you to:

  • Define DAGs in YAML/JSON instead of Python
  • Separate configuration from code
  • Easily manage environment-specific settings
  • Version control your DAG configurations
  • Generate and validate DAGs programmatically

Programmatic Usage

All operators and sensors support two methods:

  • instantiate(): Create a concrete Airflow instance at runtime
  • render(): Generate Python code as a string for the Airflow construct

Code Generation with render()

The render() method generates valid Python code from your Pydantic models, enabling code generation workflows:

from airflow_pydantic import Dag, BashTask
from datetime import datetime

dag = Dag(
    dag_id="generated-dag",
    schedule="@daily",
    start_date=datetime(2024, 1, 1),
    tasks={
        "hello": BashTask(
            task_id="hello",
            bash_command="echo 'Hello World'",
        ),
    },
)

# Generate Python code
python_code = dag.render()

# Save to a DAG file
with open("dags/generated_dag.py", "w") as f:
    f.write(python_code)

Generated File:

from datetime import datetime

from airflow.models import DAG
from airflow.providers.standard.operators.bash import BashOperator

with DAG(schedule="@daily", start_date=datetime.fromisoformat("2024-01-01T00:00:00"), dag_id="generated-dag") as dag:
    hello = BashOperator(bash_command="echo 'Hello World'", task_id="hello", dag=dag)

This is useful for:

  • Generating DAG files from configuration during CI/CD
  • Creating DAG templates programmatically
  • Migrating from configuration-driven to static DAG files
  • Debugging and inspecting generated DAG code

[!NOTE] This library was generated using copier from the Base Python Project Template repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

airflow_pydantic-1.5.10.tar.gz (74.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

airflow_pydantic-1.5.10-py3-none-any.whl (111.7 kB view details)

Uploaded Python 3

File details

Details for the file airflow_pydantic-1.5.10.tar.gz.

File metadata

  • Download URL: airflow_pydantic-1.5.10.tar.gz
  • Upload date:
  • Size: 74.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for airflow_pydantic-1.5.10.tar.gz
Algorithm Hash digest
SHA256 f545c7caa844366181f377291c64af8fcc1251cc86c0373e5a803afc8b9d1129
MD5 c96ed9d83347c7171a7493d0cf2765bf
BLAKE2b-256 a4d78f0d4bcfc98481e4dc8c4df8d40bcc452ead48ae8c9473fa851f52ad1cc7

See more details on using hashes here.

File details

Details for the file airflow_pydantic-1.5.10-py3-none-any.whl.

File metadata

File hashes

Hashes for airflow_pydantic-1.5.10-py3-none-any.whl
Algorithm Hash digest
SHA256 75d23ab5d700cb4efff78be1711c955c678cb4e0833cdf949561a5b744c64d68
MD5 87c8b278f90750c1bf074bb6a7b5f41d
BLAKE2b-256 5641dd4b46467138a35cac22468cfb0ab15a517812a2b4793e3d991973c77a50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page