What Are Sources?
In dbt, sources represent raw data tables from external systems, such as an operational database, CRM, or third-party APIs. Instead of referencing raw tables directly in models, dbt allows you to define sources in a centralized file (sources.yml) for better organization, maintainability, and documentation.
The sources.yml file is a crucial component in dbt projects, centralizing metadata about raw data tables. This ensures consistency, maintainability, and automatic documentation.
Why Use Sources?
- Centralizes raw table definitions – Avoids hardcoded table names across multiple models.
- Improves maintainability – If raw table locations change, you only need to update
sources.yml. - Enables freshness checks – dbt can monitor source data latency.
- Enhances documentation – Automatically generates lineage graphs and model dependencies.
Defining Sources in YAML
Sources are defined in.yml files under the sources: key. Here’s a typical example:
- We’ve defined a source named
jaffle_shopthat points to tables in theraw.jaffle_shopschema - We’ve defined two tables:
ordersandcustomers - We’ve added column-level tests to the
orderstable
Using Sources in Models
Once sources are defined, you can reference them using thesource() function in your dbt models:
- Consistency: Source references are standardized across your project
- Refactoring: If a source table moves, you only need to update one place
- Documentation: dbt automatically builds lineage from sources to models
- Testing: You can apply tests to sources for early validation
Best Practices for Source Organization
Group Related Sources
Organize sources by system or domain:Document Your Sources
Add descriptions to help your team understand the data:Apply Tests to Sources
Find data quality issues early by testing your sources:Source Freshness
One of the most powerful features of sources is the ability to check data freshness - ensuring your source data is up-to-date before you build models on top of it.Configuring Freshness Checks
Add afreshness block and specify a loaded_at_field in your sources definition:
- Uses the
updated_atcolumn to determine when data was last loaded - Warns if data is more than 12 hours old
- Errors if data is more than 24 hours old
Running Freshness Checks
Check freshness with:Table-Specific Freshness
You can override source-level freshness settings for specific tables:real_time_stock has stricter freshness requirements than other tables in the source.
Advanced Source Configurations
Source Overrides by Environment
You can override source details for different environments by using custom schemas:Filtering Source Data
For large source tables, you can define filter conditions:Automating Source Definitions with DinoAI
With DinoAI (Paradime’s AI Agent), you can automatically generate source definitions with appropriate freshness configurations. See step by step instructions for more details.Benefits of automating Source definitions with DinoAI:✅ Scans your data warehouse and auto-generates the correct table definitions.
✅ Prevents manual errors in source definitions.
✅ Keeps sources up to date with your evolving data warehouse schema.
✅ Prevents manual errors in source definitions.
✅ Keeps sources up to date with your evolving data warehouse schema.