0

I'm trying to pass Airflow logical_date to dbt model so that I can use it in the model (sql). I'm using Airflow 2.11.0. I'm doing below, but the dag couldn't get constructed, with an error says:

'logical_date' is undefined.

However it works if I remove the in_timezone conversion. How should I do this? It works in earlier version of Airflow. Thanks.

dbt_task = DbtTaskGroup (
       select = ["mymodel"]
       operator_args{
          "vars": {
                 "logical_date_to_use_in_model" : "{{logical_date.in_timezone('America/Vancouver')}}"
                 },
       },
)
1
  • Could you post the following output from python?print(logical_date.in_timezone('America/Vancouver')) Commented Oct 16 at 6:13

1 Answer 1

0

Not sure what parent operator you're using, but the issue is likely because the operator isn't aware that operator_args is a template field and contains Jinja templates. To fix this, you should either:

  1. Declare operator_args explicitly as a template_field, or

  2. Assign operator_args to a variable that is already listed as a template_field.

For example, if you're inheriting from KubernetesPodOperator, its template_fields include things like 'image', 'name', 'cmds', 'arguments', etc. So, you could pass operator_args into the arguments field

self.arguments = {**operator_args} #JUST AN EXAMPLE

when calling super(), which would allow the base operator to handle the templating properly.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. I added template_fields = ("operator_args",) to the operator class, but it still has the same error. Is it how to do option 1? Or anything else that I need to do to declare it as template_fields?
pay attention you not ovveridde your template_fields in the parent operator, just check what fileds are in you parent operator and merge you on variable to it llike i show in the example. if you still getting the error please share the operatore you are using

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.