AI functions and expectations
Databricks Data Engineer Professional · Lesson 11 of 21 · 1:26:31
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Data Engineer Professional exam. In this lesson: AI functions for free text, expectations in Lakeflow pipelines, and quarantining bad rows. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Free text and bad rows
Quayside Eats receives thousands of support tickets in free text every day, and someone sorts them by hand. Meanwhile, orders with an empty amount or a negative fee slip into the silver layer, and nobody notices until a report is wrong. This lesson covers AI functions for batch enrichment, pipeline expectations, and quarantining bad rows.
AI functions
AI functions bring models into SQL, so a query or a pipeline can classify, extract, or enrich thousands of rows in batch. ai_query calls a model serving endpoint, like a foundation model, with a prompt built from each row, and returnType or responseFormat set the shape of the answer. Task functions need no endpoint: ai_classify picks one label from a list, ai_extract pulls named fields out of text, and ai_mask hides entities like names or phone numbers. For large batches, set failOnError to false, so that a row that fails keeps an error message, instead of stopping the whole query. AI functions are made for batch inference over tables, not for a single interactive request that needs low latency.
Pipeline expectations
Expectations declare data quality rules on a dataset of a Lakeflow pipeline, each with a name and a SQL condition. The default action, expect, keeps invalid rows in the target, and counts them in the metrics. expect_or_drop, or on violation drop row in SQL, removes invalid rows before they are written, and counts them too. expect_or_fail, or on violation fail update, makes the update fail as soon as an invalid row appears, so no metrics are recorded for it. expect_all and its drop and fail versions apply a whole dictionary of rules at once, so rules can be kept in one place and reused. And the pass and fail counts of each expectation appear in the pipeline UI and in the event log.
Quarantining bad rows
Dropping rows loses them, so when bad rows must be reviewed, a quarantine pattern keeps them aside instead. Combine the rules into one condition, and mark each row with an is_quarantined flag, which is true when the row breaks any rule. Then one table receives the valid rows, and another table, or a partition, receives the quarantined rows, for review and repair. The valid rows flow on to the gold layer without waiting, and fixed rows can be loaded again later.
Recap
Let's recap. ai_query runs a model over every row in batch, and failOnError set to false keeps a long job going. ai_classify, ai_extract, and ai_mask cover common tasks without choosing an endpoint. expect keeps and counts invalid rows, expect_or_drop removes them, and expect_or_fail stops the update. expect_all applies a dictionary of rules, and the counts appear in the UI and in the event log. And a quarantine flag keeps bad rows aside for review, while valid rows flow on.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Every night, thousands of new support tickets must each get one of five categories, in a SQL job, with the least code. What should the data engineer use?
- ai_mask on the ticket text
- ai_classify with the five category labels
- ai_extract with one field per category
- A Python loop that calls the model per ticket
Show the answer
ai_classify with the five category labels
Ai_classify picks one label from the list for each ticket, directly in SQL. “ai_mask on the ticket text” would hide entities in the text, and would not sort the tickets. “ai_extract with one field per category” would pull named fields out of the text, not choose one category. And “A Python loop that calls the model per ticket” would call the model one ticket at a time from Python, with much more code.
The silver orders table must never contain an order with an empty amount, but the pipeline must keep running when such orders arrive. Which action fits?
- expect_or_drop
- expect
- expect_or_fail
- A CHECK constraint on the table
Show the answer
expect_or_drop
Expect_or_drop removes the invalid orders before they are written, counts them, and lets the update continue. “expect” would keep the invalid orders in the table, and only count them. “expect_or_fail” would make the whole update fail when the first invalid order arrives. And “A CHECK constraint on the table” would also reject the whole write, and stop the pipeline.
The finance team must review every order that breaks a quality rule, while all the other orders must still reach the gold layer. What should the data engineer build?
- expect_or_drop on the silver table
- expect_or_fail on the silver table
- A quarantine flag and a table of flagged orders
- expect only, then a filter in each dashboard
Show the answer
A quarantine flag and a table of flagged orders
A quarantine flag sends the invalid orders to a table that finance can review, while the valid ones flow on. “expect_or_drop on the silver table” would remove the invalid orders, so finance could never review them. “expect_or_fail on the silver table” would stop every update, so no order would reach the gold layer. And “expect only, then a filter in each dashboard” would let the invalid orders reach the gold layer, and leave each dashboard to filter them.