Jeandre de Bruyn
12/08/2025, 10:30 AMMarvin
12/08/2025, 10:30 AMMarvin
12/08/2025, 10:37 AMDbtCloudCredentials
- Job wrapper: DbtCloudJob
- Helpers: run_dbt_cloud_job, trigger_dbt_cloud_job_run_and_wait_for_completion, list_dbt_cloud_run_artifacts, get_dbt_cloud_run_artifact
- Links:
- prefect-dbt collection on GitHub
Example
from prefect import flow
from prefect_dbt.cloud.credentials import DbtCloudCredentials
from <http://prefect_dbt.cloud.jobs|prefect_dbt.cloud.jobs> import DbtCloudJob, run_dbt_cloud_job
@flow
def run_dbt_cloud():
creds = DbtCloudCredentials(api_key="***", account_id=123456)
job = DbtCloudJob(credentials=creds, job_id=999999)
result = run_dbt_cloud_job(dbt_cloud_job=job, targeted_retries=3)
return result
2) Run dbt Core via typed helpers (dbt CLI)
- If you run dbt Core yourself (self-hosted), Prefect provides tasks to drive dbt commands and stream logs.
- Key helpers (from prefect_dbt.cli.commands):
- run_dbt_build, run_dbt_model, run_dbt_test, run_dbt_seed, run_dbt_snapshot, run_dbt_source_freshness
- Optional DbtCliProfile to construct a profile in-flow (or just point to profiles_dir).
- Links:
- prefect-dbt collection on GitHub
Example
from prefect import flow
from prefect_dbt.cli.commands import run_dbt_build
@flow
def run_dbt_core():
result = run_dbt_build(
project_dir="/path/to/dbt/project",
profiles_dir="~/.dbt",
stream_output=True
)
return result
3) Run dbt via generic shell commands (flexible fallback)
- For power users or unusual flags, use prefect-shell to execute exactly the CLI you want.
- Link: prefect-shell
Example
from prefect import flow
from prefect_shell.commands import shell_run_command
@flow
def run_dbt_shell():
shell_run_command("dbt build -s tag:nightly --fail-fast", stream_output=True)
Advanced option: PrefectDbtRunner
- The collection exposes PrefectDbtRunner which can work with a dbt manifest to execute dbt nodes programmatically under Prefect orchestration. This is useful if you want more programmatic control over node execution and visibility, but it’s an advanced path.
- Signatures confirmed in the package:
- prefect_dbt.core.runner.PrefectDbtRunner(manifest=None, settings=None, raise_on_failure=True, ...)
Pros of using Prefect with dbt
- End-to-end orchestration: Combine dbt with ingestion, validation, backfills, alerts, and downstream consumers in one stateful DAG with retries, caching, SLAs, concurrency limits, and schedules.
- Infra flexibility and control: Run anywhere (Docker, Kubernetes, ECS, etc.) using Prefect workers and work pools. Keep your dbt Core execution environment fully under your control.
- Observability and governance: Centralize logs, states, notifications, and artifacts for dbt alongside the rest of your data platform runs.
- Keep dbt Cloud where it shines: If you’re on dbt Cloud already, let Cloud execute dbt while Prefect orchestrates upstream/downstream dependencies and business logic around it.
Cons / tradeoffs of Prefect + dbt
- If you run dbt Core yourself:
- You manage the execution environment (adapters, profiles, secrets), scaling, and concurrency.
- No dbt Cloud-native UI for docs/lineage, job management, or historical comparisons out of the box.
- If you trigger dbt Cloud from Prefect:Marvin
12/08/2025, 10:37 AMprefect-dbt for typed commands and logs; fall back to prefect-shell for custom invocations. Start simple (run_dbt_build) and scale infra via Prefect work pools/workers.
- Need programmatic control over node execution or deep, per-node orchestration? Explore PrefectDbtRunner for custom flows based on your manifest.
Useful links
- prefect-dbt collection (dbt Cloud + dbt Core helpers): PrefectHQ/prefect-dbt
- prefect-shell (generic shell runner): PrefectHQ/prefect-shell
- dbt Cloud product overview: dbt Cloud
If you share a bit about your current setup (Cloud vs Core, target infra, and what you want to orchestrate around dbt), I can suggest a concrete pattern and a starter flow tailored to your environment.