Pipelines for AI
Three pipelines that feed models from the lakehouse: documents for retrieval, features for scoring, and a versioned training set tied to Iceberg snapshots.
A model is a consumer of the lakehouse, the same way a report is. It needs a path that can be run again, checked, and traced to the rows it learned from. The pipeline overview names three of those paths. This lab builds each one on the tables and schedules the rest of the series already uses.
Retrieval over your own documents is the RAG lab. This page stops where that one starts: clean text, in chunks, in the bucket. Vectors and answers live downstream.
Prerequisites
- The lakehouse bucket, the
lakeIceberg catalog, and the Spark image from the Spark lab silver.accountsandsilver.transactions(account_no,amount,booked_at), written the way the lakehouse lab lays out silver- The quality checks passing for the day you build from
- A
doc-chunksimage with Docling and Python, in thedata-pipelineimage stream oc, and thelakehousesecret for S3 credentials
oc get sparkapplications -n data-pipeline
oc get cm lakehouse -n data-pipeline -o jsonpath='{.data.BUCKET_NAME}{"\n"}'
Three pipelines, one lakehouse
| Pipeline | Runs | Reads | Writes |
|---|---|---|---|
| Document | When a batch of files lands | PDFs and scans in landing/docs/ |
Chunk text in bronze/doc_chunks/ |
| Feature | Every day, after gold | silver.transactions |
gold.account_features, one as_of date |
| Training data | When someone cuts a version | Feature and label snapshots | A folder plus a manifest of snapshot ids |
The document and training jobs are workflows you submit. The feature job is on the clock, in the same Airflow DAG as the accounts merge, after quality has passed. A data scientist can try a chunk size in an OpenShift AI workbench. The run that fills the bucket is the image in Git.
Document pipeline
Files arrive the way the ingestion lab lands any other file: landing/docs/incoming/, and landing/docs/accepted/dt=YYYY-MM-DD/ after a good parse. Docling turns each PDF into Markdown. A second step splits on headings and writes Parquet. Chunk size and overlap are discussed in the RAG lab; start with a heading, the paragraphs under it, and the source file name on every row. A chunk without a source cannot be cited.
/opt/jobs/doc_chunks.sh in the image:
#!/bin/bash
set -euo pipefail
DT="${DT:?set DT to YYYY-MM-DD}"
mkdir -p /tmp/md /tmp/out
aws s3 sync "s3://${BUCKET}/landing/docs/accepted/dt=${DT}/" /tmp/in \
--endpoint-url "${S3_ENDPOINT}"
find /tmp/in -name '*.pdf' -print0 \
| xargs -0 -r -n1 docling --output /tmp/md
python /opt/jobs/chunk_md.py /tmp/md /tmp/out/chunks.parquet
aws s3 cp /tmp/out/chunks.parquet \
"s3://${BUCKET}/bronze/doc_chunks/dt=${DT}/chunks.parquet" \
--endpoint-url "${S3_ENDPOINT}"
chunk_md.py reads each Markdown file as UTF-8, splits on lines that start with #, and writes doc_id, heading, and text. It does not transliterate.
import pathlib, sys
import pyarrow as pa
import pyarrow.parquet as pq
src, dest = sys.argv[1], sys.argv[2]
rows = []
for path in sorted(pathlib.Path(src).glob("*.md")):
heading, parts, chunks = "", [], []
for line in path.read_text(encoding="utf-8").splitlines() + ["#"]:
if line.startswith("#"):
text = "\n".join(parts).strip()
if text:
chunks.append({"doc_id": path.stem, "heading": heading, "text": text})
heading = line.lstrip("#").strip()
parts = [line] if line != "#" else []
else:
parts.append(line)
rows.extend(chunks)
pq.write_table(pa.Table.from_pylist(rows), dest)
A heading such as পাসওয়ার্ড রিসেট has to be in the Parquet unchanged. On a batch that contains one, check before you embed:
python -c "import pyarrow.parquet as pq; t=pq.read_table('/tmp/out/chunks.parquet'); assert any('পাসওয়ার্ড' in x for x in t.column('text').to_pylist())"
Scanned pages need OCR. The RAG lab uses NeMo Retriever extraction NIMs or Docling for that, and Bangla OCR has to be tried on a real page, not assumed. Keep the accepted PDF. The chunk file can be rebuilt from it. Embeddings are a later step: the vector database lab is where those chunks are inserted, and the RAG lab is where they are searched.
Submit the work as a Job. DT is the logical date, as everywhere else in this series.
oc apply -f doc-chunks-job.yaml
oc wait -n data-pipeline job/doc-chunks --for=condition=complete --timeout=1800s
oc logs -n data-pipeline job/doc-chunks
The Job's container uses the doc-chunks image, envFrom the lakehouse secret, and DT in the environment. backoffLimit: 1 retries a pulled pod. A Docling error is in the log and should not be retried until the PDF is fixed. Move the object to accepted/ only after the log is clean, so a second run does not mix a failed parse with a good one.
Feature pipeline
Scoring and training have to see the same numbers. Compute them once, in silver's grain, and write a feature table partitioned by the day the features describe.
CREATE TABLE IF NOT EXISTS lake.gold.account_features (
account_no string,
as_of date,
txn_count_30d bigint,
spend_30d decimal(18, 2)
) USING iceberg
PARTITIONED BY (as_of);
The window is the thirty days ending on the logical date, and it does not include later bookings. DT is 2026-10-29 when the DAG run is that day. The session time zone is Asia/Dhaka, as in the lakehouse lab, so those bounds are civil dates in Dhaka. Use the scheduler's date. A script that calls current_date() answers a different question on the day someone reruns it.
INSERT OVERWRITE lake.gold.account_features
PARTITION (as_of = DATE '2026-10-29')
SELECT
account_no,
count(*) AS txn_count_30d,
cast(sum(amount) AS decimal(18, 2)) AS spend_30d
FROM lake.silver.transactions
WHERE booked_at >= DATE '2026-09-30'
AND booked_at < DATE '2026-10-30'
GROUP BY account_no;
The partition clause replaces that date's rows and leaves every other as_of in place. Running Tuesday twice leaves one Tuesday partition. Copy accounts-silver.yaml from the Spark lab, point mainApplicationFile at this SQL job, and submit it:
oc apply -f account-features.yaml
oc get pods -n data-pipeline -l sparkoperator.k8s.io/app-name=account-features
oc logs -n data-pipeline \
-l sparkoperator.k8s.io/app-name=account-features,spark-role=driver --tail=40
Hang the task on accounts_daily after quality_accounts. Features for a day whose silver check failed are how a null account_no becomes a null feature and then a quiet model. Add one expectation on the new partition before the DAG is allowed to mark the day done: txn_count_30d not null, spend_30d not null, and the sum of spend_30d equal to the sum of amount on silver.transactions for the same window. That last comparison is the reconciliation. Publish the partition only when it matches.
A row for training dated 1 October uses as_of = 2026-10-01, which was computed from transactions booked before 2 October. It does not use the latest partition. Using the latest partition for an old label is how the model sees the future.
Training-data pipeline
A training set is a folder you can point at later, plus the Iceberg snapshots it was cut from. Labels live in gold.account_labels (account_no, as_of, label), filled by whatever review process owns the label. The extract joins each label to the feature row of the same as_of.
SELECT f.account_no, f.as_of, f.txn_count_30d, f.spend_30d, l.label
FROM lake.gold.account_features AS f
JOIN lake.gold.account_labels AS l
ON f.account_no = l.account_no AND f.as_of = l.as_of
WHERE f.as_of = DATE '2026-10-29';
Write the result to s3://lakehouse/training/account-risk/v2026-10-29/train.parquet. Then write a manifest next to it. The snapshot ids come from the tables themselves:
SELECT snapshot_id
FROM lake.gold.account_features.snapshots
ORDER BY committed_at DESC
LIMIT 1;
{
"name": "account-risk",
"as_of": "2026-10-29",
"features_snapshot": "<snapshot_id from account_features>",
"labels_snapshot": "<snapshot_id from account_labels>",
"quality_dag_run": "accounts_daily on 2026-10-29"
}
The row count in the manifest is df.count() from the job, written into the file by the job. Do not type a number by hand. Copy the manifest up beside the Parquet:
aws s3 cp manifest.json \
"s3://${BUCKET}/training/account-risk/v2026-10-29/manifest.json" \
--endpoint-url "${S3_ENDPOINT}"
If the day's quality task failed, do not cut the version. A folder under training/ means the checks passed and the snapshot ids in the manifest still resolve. Time travel in the lakehouse lab is how you read those snapshots again when a later merge has moved the current table. The catalog page for gold.account_features should link the DAG, so a model's inputs are one click from the job that built them.
Document chunks for a fine-tune follow the same rule. The Parquet under bronze/doc_chunks/dt=... is the input, the manifest records that prefix, and the RAG index is a serving copy. Rebuilding the index is allowed. Editing a chunk in the vector database and calling that the training set is not.
What's next
- RAG: embed the chunks, search them, and answer with citations.
- Vector databases: Milvus and pgvector, where those embeddings are stored.
- Quality and governance: the checks that must pass before a
vYYYY-MM-DDfolder exists. - Data lakehouse: snapshots, partitions and the bronze copy you can rebuild from.
- Orchestration: put
account_featuresonaccounts_daily. Leave the document Job and the training cut as runs you submit.
The pipeline overview places these three flows next to the reports. They read the same gold tables.