This prompt is for analytics engineers and data platform developers who need to recover from a dbt model compilation failure. The job-to-be-done is straightforward: take a failing dbt model SQL file, the exact compilation error from the dbt run, and the relevant project context (such as upstream model schemas or ref() definitions), and produce a corrected model file that compiles successfully. The ideal user is someone who understands dbt and SQL but wants to accelerate the fix cycle, not someone expecting the prompt to replace their own understanding of the project's business logic.
Prompt
dbt Model Compilation Error Recovery Prompt

When to Use This Prompt
Define the job, reader, and constraints for the dbt Model Compilation Error Recovery Prompt.
Use this prompt when the error is a compilation error—syntax mistakes, undefined macros, incorrect ref() or source() calls, Jinja templating errors, or missing configuration blocks. It is not designed for runtime execution errors (e.g., a UNIQUE constraint violation in the warehouse) or for logical bugs where the SQL compiles but returns incorrect results. The prompt requires three concrete inputs: the failing model SQL, the full error message from dbt, and a project context block that includes at least the schema of any upstream models referenced by ref(). Without the project context, the model cannot reliably fix broken references and may hallucinate column names.
Do not use this prompt for models that fail due to missing data, warehouse permissions, or infrastructure issues. It is also a poor fit for models where the compilation error is a symptom of a deeper design problem that requires a human to rethink the transformation logic. After using the prompt, you must validate the output by running dbt compile on the corrected model and checking that all downstream ref() relationships remain intact. For high-stakes production models, a human reviewer should approve the fix before it is merged. The next section provides the copy-ready prompt template you can adapt to your project.
Use Case Fit
Where the dbt Model Compilation Error Recovery Prompt works, where it fails, and the operational preconditions required before you put it into a production harness.
Good Fit: Deterministic Compilation Errors
Use when: The error is a syntax error, undefined macro, missing ref, or YAML config mismatch that the compiler can pinpoint to a specific line. Guardrail: The prompt works best when the error message is explicit and the model SQL is self-contained. Always verify the fix compiles in a CI check before merging.
Bad Fit: Semantic Logic Bugs
Avoid when: The model compiles but produces incorrect results due to a join cardinality error, wrong grain, or a WHERE clause logic flaw. Guardrail: This prompt repairs compilation failures, not data-quality bugs. Semantic errors require a separate data-diff or row-count validation prompt with source-grounding.
Required Input: Full Project Context
What to watch: Supplying only the failing SQL without upstream model schemas, macros, or YAML configs causes the model to hallucinate column names or ref signatures. Guardrail: The harness must assemble the failing model, the exact compiler error, the project's schema.yml for referenced models, and any custom macro definitions before calling the prompt.
Operational Risk: Downstream Ref Breakage
What to watch: A fix that compiles locally can still break downstream models if column names, data types, or materialization strategies change silently. Guardrail: The harness must run dbt compile on all downstream dependents after applying the fix and flag any new compilation errors before the PR is accepted.
Operational Risk: Over-Confident Hallucination
What to watch: The model may invent a plausible-looking macro, source table, or column that does not exist in the project when the error context is ambiguous. Guardrail: The harness must validate that every ref(), source(), and macro call in the corrected SQL resolves against the project manifest. Reject any fix that introduces unresolved references.
Escalation Threshold: Multi-File Refactors
What to watch: A single compilation error may indicate a breaking change that requires updating multiple models, staging files, or base macros. Guardrail: If the corrected output touches more than one file or proposes a macro signature change, escalate to a human analytics engineer for review instead of auto-applying the fix.
Copy-Ready Prompt Template
A reusable prompt template for diagnosing and correcting dbt model compilation errors.
The following prompt template is designed to be copied directly into your AI harness, IDE, or orchestration layer. It accepts the failing SQL, the exact compiler error, and the broader project context to produce a corrected model file. The template uses square-bracket placeholders for all dynamic inputs, ensuring it can be parameterized programmatically before each inference call. Treat the output as a suggested fix, not an authoritative change—always validate the corrected SQL by running dbt compile in your target environment.
textYou are an expert analytics engineer specializing in dbt and SQL transformation. Your task is to diagnose and fix a dbt model that failed to compile. ## INPUT - Failing dbt Model SQL: ```sql [MODEL_SQL]
- Compilation Error Message:
text[COMPILATION_ERROR]
- Project Context (optional):
- Target Database Dialect: [DB_DIALECT]
- Referenced Models and their schemas: [REFERENCED_MODELS]
- dbt Project Configuration (relevant macros, variables, packages): [PROJECT_CONFIG]
OUTPUT_SCHEMA
Return a valid JSON object with the following structure: { "error_classification": "syntax_error | undefined_model | invalid_jinja | type_mismatch | config_error | other", "root_cause_summary": "A one-sentence explanation of what caused the failure.", "corrected_model_sql": "The full, corrected SQL for the model file. Ensure it is syntactically valid for the target dialect.", "change_log": [ { "line_number": 12, "description": "Added a comma after the 'status' column." } ], "downstream_impact_assessment": "An analysis of whether the fix changes the model's grain, column names, or data types, and whether any downstream refs need updating.", "confidence_score": 0.95 }
CONSTRAINTS
- Do not change the model's logical intent or grain unless absolutely necessary to resolve the error.
- Preserve all existing column names and aliases. If a column must be renamed, flag it explicitly in the downstream impact assessment.
- If the error involves an undefined reference, check if it's a typo, a missing ref, or a missing source definition before suggesting a fix.
- For Jinja errors, ensure the corrected template is syntactically valid and does not introduce new undefined variables.
- If you cannot determine a fix with high confidence, set the confidence score below 0.7 and provide a clear explanation in the root cause summary.
- Do not invent table names, column names, or business logic. Only use information present in the input.
To adapt this template for your environment, replace the placeholders with data from your dbt run artifacts. The [MODEL_SQL] should be the raw file content, and [COMPILATION_ERROR] should be the exact stderr or logs from the dbt compile command. For [REFERENCED_MODELS], you can inject the output of dbt ls or parse your manifest.json to provide upstream schemas. The [PROJECT_CONFIG] placeholder is critical for Jinja-heavy projects; include any custom macros or variables that the failing model uses. If your harness cannot provide rich project context, remove the optional fields from the prompt to avoid confusing the model with empty sections.
Prompt Variables
Required inputs for the dbt model compilation error recovery prompt. Each placeholder must be populated before the prompt is assembled and sent to the model. Validation notes describe how the harness should check each input before execution.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[FAILING_MODEL_SQL] | The complete SQL content of the dbt model file that failed compilation | SELECT * FROM {{ ref('stg_orders') }} WHERE order_date = '2024-01-01 | Parse check: must be non-empty string. Verify file path exists in dbt project. Reject if over 100KB to prevent context overflow. |
[COMPILATION_ERROR] | The full error message returned by dbt compile or dbt run | Compilation Error in model my_model (models/marts/my_model.sql) 'ref' is undefined. Did you mean 'ref'? | Parse check: must contain error keyword or dbt stack trace pattern. Strip ANSI color codes before insertion. Truncate at 2000 chars if longer. |
[PROJECT_CONTEXT] | Relevant dbt project configuration including schema.yml, dbt_project.yml, and any macros referenced by the failing model | schema.yml defines stg_orders with columns: order_id, order_date, customer_id. dbt_project.yml sets materialized='table' for marts. | Schema check: must include at minimum the model's schema definition and materialization config. Null allowed if project context is unavailable, but confidence will degrade. |
[DOWNSTREAM_REFS] | List of models, snapshots, or exposures that depend on the failing model, with their ref names | ['fct_sales', 'dim_customer_orders', 'weekly_revenue'] | Parse check: must be a JSON array of strings. Empty array allowed if no downstream dependencies exist. Validate each ref exists in manifest.json. |
[DIALECT] | The target SQL dialect for the dbt adapter in use | snowflake | Enum check: must match one of ['snowflake', 'bigquery', 'redshift', 'postgres', 'databricks', 'duckdb', 'trino']. Default to 'snowflake' if null. Reject unknown dialects. |
[RETRY_ATTEMPT] | The current retry count for this model compilation failure, used to adjust repair strategy and escalation threshold | 2 | Range check: must be integer 0-5. If value is 3 or higher, prompt should include escalation instructions. Null defaults to 0. |
[MANIFEST_JSON_SNIPPET] | Relevant excerpt from dbt manifest.json showing the failing model's node config and upstream refs | {"nodes": {"model.jaffle_shop.my_model": {"depends_on": {"nodes": ["model.jaffle_shop.stg_orders"]}}}} | Schema check: must be valid JSON with nodes key. Null allowed. If provided, harness should extract and validate node ID matches failing model name. |
Implementation Harness Notes
How to wire the dbt compilation error recovery prompt into a CI/CD pipeline or local development workflow with validation, retries, and safe file handling.
This prompt is designed to be called programmatically from a dbt Cloud job, a GitHub Actions workflow, or a local pre-commit hook. The harness should capture the failing model's SQL, the full compilation error message from dbt compile or dbt run, and the relevant project context (schema YAML, upstream model refs, and macros if they appear in the error trace). Do not send the entire project repository; instead, extract only the files referenced in the error and the immediate dependency chain. The prompt expects these inputs as structured variables: [FAILING_MODEL_SQL], [COMPILATION_ERROR], and [PROJECT_CONTEXT] (a concatenated string of relevant YAML and SQL files). The harness should also inject [DIALECT] (e.g., snowflake, bigquery, redshift) to prevent syntax mismatches.
Validation and retry logic: After the model returns a corrected SQL block, the harness must write it to a temporary file and run dbt compile --select <model_name> in a sandboxed environment. If compilation fails again, extract the new error and feed it back into the prompt as a second attempt with the [PREVIOUS_ATTEMPT] and [NEW_ERROR] variables populated. Implement a hard retry budget of 3 attempts. If all attempts fail, log the full retry trace, flag the model for human review, and block the PR merge. Downstream ref integrity check: After a successful compilation, run dbt ls --select <model_name>+ to identify all downstream models and execute a dry-run compile on them. Any new failures introduced by the fix must be surfaced to the developer before the change is accepted.
Model choice and latency: Use a model with strong SQL reasoning capabilities (e.g., Claude 3.5 Sonnet, GPT-4o) and set temperature=0 for deterministic fixes. The prompt is stateless, so each retry is an independent call. For CI/CD integration, expect a 5-15 second latency per attempt; this is acceptable for a blocking quality gate but too slow for an interactive IDE loop. Logging and audit: Log every prompt invocation with the model name, input hash, output diff, compilation result, and retry count. Store these logs alongside the dbt artifacts so that analytics engineers can trace why a fix was applied. Human approval gate: For models tagged as critical or marts in the dbt project, require a human to approve the diff before the fix is committed, even if compilation succeeds. The harness should open a PR comment with the before/after SQL diff and the compilation result, then wait for an approving review.
What to avoid: Do not allow the harness to auto-merge fixes without compilation verification. Do not send secrets, environment variables, or full profile configurations to the model. Do not use this prompt for models that fail due to data issues (e.g., division by zero at runtime) — it targets compilation errors only. For runtime errors, use the SQL Query Syntax Error Repair Prompt or the Data Validation Failure Retry Prompt instead. If the error involves a custom macro with complex Jinja logic, the prompt may struggle; in those cases, escalate directly to the macro author rather than exhausting the retry budget.
Expected Output Contract
Defines the required structure, types, and validation rules for the corrected dbt model file returned by the recovery prompt. Use this contract to parse and validate the model's response before applying the fix.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
corrected_model_sql | string | Must be a non-empty string containing valid SQL for the target dialect. Must parse successfully using a SQL parser for the specified dialect (e.g., sqlfluff, sqlparse). | |
compilation_error_resolved | boolean | Must be true. A false value indicates the prompt failed its primary objective. Harness must trigger a retry or escalate. | |
change_summary | string | Must be a non-empty string describing the exact change made. Must not be a generic placeholder like 'fixed error'. Should reference the specific line or clause modified. | |
downstream_refs_intact | boolean | Must be true. Harness must verify this by checking that all | |
linting_errors | array of objects | If present, each object must contain a | |
confidence_score | number | Must be a float between 0.0 and 1.0. A score below a configurable threshold (e.g., 0.8) should trigger a human review step before the fix is applied. | |
requires_full_build | boolean | Must be true if the change impacts materialization logic (e.g., config block, incremental strategy) or a model's grain, otherwise false. Used to warn the operator if a full refresh is needed. | |
alternative_fixes | array of strings | If the primary fix has trade-offs, this array should contain one or more alternative SQL strings. Each must also pass the SQL parse check. Used for human-in-the-loop selection when confidence is low. |
Common Failure Modes
What breaks first when using a dbt model compilation error recovery prompt and how to guard against it.
Refactoring Alters Business Logic
What to watch: The model 'fixes' a compilation error by rewriting a CTE or JOIN condition, inadvertently changing the aggregation grain or filter logic. The SQL compiles but produces incorrect results. Guardrail: Run the corrected model against a static test dataset and compare row counts and aggregate totals to a known-good baseline before merging.
Downstream Ref Breakage
What to watch: The prompt renames a model, alias, or column to resolve a compilation error, which silently breaks {{ ref('...') }} or source() calls in downstream models. Guardrail: After generating a fix, run dbt ls --select <model>+ to identify all dependents and verify they still compile. Include a --no-partial-parse flag to avoid false positives from the cache.
Jinja and Macro Misdiagnosis
What to watch: The error message points to a line inside a compiled Jinja block, but the root cause is in a macro or a {% if %} logic branch. The prompt patches the compiled output instead of the macro source, so the error returns on the next run. Guardrail: Provide the raw model file, not the compiled SQL, in the prompt. Instruct the model to trace errors back to the Jinja source and output a unified diff for the macro file if needed.
Incomplete Context Causes Hallucination
What to watch: The prompt invents column names, source tables, or macro signatures because the project's schema.yml or macro definitions were not provided. The 'fix' introduces new compilation errors. Guardrail: Bundle the model file, its schema.yml, and any custom macros it calls into the prompt context. Use a retrieval step to pull relevant project files before calling the model.
Retry Loop on Unfixable Errors
What to watch: The model encounters a fatal error (e.g., a missing source table, a database permission issue) that no SQL rewrite can fix. The prompt repeatedly suggests syntax tweaks, wasting tokens and delaying escalation. Guardrail: Classify the error type before invoking the recovery prompt. If the error is an infrastructure or permissions issue, bypass the prompt and escalate to the platform team immediately.
Materialization Config Drift
What to watch: The prompt changes the model's materialized config (e.g., from table to view) or its on_schema_change setting to make it compile, altering performance and data freshness behavior. Guardrail: Add a constraint in the prompt to preserve the original {{ config(...) }} block. Validate the output with a simple diff check that flags any config changes for human review.
Evaluation Rubric
Use this rubric to test whether the corrected dbt model output is production-ready before merging. Each criterion targets a specific failure mode in compilation error recovery.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Compilation Success | Corrected SQL compiles without errors in the target dbt adapter | dbt compile returns a non-zero exit code or new compilation error | Run dbt compile --select [MODEL_NAME] in the project environment and check exit code |
Downstream Ref Integrity | All ref() and source() calls resolve to valid project nodes | dbt compile reports undefined model or source references | Parse manifest.json or run dbt ls to verify all referenced nodes exist |
Semantic Preservation | Corrected model produces logically equivalent results to the original intent | Output columns, filters, or join logic differ from original specification | Compare compiled SQL AST or run dbt test on the model against known expected outputs |
Materialization Compatibility | Corrected SQL works with the model's configured materialization (table, view, incremental, ephemeral) | Incremental merge fails, ephemeral CTE breaks, or view materialization violates dialect constraints | Execute dbt run --select [MODEL_NAME] and verify the materialization type succeeds |
Dialect Compliance | Corrected SQL uses only syntax, functions, and features supported by the target adapter | Adapter-specific error (e.g., BigQuery STRUCT vs Snowflake OBJECT syntax) | Validate SQL against the adapter's documented SQL dialect or run a dry-run query if supported |
Macro and Jinja Correctness | All Jinja templating and custom macros evaluate without errors | Jinja rendering error, undefined macro, or incorrect macro argument count | Run dbt parse and check for Jinja compilation errors in the model file |
Column Lineage Preservation | All columns required by downstream models or exposures remain present and correctly typed | Downstream model fails due to missing or renamed column | Check downstream model dependencies with dbt ls and verify column names match the original schema |
Retry Budget Exhaustion | Correction is produced within the configured retry budget; escalation flag is set if budget exceeded | Model enters infinite retry loop or correction attempt count exceeds [MAX_RETRIES] without escalation | Inspect harness logs for retry count and escalation flag after each correction attempt |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt
Use the base prompt with a single model SQL file and error message. Skip the project-context injection and focus on getting a compilable fix. Accept the first valid output without retry logic.
codeSYSTEM: You are a dbt debugging assistant. Fix the compilation error. USER: Model SQL: [MODEL_SQL] Error: [COMPILATION_ERROR] Return only the corrected SQL.
Watch for
- The fix may compile but break downstream refs
- No validation that the corrected model produces the same columns
- Overly aggressive rewrites that change business logic

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us