Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jinja undefined errors when extracting openlineage events #612

Closed
ivanstillfront opened this issue Oct 18, 2023 · 18 comments · Fixed by #626
Closed

Jinja undefined errors when extracting openlineage events #612

ivanstillfront opened this issue Oct 18, 2023 · 18 comments · Fixed by #626
Labels
awaiting_response bug Something isn't working priority:high High priority issues are blocking or critical issues without a workaround and large impact
Milestone

Comments

@ivanstillfront
Copy link
Contributor

All my cosmos-generated tasks are failing after the dbt run because of Jinja "undefined" errors during lineage processing. I have lineage disabled with OPENLINEAGE_DISABLED=True env var.

I am using Astro runtime 9.1.0 with dbt in a virtualenv as recommended. This is my Dockerfile:

FROM quay.io/astronomer/astro-runtime:9.1.0

# install dbt into a virtual environment
RUN python -m venv dbt_venv && source dbt_venv/bin/activate && \
    pip install --no-cache-dir dbt-snowflake && deactivate

ENV OPENLINEAGE_DISABLED=True

My model is very simple:

{{ config(materialized = 'table') }}

select
  try_to_timestamp('{{ run_started_at }}') as run_started_at

Everything works fine until the Task starts extracting openlineage data.

Here is the task log:

