dbt-extractor

0.6.0 · active · verified Sun Mar 29

dbt-extractor is a Python library that processes Jinja templates within dbt model files to analyze and extract metadata such as `ref`, `source`, and `config` calls. It is part of the `dbt-labs/dbt-parser-generator` repository. The tool, currently at version 0.6.0, prioritizes 100% certainty in its extraction, raising an exception if it cannot confidently extract values, rather than risking incorrect or incomplete output.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `dbt-extractor` to parse a dbt model's SQL content and extract references to other models (`ref`), sources (`source`), and configurations (`config`). If the extraction is not 100% certain, an `ExtractionError` will be raised.

from dbt_extractor.main import extract_from_source
from dbt_extractor.extractor import ExtractionError

dbt_model_content = """
SELECT
    {{ ref('my_model') }} as model_data,
    {{ source('my_schema', 'my_table') }} as source_data,
    {{ config(materialized='table') }}
FROM some_table
"""

try:
    extracted_data = extract_from_source(dbt_model_content)
    print("Extracted Refs:", extracted_data.refs)
    print("Extracted Sources:", extracted_data.sources)
    print("Extracted Configs:", extracted_data.configs)
except ExtractionError as e:
    print(f"Extraction failed: {e}")

view raw JSON →