Skip to main content
View on GitHub Transform unreliable dbt scripts into production-grade data pipelines with enterprise observability, automatic failure recovery, and zero-downtime deployments. When you combine Prefect with dbt, you get the perfect marriage of best-in-class analytics tools:
  • Python gives you the flexibility to integrate with any data source, API, or system your analytics need.
  • dbt Core handles the heavy lifting of SQL transformations, testing, and documentation.
  • Prefect wraps the entire workflow in battle-tested orchestration: automatic retries, scheduling, and observability.
The result? Your analytics team gets reliable, observable data pipelines that leverage the strengths of both platforms. Point this combo at any warehouse and it will transform your data while providing enterprise-grade workflow management.
Note: This example uses dbt Core (the open-source CLI). For dbt Cloud integration, see the dbt Cloud guide in the Prefect documentation.
This example demonstrates these Prefect features:
  • @task – wrap dbt commands in retries & observability.
  • log_prints – surface dbt output automatically in Prefect logs.
  • Automatic retries with exponential back-off for flaky network connections.
  • prefect-dbt integration – native dbt execution with enhanced logging and failure handling.

The Scenario: Reliable Analytics Workflows

Your analytics team uses dbt to model data in DuckDB for rapid local development and testing, but deploys to Snowflake in production. You need a workflow that:
  • Anyone can run locally without complex setup (DuckDB)
  • Automatically retries on network failures or temporary dbt errors
  • Provides clear logs and observability for debugging
  • Can be easily scheduled and deployed to production

Our Solution

Write three focused Python functions (download project, run dbt commands, orchestrate workflow), add Prefect decorators, and let Prefect handle retries, logging, and scheduling. The entire example is self-contained – no git client or global dbt configuration required. For more on integrating Prefect with dbt, see the prefect-dbt guide.

Running the example locally

Watch as Prefect orchestrates the complete dbt lifecycle: downloading the project, running models, executing tests, and materializing results. The flow creates a local DuckDB file you can explore with any SQL tool.

Code walkthrough

  1. Project Setup – Download and cache a demo dbt project from GitHub
  2. dbt CLI Wrapper – Execute dbt commands with automatic retries and logging using prefect-dbt
  3. Orchestration Flow – Run the complete dbt lifecycle in sequence
  4. Execution – Self-contained example that works out of the box

Project Setup – download and cache dbt project

To keep this example fully self-contained, we download a demo dbt project directly from GitHub as a ZIP file. This means users don’t need git installed. Learn more about tasks in the Prefect documentation

Create profiles.yml for DuckDB – needed for dbt to work

This task creates a simple profiles.yml file for DuckDB so dbt can connect to the database. This keeps the example self-contained.

dbt CLI Wrapper – execute commands with retries and logging using prefect-dbt

This task uses the modern PrefectDbtRunner from prefect-dbt integration which provides native dbt execution with enhanced logging, failure handling, and automatic event emission. Learn more about retries in the Prefect documentation

Orchestration Flow – run the complete dbt lifecycle

This flow orchestrates the standard dbt workflow: deps → seed → run → test. Each step is a separate task run in Prefect, providing granular observability and automatic retry handling for any step that fails. Now using the flexible prefect-dbt integration for enhanced dbt execution. Learn more about flows in the Prefect documentation

What Just Happened?

Here’s the sequence of events when you run this flow:
  1. Project Download – Prefect registered a task run to download and extract the dbt project from GitHub. The task uses Prefect’s task cache with a custom cache_key_fn that folds the archive URL together with the local directory’s existence, so repeat runs inside an hour reuse the prior download but a deleted prefect_dbt_project/ directory invalidates the cache and forces a fresh fetch.
  2. Profiles Setup – A second task run wrote a local profiles.yml pointing dbt at the on-disk DuckDB file.
  3. dbt Lifecycle – Four further task runs executed the standard dbt workflow: deps, seed, run, and test.
  4. Native dbt Integration – Each dbt command was executed through prefect-dbt for enhanced logging, failure handling, and automatic event emission.
  5. Automatic Retries – Each dbt command would automatically retry on failure (network issues, temporary dbt errors, etc.).
  6. Centralized Logging – All dbt output streamed directly to Prefect logs with proper log level mapping.
  7. Event Emission – Prefect automatically emitted events for each dbt node execution, enabling advanced monitoring and alerting.
  8. Local Results – A DuckDB file appeared at prefect_dbt_project/demo.duckdb ready for analysis.
Prefect + prefect-dbt transformed a series of shell commands into a resilient, observable workflow — no hand-authored YAML, no cron jobs, just Python with enterprise-grade dbt integration. (dbt’s own profiles.yml still lives next to the project; this flow writes it programmatically so you never have to edit it by hand.)

Why This Matters

Traditional dbt orchestration often involves brittle shell scripts, complex YAML configurations, or heavyweight workflow tools. Prefect with the prefect-dbt integration gives you enterprise-grade orchestration with zero operational overhead:
  • Reliability: Automatic retries with exponential backoff handle transient failures
  • Native Integration: PrefectDbtRunner provides enhanced logging, failure handling, and event emission
  • Observability: Every dbt command and node is logged, timed, and searchable in the Prefect UI with proper log level mapping
  • Event-Driven: Automatic Prefect events for dbt node status changes enable advanced monitoring and alerting
  • Portability: The same Python file runs locally, in CI/CD, and in production
  • Composability: Easily extend this flow with data quality checks, Slack alerts, or downstream dependencies
This pattern scales from prototype analytics to production data platforms. Whether you’re running dbt against DuckDB for rapid local iteration or Snowflake for enterprise analytics, Prefect ensures your workflows are reliable, observable, and maintainable. To learn more about orchestrating analytics workflows with Prefect, check out: