Schedule icon
Webhook icon
AIAgent icon
GoogleGemini icon
Script icon
Process icon
If icon
SlackIncomingWebhook icon

dbt Test Failure AI Root Cause and Remediation Advisor

Ingest failed dbt tests, diagnose join fan-outs and null anomalies with Gemini AI, and generate SQL remediation briefs automatically.

Categories
AICoreData

Data transformations orchestrated with dbt rely on data tests (unique, not_null, relationships, and custom singular assertions) to ensure dimensional integrity before downstream BI dashboards refresh. However, when nightly batch runs fail with cryptic errors (such as 14 records returned where count > 1), on-call analytics engineers face significant toil.

Engineers must manually copy compiled SQL queries from target folders, trace macro expansions, inspect database catalogs, and determine whether the duplication stemmed from a join fan-out, unhandled null keys, or upstream schema drift.

This blueprint automates dbt failure triage. When a test fails in dbt Core or dbt Cloud, webhook integration triggers Kestra with the failed test context. An AI agent powered by Google Gemini analyzes the compiled test query and upstream transformation logic, determines the exact failure mechanism (e.g. 1-to-many payment relationship inflating order rows), categorizes severity, and generates an executable SQL remediation diff. High-severity failures immediately dispatch an executive triage card to Slack with actionable remediation steps.

How it works

  1. diagnose_dbt_failure (ai.agent.AIAgent): Ingests the failing model name, test query, and upstream transformation SQL, using gemini-2.5-flash with a strict JSON schema to diagnose the failure mechanism, evaluate BI impact, and formulate corrected SQL.
  2. render_remediation_brief (scripts.python.Script): Transforms the diagnosis into a downloadable dbt-remediation-brief.md markdown report containing root cause explanations and ready-to-apply SQL code blocks.
  3. triage_routing_gate (core.flow.If):
    • High / Critical Severity: Alerts #analytics-alerts-urgent in Slack with the diagnosis summary and a direct link to the remediation artifact.
    • Standard Severity: Records the finding in #data-team-feed for routine sprint review.
  4. alert_on_failure (errors block): Catches unexpected script execution errors and notifies the analytics operations team.
  5. Triggers: Morning post-transformation sweep schedule (disabled: true by default) plus an authenticated Webhook trigger for CI/CD and dbt Cloud failure webhooks.

What you get

  • Instant root cause explanation for complex join fan-outs and unexpected duplicates.
  • Actionable SQL diffs ready for copy-pasting into pull requests to unblock pipeline runs.
  • Automated severity triage preventing minor test warnings from waking up on-call engineers.

Who it's for

  • Analytics Engineers and Analytics Team Leads maintaining production dbt projects.
  • Data Engineers orchestrating lakehouse and warehouse ELT pipelines.
  • BI Engineers responsible for executive dashboard metrics accuracy.

Why orchestrate this with Kestra

Writing custom notification webhooks inside dbt packages or shell scripts creates brittle pipelines without persistent artifacts or AI capabilities. Kestra provides declarative DAG orchestration, secure API key management, native Python execution, and structured AI agent tasks for automated triage.

Prerequisites

  • Google Gemini API key with access to gemini-2.5-flash.
  • Slack incoming webhook endpoint for analytics alerts.

Secrets

  • GEMINI_API_KEY: API key for Google Gemini model inference.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL for analytics alert channels.
  • WEBHOOK_KEY: Authentication secret for event-driven webhook invocation.

Quick start

  1. Set GEMINI_API_KEY, SLACK_WEBHOOK_URL, and WEBHOOK_KEY in your Kestra namespace secrets.
  2. Click Execute in the Kestra UI to run the blueprint against the default customer orders join fan-out test.
  3. Download dbt-remediation-brief.md from the Outputs tab and review the Slack card.

How to extend

  • Add an upstream task to fetch run_results.json and manifest.json directly from AWS S3, Google Cloud Storage, or the dbt Cloud Administrative API.
  • Connect GitHub or GitLab plugins inside triage_routing_gate to automatically open a bug issue with the recommended SQL fix attached.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.