What you will be able to do
- Write a Lakeflow pipeline dataset that scores newly arriving rows with an MLflow model using a model URI, spark_udf and withColumn
- Choose between a streaming table and a materialized view as the target for predictions
- Predict which rows a changed model or query affects, and when a full refresh is needed
- Choose triggered or continuous execution for a scoring pipeline, and generate a starter inference notebook from the model registry
Key concept
Streaming table as the prediction target — A streaming table reads each new row from an append-only source once, scores it, and appends the result. Rows that were already scored stay as they are, so each pipeline update only processes what arrived since the last checkpoint.
1.Where predictions live in a Lakeflow pipeline
Streaming inference on Databricks is done with a declarative pipeline. Each dataset definition applies the model to rows as they arrive and writes the predictions into a table that the pipeline manages. Delta Live Tables is the old name for this product, which is now Lakeflow pipelines (Lakeflow Spark Declarative Pipelines). Existing DLT code keeps working without migration. In Python, import dlt becomes from pyspark import pipelines as dp, and @dlt decorators become @dp.
A pipeline is a set of source files that declare datasets, plus a configuration that says how the pipeline runs. You don't write the execution order. The pipeline works out the dependencies between datasets, builds a DAG, and computes upstream datasets before downstream ones. For inference, that lets you declare a raw ingestion table, a scoring table that reads from it, and any aggregates that read from the scores. The pipeline then runs them in the right order.
The decorator decides what kind of dataset receives the predictions. @dp.table creates a streaming table: a Delta table that processes data incrementally and appends new rows on each update. @dp.materialized_view creates a materialized view, which stays correct by recomputing, for example when a joined dimension changes. That difference determines which one you use for scoring.
| Property | Streaming table (@dp.table) | Materialized view (@dp.materialized_view) |
|---|---|---|
| How input rows are handled | Each input row is processed once and appended | Recomputed so the result is always correct |
| Joins when a dimension changes | Do not recompute (fast but possibly stale) | Automatically recomputed |
| Best fit for scoring | New events that must be scored as they land, without rescoring old ones | Results that must reflect the current state of every input |
| Source requirement | Append-only, bounded naturally or with a watermark | Any table the query reads |
For a stream of orders or transactions that need a score when they arrive, use a streaming table. A materialized view can fit when every prediction must reflect the latest joined dimension values. It does not meet the requirement 'score new rows, never rescore old ones', because incremental, once-only processing is what defines a streaming table. Batch and streaming scoring both give high throughput at low cost, with latencies down to minutes. The trade-off against real-time endpoints is covered in Batch vs streaming vs real-time inference.
Checkpoint 1 of 8· Check yourself
A team scores card transactions with an MLflow model in a Lakeflow pipeline. The source table is append-only. Each new transaction must be scored when it arrives, and transactions that were already scored must not be rescored on later updates. Which dataset definition should hold the predictions?
@dp.table defines a streaming table, which reads each new row from an append-only source once and appends it, so old rows are never rescored. A materialized view recomputes to stay correct, which means it rescores rows it has already scored. Renaming @dlt to @dp changes nothing about how the dataset behaves, and the pipeline works out dependencies between datasets of either type.
“Streaming tables are designed for append-only data sources and process inputs only once.”Source: docs.databricks.com
2.The three-step scoring pattern inside a dataset definition
Pipelines treat an MLflow model as a transformation: it takes a Spark DataFrame and returns a Spark DataFrame. A pipeline dataset is also defined as a function that returns a DataFrame, so a scoring script needs only a few changes to become a pipeline. The spark_udf mechanics are taught in Batch inference with mlflow.pyfunc and spark_udf. Inside a pipeline they take three steps: build a model URI, turn it into a Spark UDF with mlflow.pyfunc.spark_udf, then call that UDF in withColumn inside the dataset function.
%pip install mlflow==2.20.2
from pyspark import pipelines as dp
import mlflow
run_id= "<mlflow-run-id>"
model_name = "<the-model-name-in-run>"
model_uri = f"runs:/{run_id}/{model_name}"
loaded_model_udf = mlflow.pyfunc.spark_udf(spark, model_uri=model_uri)
@dp.materialized_view
def model_predictions():
return (
spark.read.table(<input-data>)
.withColumn("prediction", loaded_model_udf(<model-features>))
)This sample is a batch materialized view. Two changes make it streaming. First, switch the decorator to @dp.table. Second, read the source with spark.readStream.table(...) instead of spark.read.table(...). The UDF stays where it was. The pipeline join example below shows the streaming read form in a @dp.table function.
@dp.table
def customer_sales():
return spark.readStream.table("sales").join(spark.read.table("customers"), ["customer_id"], "left")spark_udf returns a column function, not a model object. You call it on the feature columns, packed into a single column with struct(...) from pyspark.sql.functions; in the snippet below, features is a Python list of column names. The result goes in as a new column with .withColumn("prediction", ...). It has no .transform(df) method like an MLlib Transformer. Calling .toPandas() on a streaming DataFrame and returning a pandas object also breaks the contract, because a dataset function must return a Spark DataFrame. Passing the streaming DataFrame to pyfunc.load_model(...).predict() fails for the same reason: the model is applied through the UDF, row by row, across the cluster.
def loan_risk_predictions():
return (
spark.read.table("loan_risk_input_data")
.withColumn('predictions', loaded_model_udf(struct(features)))
)The URI decides which model version the pipeline scores with. The sample uses a runs:/ URI, which pins one training run. For a registered model, point the URI at an alias, for example models:/catalog.schema.model@Champion. When someone moves the alias to a new version, the pipeline scores with that version without any code change. Alias management is taught in Model aliases (Champion / Challenger).
Checkpoint 2 of 8· Check yourself
Inside a @dp.table function that reads orders with spark.readStream.table("orders"), you have loaded_model_udf = mlflow.pyfunc.spark_udf(spark, model_uri=model_uri). How should the function apply the model to produce predictions?
spark_udf returns a column function, so the model is applied as a Spark transformation: call the UDF on the feature columns inside withColumn, and the dataset function returns a Spark DataFrame. It has no .transform() method. Returning a pandas object breaks the dataset contract. A watermark limits stateful processing and is not required for row-by-row scoring.
“creates a data transform that uses the input table as a source and integrates the MLflow PySpark inference UDF to perform model predictions”Source: docs.databricks.com
Checkpoint 3 of 8· Exam question
A machine learning engineer is building a Lakeflow Spark Declarative Pipeline (the rebranded Delta Live Tables) that must score newly arriving rows in the bronze_orders table with a registered MLflow model as soon as they land, without recomputing predictions for orders that were already scored on a previous run. Which approach correctly implements this streaming inference pattern?
Correct answer: A — Decorate the scoring function with @dp.table, read the source using spark.readStream.table("bronze_orders"), load the model with mlflow.pyfunc.spark_udf, and append predictions with withColumn.
- A. This is correct:
@dp.tablebacked by a streaming read processes only the new micro-batch of rows on each run, and loading the model once withmlflow.pyfunc.spark_udflets it be applied as an ordinary column transformation on that incremental data. - B. A materialized view is recomputed from its complete upstream input each time the pipeline runs, so every order would be rescored on every run instead of only the new ones, defeating the incremental goal.
- C. Calling
.toPandas()forces a full collect of the streaming data to the driver, which is unsupported inside a dataset definition and breaks the pipeline's distributed, incremental execution model. - D. This is unnecessary and inaccurate: dataset functions can call
mlflow.pyfunc.spark_udfdirectly, so routing scoring through an externalforeachBatchsink outside the pipeline is not required to achieve incremental inference.
Checkpoint 4 of 8· Exam question
A model is registered in Unity Catalog and referenced by the alias champion. Inside a Lakeflow pipeline dataset function that reads a streaming source, which snippet correctly loads and applies that model to produce a prediction column?
Correct answer: B — Call mlflow.pyfunc.spark_udf(spark, model_uri="models:/catalog.schema.model@champion"), then apply the UDF via .withColumn("prediction", loaded_model(struct(*feature_cols))) on each row.
- A.
load_modelreturns a plain pyfunc object whose.predict()expects a local pandas or numpy input, not a distributed Spark DataFrame, so it cannot be called this way on streaming data inside a table definition. - B. This is correct:
mlflow.pyfunc.spark_udfloads the aliased model as a Spark UDF, and wrapping the feature columns instructand applyingwithColumnscores each row of the incremental DataFrame. - C.
spark_udfreturns a callable Python UDF, not an MLlibTransformer, so it has no.transform()method; it must be applied throughwithColumnorselectlike any other UDF. - D. This is unnecessary:
mlflow.pyfunc.spark_udfcan be called directly inside a dataset function, so hand-rewriting the model's logic as a separatepandas_udfdiscards the registered model definition for no reason.
3.What gets scored, and what never gets rescored
A streaming table scores each input row once. Its default flow is an append flow that adds new rows on each update, and only new input data is read. Progress is tracked with checkpoint management. When a pipeline restarts mid-run, it resumes from the checkpoint instead of rescanning the source, so transactions that were already processed are not scored a second time.
Once-only processing also means changes are not applied to old rows. The docs illustrate this with a query edit: change SELECT LOWER(name) to SELECT UPPER(name), and existing rows stay lowercase while new rows are uppercase. A new model version behaves the same way. After a promotion, one streaming table can hold rows scored by the old version and rows scored by the new one. To rescore everything with the current model, run a full refresh, which re-reads all previous data from the source and updates every row.
The model behind the URI probably changed around 1 March, for example after an alias was reassigned. Rows appended before the change kept their old scores. A full refresh of the streaming table rescores all historical rows with the current model.
Watermarks are about state, not about whether the model can run. Streaming tables expect streams that are bounded, either naturally (a finite set of files) or by a watermark. A watermark says how long to wait for late events before treating a time window as complete. A plain row-by-row scoring query needs no watermark for the UDF to run, and out-of-order rows are not dropped from scoring. Watermarks matter when the query keeps unbounded state, such as windowed aggregations. Without one, that kind of stream can fail the pipeline from memory pressure.
Joins are the other place where streaming scores can go stale. If the scoring query joins the stream to a dimension table, such as customer attributes used as features, the join is not recomputed when the dimension changes. That is fine when speed matters more than perfect accuracy. If every prediction must reflect current dimension values, a materialized view is the correct target.
Checkpoint 5 of 8· Match them up
Match each situation in a streaming scoring pipeline to what handles it.
Tap a term, then the definition that fits it.
A streaming table scores each row only once, so rescoring history requires a full refresh. Watermarks limit state for aggregations; they are not needed to run the model. Checkpoints stop rows from being scored twice after a restart. Joins in a streaming table are not recomputed, so predictions that must always reflect current dimension values belong in a materialized view.
“To reprocess existing rows that were processed prior to the change, a full refresh is required.”Source: docs.databricks.com
Checkpoint 6 of 8· Exam question
A fraud-detection team streams scored transactions through a Lakeflow pipeline and is concerned about duplicate predictions being written if the pipeline restarts mid-run. Assuming transactions_bronze is an append-only source, what should the team expect from the streaming table that applies the model?
Correct answer: A — Each transaction is processed and scored exactly once, and a restart resumes from the pipeline's checkpointed progress rather than rescoring transactions that were already processed.
- A. This is correct: a streaming table over an append-only source processes each record exactly once, and restarts resume from tracked progress instead of reprocessing rows that were already scored.
- B. Reprocessing the full history on every run describes a materialized view's batch recomputation, not a streaming table, which only handles new or changed source data incrementally.
- C. Watermarking is a separate mechanism used for time-based windowed aggregations; it is not a prerequisite for a model UDF to score rows arriving on a streaming table.
- D. There is no dedicated streaming model registration type in the Unity Catalog Model Registry; a normally registered model can be loaded with
mlflow.pyfunc.spark_udffor both batch and streaming use.
4.Triggered or continuous: when the predictions refresh
How often predictions refresh depends on the pipeline mode, not the table type. Both streaming tables and materialized views can run in either mode. In triggered mode, the pipeline processes the data available when the update started and then stops. In continuous mode, it keeps processing new data as it arrives.
| Question | Triggered | Continuous |
|---|---|---|
| When does the update stop? | Automatically when complete | Runs until manually stopped |
| What data is processed? | Data available when the update starts | All data as it arrives at configured sources |
| Freshness it suits | Every 10 minutes, hourly, or daily | Between every 10 seconds and a few minutes |
| Cost profile | Cluster runs only long enough to update | Always-running cluster, more expensive, lower latency |
Pick the mode based on how stale a prediction can be. A churn score that feeds a daily retention email can run as an hourly triggered update and pay for compute only during that run. A fraud flag that has to appear within a minute of the card swipe needs continuous execution. In continuous mode, the pipeline watches the Delta tables it depends on and updates only when their contents change.
Checkpoint 7 of 8· Check yourself
Fraud flags from a streaming scoring table currently come from an hourly scheduled job that runs the pipeline in triggered mode. The business now needs each flag within a minute of the card swipe. What change meets this requirement the way Databricks recommends?
Refresh frequency depends on the pipeline mode, not the table type, and Databricks recommends a continuous job for continuous runs. The job's execution mode overrides the pipeline setting, so an hourly scheduled job would still run single updates even with Pipeline mode set to continuous. A watermark limits state and does not change how often the pipeline runs.
“Databricks recommends running continuous pipelines with a continuous job rather than setting the value of Pipeline mode to continuous.”Source: docs.databricks.com
Sources8
5.Generating the inference notebook from the model registry
You can generate a starting scoring notebook from the model registry instead of writing it by hand. Use the model's inference dialog in the workspace model registry and choose the Streaming (Lakeflow pipelines) tab. Then:
- Select the model version to score with.
- Browse to the input table. In Unity Catalog workspaces you pick it as
catalog.schema.table. - Name the output Lakeflow dataset that will store the predictions.
The generated notebook uses the input table as its source and applies the model through the MLflow PySpark inference UDF, the same pattern as the previous section. By default it writes to a live table, the older DLT name for a dataset that is not streaming: like a materialized view, it is recomputed from its input on every update rather than appended to. The streaming alternative, which the docs call a streaming live table, is what this lesson calls a streaming table. Common edits are turning the output into a streaming table, adding schema information, or adding data quality constraints. You can create a new pipeline from the notebook or add it to an existing pipeline as another source library.
The version picker in this legacy dialog can follow the registry. If you choose the current Production or Staging entry, the notebook uses whichever version holds that stage when it runs, so you don't edit the notebook after each new version. In Unity Catalog, an alias-based URI gives the same 'follow the promoted model' behaviour.
Checkpoint 8 of 8· Check yourself
You generate a scoring notebook from the workspace model registry's Streaming (Lakeflow pipelines) tab and choose the current Production entry in the Model version picker. You then edit the output into a streaming table and run the pipeline. A month later, a newer model version is moved to Production. What happens on the next pipeline updates?
Choosing the Production entry makes the notebook use whichever version holds that stage when it runs, so no edit is needed. A streaming table does not re-query rows it has already appended, so only new rows get the new version's scores. Rescoring the history requires a full refresh.
“When you select one of these options, the notebook automatically uses the Production or Staging version as of the time it is run.”Source: docs.databricks.com
Sources
(9)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“The product formerly known as Delta Live Tables (DLT) has been updated to Lakeflow pipelines.”
↩︎ Where predictions live in a Lakeflow pipeline“The @table decorator is now used to create streaming tables, and the new @materialized_view decorator is used to create materialized views.”
↩︎ Where predictions live in a Lakeflow pipeline - 2.
“Pipelines automatically infer dependencies between datasets and arrange them in a directed acyclic graph (DAG).”
↩︎ Where predictions live in a Lakeflow pipeline - 3.
“A streaming table is a Delta table with additional support for streaming or incremental data processing.”
↩︎ Where predictions live in a Lakeflow pipeline“Materialized views are always correct because they automatically recompute joins when dimensions change.”
↩︎ Where predictions live in a Lakeflow pipeline“A row that has already been appended to a streaming table will not be re-queried with later updates to the pipeline.”
↩︎ What gets scored, and what never gets rescored“Streaming tables use checkpoint management, which makes them well-suited for low-latency streaming.”
↩︎ What gets scored, and what never gets rescored“You can trigger a full refresh to requery all previous data from the source table to update all rows in the streaming table.”
↩︎ What gets scored, and what never gets rescored“An unbounded stream that does not have a watermark can cause a pipeline to fail due to memory pressure.”
↩︎ What gets scored, and what never gets rescored“Joins in streaming tables do not recompute when dimensions change.”
↩︎ What gets scored, and what never gets rescored“Streaming tables are designed for append-only data sources and process inputs only once.”
↩︎ Key concept“Streaming tables are designed for append-only data sources and process inputs only once.”
↩︎ Exam trap 1“You can trigger a full refresh to requery all previous data from the source table to update all rows in the streaming table.”
↩︎ Exam trap 3“An unbounded stream that does not have a watermark can cause a pipeline to fail due to memory pressure.”
↩︎ Exam trap 4“Streaming tables are designed for append-only data sources and process inputs only once.”
↩︎ Checkpoint“To reprocess existing rows that were processed prior to the change, a full refresh is required.”
↩︎ Checkpoint - 4.
“Batch and streaming scoring supports high-throughput, low-cost scoring at latencies as low as minutes.”
↩︎ Where predictions live in a Lakeflow pipeline - 5.https://docs.databricks.com/aws/en/ldp/transformOfficial docs
“MLflow models are treated as transformations in Databricks, meaning they act upon a Spark DataFrame input and return results as a Spark DataFrame.”
↩︎ The three-step scoring pattern inside a dataset definition“Use the URI to define a Spark UDF to load the MLflow model.”
↩︎ The three-step scoring pattern inside a dataset definition“Call the UDF in your table definitions to use the MLflow model.”
↩︎ The three-step scoring pattern inside a dataset definition“The data columns used to make the prediction are passed as an argument to the UDF.”
↩︎ The three-step scoring pattern inside a dataset definition“To use MLflow models in a Unity Catalog-enabled pipeline, your pipeline must be configured to use the preview channel.”
↩︎ The three-step scoring pattern inside a dataset definition“Pipelines do not install MLflow by default”
↩︎ The three-step scoring pattern inside a dataset definition“Only new input data is read with each update.”
↩︎ What gets scored, and what never gets rescored“By default, pipelines recompute table results based on input data each time they are updated”
↩︎ Generating the inference notebook from the model registry“MLflow models are treated as transformations in Databricks, meaning they act upon a Spark DataFrame input and return results as a Spark DataFrame.”
↩︎ Exam trap 2 - 6.
“You can then update the production model by reassigning the “Champion” alias to a different model version.”
↩︎ The three-step scoring pattern inside a dataset definition - 7.
“the default flow is an append flow that adds new rows with each update”
↩︎ What gets scored, and what never gets rescored - 8.
“Pipeline mode is independent of the type of table being computed.”
↩︎ Triggered or continuous: when the predictions refresh“If the pipeline uses triggered mode, the system stops after refreshing all tables based on the data available when the update started.”
↩︎ Triggered or continuous: when the predictions refresh“Continuous pipelines require an always-running cluster, which is more expensive but reduces processing latency.”
↩︎ Triggered or continuous: when the predictions refresh“Databricks recommends running continuous pipelines with a continuous job rather than setting the value of Pipeline mode to continuous.”
↩︎ Triggered or continuous: when the predictions refresh“Standalone materialized views and streaming tables always run in triggered mode, regardless of job orchestration.”
↩︎ Triggered or continuous: when the predictions refresh - 9.https://docs.databricks.com/aws/en/machine-learning/manage-model-lifecycle/workspace-model-registryOfficial docs
“integrates the MLflow PySpark inference UDF to perform model predictions”
↩︎ Generating the inference notebook from the model registry“define a streaming live table as output, add schema information or data quality constraints”
↩︎ Generating the inference notebook from the model registry“You can then either create a new pipeline with this notebook or add it to an existing pipeline as an additional notebook library.”
↩︎ Generating the inference notebook from the model registry“When you select one of these options, the notebook automatically uses the Production or Staging version as of the time it is run.”
↩︎ Generating the inference notebook from the model registry“creates a data transform that uses the input table as a source and integrates the MLflow PySpark inference UDF to perform model predictions”
↩︎ Checkpoint