Dagster Python Tutorial: Build Your First Data Assets
Dagster Python development centres on a simple idea: describe the data you want to exist, and let the framework work out how and when to produce it. Every asset is a regular Python function decorated with @asset. Dependencies come from function parameters, and a Definitions object tells Dagster which assets, resources and schedules belong to your project.
This tutorial builds a small pipeline that loads orders from a CSV file and calculates daily revenue. The example is deliberately small, but the same patterns scale to hundreds of assets spread across warehouses, dbt models and machine learning steps.
Setting Up the Project
Create a virtual environment and install the core packages plus pandas for the transformation logic. Keep your code in a single module at first, then split it into a package as the project grows.
pip install dagster dagster-webserver pandas
A typical early layout has an assets module, a definitions module and a tests folder. Clear separation from day one makes later refactoring much easier.
Defining Your First Assets
An asset function returns the data it produces. The function name becomes the asset key, which appears in the UI's asset graph. Docstrings are shown in the UI too, so a short description helps teammates understand each asset.
import pandas as pd
from dagster import asset
@asset
def raw_orders() -> pd.DataFrame:
"""Orders loaded from the daily export."""
return pd.read_csv("orders.csv")
@asset
def daily_revenue(raw_orders: pd.DataFrame) -> pd.DataFrame:
"""Revenue totals grouped by day."""
return raw_orders.groupby("day", as_index=False)["amount"].sum()
How dependencies are inferred
The daily_revenue function has a parameter named raw_orders. Dagster matches that name to the upstream asset and draws an edge in the lineage graph. No separate dependency list is required, which keeps definitions readable and reduces mistakes.
Registering Assets with Definitions
The Definitions object is the entry point Dagster loads. It collects assets, resources, jobs, schedules and sensors into one deployable unit, often called a code location.
from dagster import Definitions
defs = Definitions(assets=[raw_orders, daily_revenue])
Run dagster dev pointing at this module, open the UI and click to materialise both assets. Dagster executes them in dependency order and records metadata about each run.
Using Resources for External Systems
Hard-coding database credentials or file paths inside assets makes testing painful. Resources solve this by providing external connections that can be swapped per environment. A configurable resource is a small class whose fields hold settings such as a connection string.
| Concept | Purpose | Example |
|---|---|---|
| Asset | Produces a piece of data | A cleaned orders table |
| Resource | Supplies an external connection | A warehouse client |
| IO manager | Controls how outputs are stored and loaded | Writing DataFrames to Parquet |
| Definitions | Bundles everything Dagster loads | The project entry point |
In production, the resource might connect to a cloud warehouse. In tests, the same asset can receive a lightweight local substitute, keeping tests fast and free of side effects.
Testing Your Assets
Because assets are plain functions, you can call them directly with sample data. For integration-style tests, the materialize helper runs selected assets in-process and returns a result you can inspect.
from dagster import materialize
def test_pipeline():
result = materialize([raw_orders, daily_revenue])
assert result.success
Asset checks add data quality rules that run alongside materialisations. Typical checks confirm that a table is not empty, that keys are unique or that values fall within expected ranges.
Organising a Growing Project
As assets multiply, structure becomes important. Group related assets into modules, use asset groups to organise the UI and keep shared resources in one place.
- Name assets after the data they represent, not the action that creates them.
- Keep transformation logic small and focused within each asset.
- Add metadata such as row counts to make the UI more informative.
- Use partitions when data naturally splits by date or region.
- Introduce schedules or declarative automation only after assets are stable.
Adding Metadata and Descriptions
Metadata makes the UI far more informative. Assets can attach values such as row counts, previews or links to each materialisation, and those values appear in the asset details page. Over time, this history helps teams spot unusual changes, such as a table that suddenly shrinks.
Descriptions and owners are equally useful. Documenting what an asset represents and who maintains it turns the asset graph into a living catalogue that new teammates can explore on their own.
Working with Partitions
Partitions divide an asset into slices, commonly by date. A daily partitioned asset processes one day of data per run, which keeps runs small and makes reprocessing specific days straightforward. Backfills then launch runs for a range of partitions when historical data needs updating.
- Choose daily or hourly partitions for event-style data.
- Keep partition definitions consistent across dependent assets.
- Launch backfills in manageable ranges to control resource usage.
Debugging Failed Materialisations
When an asset fails, the run page in the UI shows structured logs, the full Python traceback and which downstream assets were skipped. Re-running only the failed asset after a fix saves time, because successful upstream outputs do not need recomputing.
Adding context.log messages inside assets makes debugging easier. Those messages appear in the run timeline, giving clear clues about which step or input caused the problem.
Frequently Asked Questions
Do I need to know advanced Python to use Dagster?
Comfort with functions, decorators and type hints is enough to start. Advanced features become useful as projects grow.
Can Dagster assets return objects other than DataFrames?
Yes. Assets can return many Python objects, and IO managers decide how those outputs are stored and loaded.
What is a code location?
A code location is a set of definitions that Dagster loads together, typically a Python module or package exposing a Definitions object.
How do I run only one asset?
Select the asset in the UI and materialise it, or pass a selection when using the materialize helper in code.
Conclusion
Dagster Python projects begin with a handful of decorated functions and grow into well-structured, testable pipelines. Define assets clearly, let parameters express dependencies, isolate external systems in resources and test with the materialize helper. Mastering these fundamentals makes every later feature, from schedules to partitions, far easier to adopt.



