prefect-dbt include the PrefectDbtRunner class, which provides an improved interface for running dbt Core commands with better logging, failure handling, and automatic asset lineage.
Basic usage
.invoke() in a flow or task, each node in dbt’s execution graph is reflected as a task in Prefect’s execution graph.
Logs from each node will belong to the corresponding task, and each task’s state is determined by the state of that node’s execution.
Assets
Prefect Cloud maintains a graph of assets, objects produced by your workflows. Any dbt seed, source or model will appear on your asset graph in Prefect Cloud once it has been executed using thePrefectDbtRunner.
The upstream dependencies of an asset materialized by prefect-dbt are derived from the depends_on field in dbt’s manifest.json.
The asset’s key will be its corresponding dbt resource’s relation_name.
The name asset property is derived from the dbt resource’s relation_name with adapter-specific quoting characters removed (for example, "dev"."main_marts"."product_metrics" becomes dev.main_marts.product_metrics). The description property is populated from the dbt resource’s description.
The owners asset property is populated if there is data assigned to the owner key under a resource’s meta config.
dbt settings
ThePrefectDbtSettings class, based on Pydantic’s BaseSettings class, automatically detects DBT_-prefixed environment variables that have a direct effect on the PrefectDbtRunner class.
If no environment variables are set, dbt’s defaults are used.
Provide a PrefectDbtSettings instance to PrefectDbtRunner to customize dbt settings or override environment variables.
Logging
ThePrefectDbtRunner class maps all dbt log levels to standard Python logging levels, so filtering for log levels like WARNING or ERROR in the Prefect UI applies to dbt’s logs.
By default, the logging level used by dbt is Prefect’s logging level, which can be configured using the PREFECT_LOGGING_LEVEL Prefect setting.
The dbt logging level can be set independently from Prefect’s by using the DBT_LOG_LEVEL environment variable, setting log_level in PrefectDbtSettings, or passing the --log-level flag or log_level kwarg to .invoke().
Only logging levels of higher severity (more restrictive) than Prefect’s logging level will have an effect.
profiles.yml templating
The PrefectDbtRunner class supports templating in your profiles.yml file, allowing you to reference Prefect blocks and variables that will be resolved at runtime.
This enables you to store sensitive credentials securely using Prefect blocks, and configure different targets based on the Prefect workspace.
For example, a Prefect variable called target can have a different value in development (dev) and production (prod) workspaces.
This allows you to use the same profiles.yml file to automatically reference a local DuckDB instance in development and a Snowflake instance in production.
Failure handling
By default, any dbt node execution failures cause the entire dbt run to raise an exception with a message containing detailed information about the failure.PrefectDbtRunner’s raise_on_failure option can be set to False to prevent failures in dbt from causing the failure of the flow or task in which .invoke() is called.
Native dbt configuration
You can disable automatic asset lineage detection for all resources in your dbt project config, or for specific resources in their own config:See also
- SDK reference — full
PrefectDbtRunnerAPI.