...normal dbt run above
[2023-10-18, 14:49:11 PDT] {subprocess.py:94} INFO - �[0m21:49:11  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1
[2023-10-18, 14:49:12 PDT] {subprocess.py:98} INFO - Command exited with return code 0
[2023-10-18, 14:49:12 PDT] {taskinstance.py:1935} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 445, in execute
    self.build_and_run_cmd(context=context, cmd_flags=cmd_flags)
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 357, in build_and_run_cmd
    result = self.run_command(cmd=dbt_cmd, env=env, context=context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 231, in run_command
    self.calculate_openlineage_events_completes(env, Path(tmp_project_dir))
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 268, in calculate_openlineage_events_completes
    openlineage_processor = DbtLocalArtifactProcessor(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 53, in __init__
    dbt_project = self.load_yaml_with_jinja(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 133, in load_yaml_with_jinja
    return self.render_values_jinja(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 156, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 156, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 156, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 166, in render_values_jinja
    return environment.from_string(value).render()  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'run_started_at' is undefined

What am I doing wrong?

@tatiana
Copy link
Collaborator

tatiana commented Oct 19, 2023

Hi @ivanstillfront , thanks for reporting this issue.

Could you confirm that running this model using plain dbt works as expected?

The library Cosmos is using to build Dataset URIs (openlineage-common) could not find the macro run_started_at. These Dataset URIs are used to populate Cosmos inlets and outlets - and, if lineage is enabled, it would also send to lineage events (but this is optional):

if is_openlineage_available:
self.calculate_openlineage_events_completes(env, Path(tmp_project_dir))
context[
"task_instance"
].openlineage_events_completes = self.openlineage_events_completes # type: ignore
if self.emit_datasets:
inlets = self.get_datasets("inputs")
outlets = self.get_datasets("outputs")
logger.info("Inlets: %s", inlets)
logger.info("Outlets: %s", outlets)
self.register_dataset(inlets, outlets)

Is there any chance you could share a reproducible dbt project for us to test this out - it would save us time reproducing the issue - and would help us finding a fix quicker.

@ivanstillfront
Copy link
Contributor Author

@tatiana I get a different error, but it happens at the same spot when initialising DbtLocalArtifactProcessor with this minimal configuration. This runs without error when running dbt run from the command line. Here is the log output:

[2023-10-19, 09:47:15 PDT] {subprocess.py:94} INFO - �[0m16:47:15  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1
[2023-10-19, 09:47:16 PDT] {subprocess.py:98} INFO - Command exited with return code 0
[2023-10-19, 09:47:16 PDT] {taskinstance.py:1935} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 445, in execute
    self.build_and_run_cmd(context=context, cmd_flags=cmd_flags)
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 357, in build_and_run_cmd
    result = self.run_command(cmd=dbt_cmd, env=env, context=context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 231, in run_command
    self.calculate_openlineage_events_completes(env, Path(tmp_project_dir))
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 268, in calculate_openlineage_events_completes
    openlineage_processor = DbtLocalArtifactProcessor(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 58, in __init__
    absolute_dir, dbt_project["target-path"], "manifest.json"
                  ~~~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'target-path'

models/test/test.sql:

{{ config(materialized = 'table', tags=['test']) }}

select
    try_to_timestamp('{{ run_started_at }}') as run_started_at

dbt_project.yml:

name: 'dbt_imo'
version: '1.0.0'
config-version: 2

profile: 'dbt_imo'

model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
snapshot-paths: ["snapshots"]

clean-targets:
  - "target"
  - "dbt_packages"

models:
  dbt_imo:
    materialized: incremental
    incremental_strategy: delete+insert
    test:
      schema: test

My DAG code is very simple:

with DAG(...):
    profile_mapping = (
        get_automatic_profile_mapping(conn_id="snowflake"),
    )

    profile_config = ProfileConfig(
        profile_name="dbt_imo",
        target_name="prod" if is_prod() else "dev",
        profile_mapping=profile_mapping[0],
    )

    project_config = ProjectConfig(
        dbt_project_path=dbt_root,
    )

    render_config = RenderConfig(
        test_behavior=TestBehavior.NONE,
        select=["config.tags:test"],
        emit_datasets=False,
    )

    dbt_models = DbtTaskGroup(
        project_config=project_config,
        profile_config=profile_config,
        render_config=render_config,
        execution_config=ExecutionConfig(
            dbt_executable_path=str(
                Path(airflow_home)
                / "dbt_venv"
                / "bin"
                / "dbt"
            ),
        ),
    )

@ivanstillfront
Copy link
Contributor Author

@tatiana is there anything else I can do to help figure out what this is about?

@ivanstillfront
Copy link
Contributor Author

ivanstillfront commented Oct 23, 2023

I created a workaround that works for us which might be something the maintainers want to integrate, @tatiana, see PR #622 This change does not resolve the issue, but it allows us to not have failing tasks when dbt runs succeed without problem (and OPENLINEAGE_DISABLED=True)

@tatiana
Copy link
Collaborator

tatiana commented Oct 24, 2023

@ivanstillfront The issue dbt_project["target-path"] you described has been addressed in a previous issue, #533, as commented in:
#533 (comment)

If you update this dependency, things should work:

openlineage-integration-common>=1.2.0

@ivanstillfront
Copy link
Contributor Author

@ivanstillfront The issue dbt_project["target-path"] you described has been addressed in a previous issue, #533, as commented in: #533 (comment)

If you update this dependency, things should work:

openlineage-integration-common>=1.2.0

Thank you @tatiana , that did resolve the dbt_project["target-path"] issue. Now I am back to my original issue, here is the log output:

[2023-10-24, 09:05:52 PDT] {subprocess.py:73} INFO - Running command: ['/usr/local/airflow/dbt_venv/bin/dbt', 'run', '--models', 'test', '--profiles-dir', '/tmp/tmpalexojuu', '--profile', 'dbt_imo', '--target', 'dev']
[2023-10-24, 09:05:52 PDT] {subprocess.py:84} INFO - Command output:
[2023-10-24, 09:05:53 PDT] {subprocess.py:94} INFO - �[0m16:05:53  Running with dbt=1.6.6
[2023-10-24, 09:05:54 PDT] {subprocess.py:94} INFO - �[0m16:05:54  Registered adapter: snowflake=1.6.4
[2023-10-24, 09:05:54 PDT] {subprocess.py:94} INFO - �[0m16:05:54  Unable to do partial parsing because of a version mismatch
[2023-10-24, 09:05:54 PDT] {subprocess.py:94} INFO - �[0m16:05:54  Found 1 model, 0 sources, 0 exposures, 0 metrics, 377 macros, 0 groups, 0 semantic models
[2023-10-24, 09:05:54 PDT] {subprocess.py:94} INFO - �[0m16:05:54
[2023-10-24, 09:05:57 PDT] {subprocess.py:94} INFO - �[0m16:05:57  Concurrency: 1 threads (target='dev')
[2023-10-24, 09:05:57 PDT] {subprocess.py:94} INFO - �[0m16:05:57
[2023-10-24, 09:05:57 PDT] {subprocess.py:94} INFO - �[0m16:05:57  1 of 1 START sql table model dev.test .......................................... [RUN]
[2023-10-24, 09:05:59 PDT] {subprocess.py:94} INFO - �[0m16:05:59  1 of 1 OK created sql table model dev.test ..................................... [�[32mSUCCESS 1�[0m in 2.00s]
[2023-10-24, 09:05:59 PDT] {subprocess.py:94} INFO - �[0m16:05:59
[2023-10-24, 09:05:59 PDT] {subprocess.py:94} INFO - �[0m16:05:59  Finished running 1 table model in 0 hours 0 minutes and 5.13 seconds (5.13s).
[2023-10-24, 09:05:59 PDT] {subprocess.py:94} INFO - �[0m16:05:59
[2023-10-24, 09:05:59 PDT] {subprocess.py:94} INFO - �[0m16:05:59  �[32mCompleted successfully�[0m
[2023-10-24, 09:05:59 PDT] {subprocess.py:94} INFO - �[0m16:05:59
[2023-10-24, 09:05:59 PDT] {subprocess.py:94} INFO - �[0m16:05:59  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1
[2023-10-24, 09:06:00 PDT] {subprocess.py:98} INFO - Command exited with return code 0
[2023-10-24, 09:06:00 PDT] {taskinstance.py:1937} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 445, in execute
    self.build_and_run_cmd(context=context, cmd_flags=cmd_flags)
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 357, in build_and_run_cmd
    result = self.run_command(cmd=dbt_cmd, env=env, context=context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 231, in run_command
    self.calculate_openlineage_events_completes(env, Path(tmp_project_dir))
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 268, in calculate_openlineage_events_completes
    openlineage_processor = DbtLocalArtifactProcessor(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 57, in __init__
    dbt_project = self.load_yaml_with_jinja(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 157, in load_yaml_with_jinja
    return self.render_values_jinja(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 190, in render_values_jinja
    return environment.from_string(value).render()  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'run_started_at' is undefined

@tatiana
Copy link
Collaborator

tatiana commented Oct 25, 2023

@ivanstillfront It seems Cosmos could pass variables/envvars to dbt, but it did not forward those values to the library we're using to create Dataset URIs.

Could you confirm what run_started_at is? Is it a variable, an environment variable, or something else..?

If we merge this PR:
#626
you'll be unblocked - but I'm just worried that we're hiding some more critical issues that we should address.

@ivanstillfront
Copy link
Contributor Author

@tatiana
Copy link
Collaborator

tatiana commented Oct 25, 2023

@ivanstillfront I just logged a ticket in the Openlineage project:
OpenLineage/OpenLineage#2212

In the meantime, we'll merge the PR I mentioned and release it as part of 1.2.1 - so you are no longer blocked.

tatiana added a commit that referenced this issue Oct 25, 2023
)

Before this change, Cosmos failed to run if there is an issue in the
openlineage-common parsing of the dbt project due to a jinja2 exception,
which did not happen when running the dbt command by itself:

#612 (comment)

```
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 268, in calculate_openlineage_events_completes
    openlineage_processor = DbtLocalArtifactProcessor(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 57, in __init__
    dbt_project = self.load_yaml_with_jinja(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 157, in load_yaml_with_jinja
    return self.render_values_jinja(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 190, in render_values_jinja
    return environment.from_string(value).render()  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'run_started_at' is undefined
```
Closes: #612
Relates to: OpenLineage/OpenLineage#2212
tatiana added a commit that referenced this issue Oct 25, 2023
)

Before this change, Cosmos failed to run if there is an issue in the
openlineage-common parsing of the dbt project due to a jinja2 exception,
which did not happen when running the dbt command by itself:

#612 (comment)

```
  File "/usr/local/lib/python3.11/site-packages/cosmos/operators/local.py", line 268, in calculate_openlineage_events_completes
    openlineage_processor = DbtLocalArtifactProcessor(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 57, in __init__
    dbt_project = self.load_yaml_with_jinja(
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 157, in load_yaml_with_jinja
    return self.render_values_jinja(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 180, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
                       ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/openlineage/common/provider/dbt/local.py", line 190, in render_values_jinja
    return environment.from_string(value).render()  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'run_started_at' is undefined
```
Closes: #612
Relates to: OpenLineage/OpenLineage#2212

(cherry picked from commit 4c65f25)
@tatiana
Copy link
Collaborator

tatiana commented Oct 25, 2023

@sylashsu
Copy link

Same as as_native:
https://docs.getdbt.com/reference/dbt-jinja-functions/as_native

In dbt profiles.yml, I set env_var and use `as_native'.

my_dbt_project:
  target: dev
  outputs:
    dev:
      dataset: dataset
      location: US
      method: service-account-json
      keyfile_json: "{{ env_var('GCP_CREDENTIALS') | as_native }}"
      priority: interactive
      project: my_dbt_project
      threads: 1
      timeout_seconds: 300
      type: bigquery

dbt seems normal, but is getting `jinja2.exceptions.TemplateAssertionError', causing the DAG task to fail.

Task log:

[2023-10-26, 03:08:15 UTC] {{subprocess.py:94}} INFO - �[0m03:08:15  Finished running 1 table model in 0 hours 0 minutes and 13.59 seconds (13.59s).
[2023-10-26, 03:08:15 UTC] {{subprocess.py:94}} INFO - �[0m03:08:15
[2023-10-26, 03:08:15 UTC] {{subprocess.py:94}} INFO - �[0m03:08:15  �[32mCompleted successfully�[0m
[2023-10-26, 03:08:15 UTC] {{subprocess.py:94}} INFO - �[0m03:08:15
[2023-10-26, 03:08:15 UTC] {{subprocess.py:94}} INFO - �[0m03:08:15  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1
[2023-10-26, 03:08:16 UTC] {{subprocess.py:98}} INFO - Command exited with return code 0
[2023-10-26, 03:08:16 UTC] {{local.py:86}} WARNING - Artifact schema version: https://schemas.getdbt.com/dbt/manifest/v10.json is above dbt-ol supported version 7. This might cause errors.
[2023-10-26, 03:08:16 UTC] {{taskinstance.py:1768}} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/cosmos/operators/local.py", line 449, in execute
    self.build_and_run_cmd(context=context, cmd_flags=cmd_flags)
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/cosmos/operators/local.py", line 361, in build_and_run_cmd
    result = self.run_command(cmd=dbt_cmd, env=env, context=context)
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/cosmos/operators/local.py", line 235, in run_command
    self.calculate_openlineage_events_completes(env, Path(tmp_project_dir))
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/cosmos/operators/local.py", line 283, in calculate_openlineage_events_completes
    events = openlineage_processor.parse()
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/openlineage/common/provider/dbt/processor.py", line 177, in parse
    manifest, run_result, profile, catalog = self.get_dbt_metadata()
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/openlineage/common/provider/dbt/local.py", line 186, in get_dbt_metadata
    profile = self.load_yaml_with_jinja(
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/openlineage/common/provider/dbt/local.py", line 133, in load_yaml_with_jinja
    return self.render_values_jinja(
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/openlineage/common/provider/dbt/local.py", line 156, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/openlineage/common/provider/dbt/local.py", line 156, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/openlineage/common/provider/dbt/local.py", line 156, in render_values_jinja
    parsed_dict[key] = cls.render_values_jinja(
  [Previous line repeated 1 more time]
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/openlineage/common/provider/dbt/local.py", line 166, in render_values_jinja
    return environment.from_string(value).render()  # type: ignore
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/jinja2/environment.py", line 1105, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/jinja2/environment.py", line 768, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/airflow/.local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateAssertionError: No filter named 'as_native'.
[2023-10-26, 03:08:16 UTC] {{standard_task_runner.py:100}} ERROR - Failed to execute job 13694 for task dbt_dag.my_dbt_run (No filter named 'as_native'.; 944)

@tatiana
Copy link
Collaborator

tatiana commented Oct 26, 2023

@sylashsu thank you very much for reporting this issue!
Could you confirm how you instantiated your DbtDag or DbtTaskGroup? I'm specially interested how you set the profile_config and operator_args arguments.

If you're not yet using this yet, could you please try:

operator_args = {"append_env": True}

We can probably improve this by implementing this ticket: #595

@sylashsu
Copy link

@tatiana Thanks for your answer.

I used the append_env parameter to get the environment variables.

My DbtTaskGroup is set up as follows:

# Variables
DBT_PROJECT_PATH = f"{os.environ['AIRFLOW_HOME']}/dags/dbt/my_dbt_project/"
DBT_CONNECTION_PATH = f"{os.environ['AIRFLOW_HOME']}/dags/dbt/connection_profiles/"
DBT_EXECUTABLE_PATH = f"{os.environ['AIRFLOW_HOME']}/dbt_venv/bin/dbt"
SERVICE_ACCOUNT_INFO = Variable.get("GCP_CREDENTIALS", deserialize_json=False)
os.environ["GCP_CREDENTIALS"] = SERVICE_ACCOUNT_INFO


with DAG(
    dag_id=DAG_ID,
    description="Run dbt model",
    default_args=DEFAULT_ARGS,
    dagrun_timeout=timedelta(minutes=60),
    start_date=days_ago(1),
    schedule_interval="0 3 * * *",
    catchup=False,
) as dag:
    profile_config = ProfileConfig(
        profile_name="my_dbt_project",
        target_name="dev",
        profiles_yml_filepath=f"{DBT_CONNECTION_PATH}profiles.yml",
    )
    project_config = ProjectConfig(
        dbt_project_path=Path(DBT_PROJECT_PATH),
    )
    render_config = RenderConfig(
        load_method=LoadMode.DBT_LS,
    )
    execution_config = ExecutionConfig(
        dbt_executable_path=DBT_EXECUTABLE_PATH,
    )

    start = EmptyOperator(task_id="pre_dbt")

    end = EmptyOperator(task_id="post_dbt")

    dbt_dag = DbtTaskGroup(
        profile_config=profile_config,
        project_config=project_config,
        execution_config=execution_config,
        group_id="dbt_dag",
        operator_args={
            "append_env": True,
            "fail_fast": True,
            "full_refresh": True,
        },
    )

    start >> dbt_dag >> end

@tatiana
Copy link
Collaborator

tatiana commented Oct 27, 2023

@sylashsu, thanks for getting back so quickly!

The openlineage library will improve to avoid this type of issue:
OpenLineage/OpenLineage#2215

In the meantime, I'll aim to release early next week to ensure Cosmos users are not blocked.

@tatiana tatiana reopened this Oct 27, 2023
@tatiana tatiana added priority:high High priority issues are blocking or critical issues without a workaround and large impact and removed awaiting_response labels Oct 27, 2023
@tatiana tatiana modified the milestones: 1.3.0, 1.2.2 Oct 27, 2023
@tatiana tatiana added the bug Something isn't working label Oct 27, 2023
@JDarDagran
Copy link

@ivanstillfront we'll be soon releasing a possible fix to most cases with failing renders: OpenLineage/OpenLineage#2221

However, I still don't recognize where OL integration would try to read value with run_started_at variable - it tried to render whole dbt_project.yml and needed section from profiles.yml. From issue description it seems the variable was used in model.

Anyway, I'll be grateful if you could please check if the issue is solved in your case. Thanks for the feedback!

@tatiana
Copy link
Collaborator

tatiana commented Nov 2, 2023

@JDarDagran thank you very much for promptly working on this!
@sylashsu Could you please try to upgrade openlineage-integration-common>=1.5.0 and check if the error was solved?

This version was just released:

@ivanstillfront
Copy link
Contributor Author

@ivanstillfront we'll be soon releasing a possible fix to most cases with failing renders: OpenLineage/OpenLineage#2221

However, I still don't recognize where OL integration would try to read value with run_started_at variable - it tried to render whole dbt_project.yml and needed section from profiles.yml. From issue description it seems the variable was used in model.

Anyway, I'll be grateful if you could please check if the issue is solved in your case. Thanks for the feedback!

@JDarDagran I haven't tried yet, but you are correct that run_started_at was part of the model:

{{ config(materialized = 'table', tags=['test']) }}

select
    try_to_timestamp('{{ run_started_at }}') as run_started_at

@sylashsu
Copy link

sylashsu commented Nov 3, 2023

@sylashsu, thanks for getting back so quickly!

The openlineage library will improve to avoid this type of issue: OpenLineage/OpenLineage#2215

In the meantime, I'll aim to release early next week to ensure Cosmos users are not blocked.

@tatiana
This worked for me after upgrading to openlineage-integration-common, thanks!!

@tatiana tatiana closed this as completed Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting_response bug Something isn't working priority:high High priority issues are blocking or critical issues without a workaround and large impact
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants