Your first workflow
In this page you will run your first end-to-end Bauplan workflow with a toy example. It covers all the primitives you will need to successfully use Bauplan. All the tips and tricks to make Bauplan amenable to production-ready workflows are collected in our how-to guides.
To get started, you will only need:
- Python 3.11 or higher.
- Bauplan installed.
- Your favorite agent set up.
Get the data
This page uses the titanic table throughout. Let's make sure this table is available to you first.
- Public sandbox
- Your own environment
If you are using the public Bauplan sandbox, titanic is already available on the main branch. You can go to the next step!
On your environment, the titanic table is likely missing, but you can load it yourself as follows. First, download the titanic.parquet file from our public storage.
Then, upload it to an object storage readable by your Bauplan deployment. For instance, if your cloud provider is AWS, run
aws s3 cp titanic.parquet s3://<YOUR_BUCKET>/titanic/titanic.parquet
Once the file is uploaded, it won't appear as a table just yet as it needs to be imported into Bauplan first. To this end, create an import branch in your name, such as
bauplan checkout -b <YOUR_USERNAME>.first_import
and register the titanic table by running
bauplan table create titanic --search-uri 's3://<YOUR_BUCKET>/titanic/titanic.parquet'
bauplan table import titanic --search-uri 's3://<YOUR_BUCKET>/titanic/titanic.parquet'
Now move back to the main branch and merge the import branch:
bauplan checkout main
bauplan branch merge <YOUR_USERNAME>.first_import
Create a branch
Create a new data branch in your name and switch to it:
bauplan checkout -b <YOUR_USERNAME>.first_workflow
Everything you do from here on happens on this branch: main stays untouched until you merge. A new branch starts with every table of the branch it was created from, so titanic is already there. Check its columns and types:
bauplan table get titanic
Run a pipeline
We are now going to create a brand new table out of the raw Titanic data. This new table will aggregate the original data so that we can gain some further insights. In particular, we will explore how the survival rate varied by year of age.
This new table will be created as the output of a data pipeline. In Bauplan, a data pipeline is represented by a project. To scaffold a new one from a template, run:
bauplan init --name first_workflow
This command creates three files:
bauplan_project.yaml, the project metadata
The project's id and name.
project:
id: 23f6f3e3-2364-4fc7-8d46-92e4d687d161
name: first_workflow
models.py, the data pipeline
This file contains the actual data pipeline, organized around three kinds of objects:
- The
survival_rate_by_agefunction, a model. It reads theAgeandSurvivedcolumns oftitanic, groups passengers by age, and computes the survival rate of each age. - The
test_agefunction, an expectation: a data quality check that runs on the model's output before anything is written. It checks that every age appears only once in the output. - The classes
SurvivalColumns,SurvivalRateSchema, andPassengerAgeGroup, three table schemas defining sets of columns to be used as inputs and outputs of the two functions above.
from typing import Annotated
import bauplan
import pyarrow
from bauplan.standard_expectations import expect_column_all_unique
class SurvivalColumns(bauplan.TableSchema):
"""A projection of 2 columns for survival rate analysis."""
Age: Annotated[
bauplan.Float64 | None,
bauplan.TableField(
doc="A passenger's age in years according to the solar calendar.",
),
]
Survived: Annotated[
bauplan.Int64 | None,
bauplan.TableField(
doc="Indicator of passenger's survival; may be 1 (survived) or 0 (did not).",
),
]
class SurvivalRateSchema(bauplan.TableSchema):
"""Analysis result of passenger survival rate by age (grouped by year)."""
Age: Annotated[
bauplan.Float64 | None,
bauplan.TableField(
doc="Passenger age group by year.",
lineage=SurvivalColumns["Age"],
),
]
survival_rate: Annotated[
bauplan.Float64 | None,
bauplan.TableField(
doc="Likelihood of passenger survival for a given age group (by year).",
lineage=SurvivalColumns["Survived"],
),
]
@bauplan.model()
@bauplan.python("3.12", pip={"polars": "1.37"})
def survival_rate_by_age(
passengers: Annotated[
pyarrow.Table,
bauplan.Model(
"titanic",
projection_schema=SurvivalColumns,
filter="Age IS NOT NULL",
),
],
) -> Annotated[pyarrow.Table, SurvivalRateSchema]:
"""
Bins passengers by age and returns survival rate per bin.
Returned table:
| Age | survival_rate |
|----------|---------------|
| 0 | 1.0 |
| ... | ... |
| 19 | 0.3 |
| 20 | 0.4 |
"""
import polars as pl
df = pl.DataFrame(passengers)
return (
df.group_by(pl.col("Age").floor())
.agg(pl.col("Survived").mean().alias("survival_rate"))
.sort("Age")
.to_arrow()
)
class PassengerAgeGroup(bauplan.TableSchema):
"""Projection schema for age group data."""
Age: Annotated[
bauplan.Float64 | None,
bauplan.TableField(lineage=SurvivalRateSchema["Age"]),
]
@bauplan.expectation()
@bauplan.python("3.12")
def test_age(
data: Annotated[
pyarrow.Table,
bauplan.Model("survival_rate_by_age", projection_schema=PassengerAgeGroup),
],
) -> bool:
"""Validates that the Age bins are unique"""
return expect_column_all_unique(data, "Age")
pyproject.toml, the Python dependencies
Bauplan itself, plus a linter, a type checker and polars for local development.
[project]
name = "first_workflow"
version = "0.1.0"
requires-python = "~=3.13.15"
dependencies = ["bauplan~=0.3.7"]
[dependency-groups]
dev = [
"ruff>=0.16.4",
"ty>=0.0.75",
"polars>=1",
]
This is enough to get started. You can run this pipeline with
bauplan run
Bauplan will run your code (remotely, on the cloud) and verify that the output table conforms with the expectations. However, you will soon realize that the table was not written anywhere. This is expected because we did not specify any materialization strategy for it. To write it to your branch as a table, set its materialization_strategy in models.py:
# Add a materialization strategy to write the table.
# "REPLACE" will overwrite any existing table with that name.
@bauplan.model(materialization_strategy="REPLACE")
@bauplan.python("3.12", pip={"polars": "1.37"})
def survival_rate_by_age(...)
You can now re-run it and you will see it appear on your branch!
See an expectation stop bad data
In the previous run, the expectation did not stop any bad data. This is because there was no bad data to stop to begin with! So, let's add a new expectation to see it at work.
The survival_rate_by_age model reads titanic with filter="Age IS NOT NULL", because 177 passengers have no recorded age. Add an expectation that checks for exactly that, at the end of models.py:
# The rest of models.py
...
@bauplan.expectation()
@bauplan.python("3.12")
def test_age_not_null(
data: Annotated[
pyarrow.Table,
bauplan.Model("survival_rate_by_age", projection_schema=PassengerAgeGroup),
],
) -> bool:
"""Validates that every row has an age."""
return bauplan.standard_expectations.expect_column_no_nulls(data, "Age")
Then, break the model on purpose by deleting the filter="Age IS NOT NULL", line from survival_rate_by_age, and run the pipeline again.
You will see that this time the run fails. Without the filter, the passengers with no age end up in a row whose Age is null, and test_age_not_null returns False. A failed expectation stops the run before anything is written, so the table on your branch is still the one from the previous run, with no null ages; you can check that this is the case with:
bauplan query "SELECT COUNT(*) FROM survival_rate_by_age WHERE Age IS NULL"
Restore the filter="Age IS NOT NULL", line in survival_rate_by_age and run the pipeline again. This time all expectations pass.
Merge
It's time to bring this table to production! The survival_rate_by_age table exists on your branch only. Switch to main and merge your branch into it:
bauplan checkout main
bauplan branch merge <YOUR_USERNAME>.first_workflow
Query the result
survival_rate_by_age is now on main. Query it with:
bauplan query "SELECT * FROM survival_rate_by_age"
You should see two columns, Age and survival_rate: the survival rate of the passengers of each age.
Congratulations! You've built a pipeline on a branch, checked its output, and published the result to main.