[Python] Can you pass Airflow templated variables with their actual type into Operators?

Discussão em 'Python' iniciado por Stack, Junho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I know that you can pass templated variables as string into operators, but I'd like to pass them around as their actual type. For example, if I have (trivial function just to illustrate my point)

    def get_day(datetime_object):
    return datetime_object.day


    And a DAG with task

    py_op=PythonOperator(
    task_id='foo',
    python_callable=get_day,
    op_args=[
    {{ ts }}
    ]
    )


    I get an error saying that {{ ts }} is undefined, but if I pass it wrapped in a string like "{{ ts }}" then the value is passed without errors, however, it's passed as a string, so get_day would error because strings have no day property, which would mean I'd have to add another step in get_day to parse the string as a datetime object, and that's of course a bit silly if I already have an original datetime object.

    Continue reading...

Compartilhe esta Página