Skip to main content

Handling non-string DAG parameters

You may need to pass a dag parameter to an operator using templates - however, it may be important that this is the right data type (e.g. bool). By default all template values are stringified.

You can override this by passing render_template_as_native_obj=True to the DAG constructor which fills the template and then casts the value to the expected type.

For example

with DAG(
    dag_id="example",
    catchup=False,
    start_date=datetime(2023, 4, 26),
    render_template_as_native_obj=True,
    params={
        "test_run": Param(False, title="Test run only", description="If true, don't do the transaction, just spoof it", type='boolean')
    }
) as dag:


    task_id = f"trigger_build_table_{table}"

    ts = datetime.now().replace(tzinfo=tz)
    dag_execution_ts = TriggerDagRunOperator(
            trigger_dag_id="exampledag",
            wait_for_completion=True,
            poke_interval=10,
            # reset_dag_run=True,tk
            execution_date=ts.isoformat(),
            trigger_run_id=f"example_dag_{ts.isoformat()}",
            conf={
                "table_name": table,
                "date_suffix": date_suffix,
                "test_run": "{{params.test_run}}"
            },
    )