GitHub user hezeclark added a comment to the discussion: Override default_args 
definition using passed params

Overriding `default_args` using passed params in Airflow requires understanding 
when params are resolved. The key is that `params` are available at parse time 
via `{{ params.key }}` in templates, but Python-level `default_args` are set at 
DAG definition time.\n\n**Pattern 1: Use Jinja templating in operator 
fields**\n```python\nfrom airflow import DAG\nfrom airflow.operators.python 
import PythonOperator\nfrom datetime import datetime\n\nwith DAG(\n    
dag_id='my_dag',\n    params={'retries': 3, 'email': '[email protected]'},\n  
  default_args={'owner': 'airflow'},\n) as dag:\n    # Use {{ params }} in 
templates, not in default_args directly\n    task = PythonOperator(\n        
task_id='my_task',\n        python_callable=my_function,\n        retries='{{ 
params.retries }}',  # templated\n    )\n```\n\n**Pattern 2: Params in 
`default_args` via a callable (Airflow 2.2+)**\n```python\n# Use Param objects 
for structured params with defaults\nfrom airflow.models.param import 
Param\n\nwit
 h DAG(\n    dag_id='my_dag',\n    params={\n        'retry_count': Param(3, 
type='integer', minimum=0, maximum=10),\n        'notify_email': 
Param('[email protected]', type='string'),\n    }\n) as dag:\n    
pass\n```\n\n**Pattern 3: Dynamic default_args using the DAG run 
conf**\n```python\n# In a PythonCallable, access conf directly\ndef 
my_task(**context):\n    params = context['params']\n    retries = 
params.get('retries', 3)\n    # use runtime value\n```\n\nThe limitation is 
that `default_args` like `retries` and `email_on_failure` are evaluated at 
parse time, not run time. For runtime-configurable behavior, you need to access 
`context['params']` inside the callable.

GitHub link: 
https://github.com/apache/airflow/discussions/21627#discussioncomment-16154007

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to