skip to content

A daily click-through model's TFDV step re-infers the schema from each day's data before validating; after a tracking change emptied one feature, why did nothing fire?

level: seniorimportance: should knowfreq 6%

answer

  1. who decides what correct looks like
  2. inferred from the broken day itself
  3. the schema is meant to be fairly static
  4. write_schema_text, load_schema_text
  5. presence constraints, not comparators

basics

~20 s

Re-inferring makes the schema describe the broken batch itself, so validation compares the data with a copy of its own shape. Freeze a reviewed schema under version control, load it each run, and change it only through review.

solid answer

~50 s

`tfdv.infer_schema` builds every expectation from the statistics it is given, so a schema inferred from today's batch already accepts today's batch: a feature that vanished is no longer expected, and one that arrived empty is at best given a presence requirement the broken data meets. The TFDV docs describe the schema as fairly static and the inferred one as a draft to review. So infer once from a trusted window, review it (set `presence.min_fraction` on features that must be filled, tighten domains), save it with `tfdv.write_schema_text`, and have the daily job load it with `tfdv.load_schema_text` and only validate. Against that frozen schema an emptied feature raises `FEATURE_TYPE_NOT_PRESENT` if it is present with no values, or `SCHEMA_MISSING_COLUMN` if it is gone from the statistics, and the step fails the run. A legitimate change then arrives as a reviewed schema edit, not as silent re-inference.

go deeper

for a junior

Recall that TFDV's schema is meant to stay fixed across datasets and that an inferred schema is a draft someone must review before it is trusted.

for a middle

Explain why inferring from the batch you are about to validate makes the check circular, and which presence fields and anomalies catch a feature that empties.

for a senior

Diagnose the silent pass, then show the fix end to end: a reviewed schema in version control, presence constraints on critical features, comparators for value shifts, a failing step.

for a principal

Treat the schema as an owned artefact: decide who approves changes, how a new or retired feature moves through review, and how many pipelines share one schema.

## What happened An ad click-through model retrains every day. Its TFDV step computes statistics over the day's training data, calls `tfdv.infer_schema` on those statistics, and validates the same statistics against the schema it just produced. A change in click tracking then left one feature, call it `ad_position`, without values in every example. Validation reported nothing, the model trained on a dead feature, and the problem surfaced only when quality fell. ## Why re-inference hides it - `tfdv.infer_schema` derives each expectation from the **statistics it is given**. Inferred from the broken day, the schema describes the broken day. - A feature absent from today's statistics is simply **not in today's schema**, so nothing expects it. - A feature present but empty is at best given a presence requirement the broken batch already meets. - The check becomes the data compared with a copy of its own shape, and that comparison has almost nothing to disagree with. The TFDV guide points the other way: the schema "is expected to be fairly static" (several datasets conform to one schema while their statistics vary) and an inferred schema is a starting point you **review and refine**. ## Freezing the schema 1. Compute statistics over a window of training data you trust and call `tfdv.infer_schema` once. 2. Review the result: set `presence.min_fraction` on features that must always be filled, tighten string domains, and use schema environments to mark the label as absent at serving. 3. Save it with `tfdv.write_schema_text(schema, output_path)` and keep the text file under version control beside the pipeline code. 4. In the daily job, load it with `tfdv.load_schema_text(input_path)` and call `tfdv.validate_statistics`, with no inference step. 5. Fail the step whenever the returned `Anomalies` proto has entries. ## What a frozen schema catches | What broke | Schema field that catches it | Anomaly | |---|---|---| | feature missing from the statistics entirely | `presence.min_count` or `presence.min_fraction` above 0 | `SCHEMA_MISSING_COLUMN` | | feature present with zero non-missing values | `presence.min_count` or `presence.min_fraction` above 0 | `FEATURE_TYPE_NOT_PRESENT` | | feature filled less often than required | `presence.min_fraction` | `FEATURE_TYPE_LOW_FRACTION_PRESENT` | | categorical values outside the reviewed domain | the feature's string domain | `ENUM_TYPE_UNEXPECTED_STRING_VALUES` | | far fewer rows than usual | `dataset_constraints.min_examples_count` | `DATASET_LOW_NUM_EXAMPLES` | The two presence anomalies in the first rows are skipped for a feature whose `lifecycle_stage` is `PLANNED`, `ALPHA`, `DEBUG` or `DEPRECATED`, which is how a feature is retired without a daily alert. ## Where the frozen schema lives - **One reviewed file per model input**, stored as text beside the pipeline code, so a schema change is a diff a reviewer reads. - **The same file everywhere it applies**: the TFDV guide notes that the schema used for validation can also set up TensorFlow Transform, so the trainer's transforms and its validation read one description of the data. - **A known-good origin**: record which window of data the first inference ran on, so a later reader knows what "correct" was measured against. ## Why a drift comparator is not the fix A `drift_comparator` compares today's value distribution with the previous statistics. It helps when a feature's values collapse: a categorical feature suddenly filled with one placeholder value moves its distribution and can cross a configured `infinity_norm` threshold. But the anomalies reference lists `COMPARATOR_TREATMENT_DATA_MISSING` (current data lacking the feature) as **not detected in TFDV**, so a feature that vanished from today's statistics is left to the schema's presence constraints. Use both: presence in the frozen schema for "is it there", a comparator for "has it moved". ## Changing the schema on purpose A frozen schema is not a schema that never changes; it changes only through review. - A new feature first appears as `SCHEMA_NEW_COLUMN`: it is in the statistics but not in the schema. That anomaly is the prompt to add it. - `tfdv.update_schema(schema, statistics)` returns the schema updated to conform to a trusted batch's statistics; its `infer_feature_shape` argument is marked deprecated in TFDV 1.21.0. - The text diff goes through the same review as code, and the next run loads the new version. Re-inference and freezing differ in one decision: **who is allowed to say what correct data looks like**. Left to each day's data, the answer is whatever arrived that day, including a tracking bug.

  • A new feature is added on purpose next week. How does it get into the frozen schema?
    It first shows up as `SCHEMA_NEW_COLUMN`, a feature in the statistics with no entry in the schema. Compute statistics over a trusted batch, apply `tfdv.update_schema(schema, statistics)` or edit the proto by hand, review the text diff like code, and commit it; the next run loads the new version.
  • Would a drift_comparator on the feature have caught the emptied feature instead?
    Only in part. A drift comparator compares value distributions with previous statistics, so values collapsing to one placeholder can cross its threshold. The anomalies reference lists the case of current data lacking the feature as not detected in TFDV, so a vanished feature needs a presence constraint in the frozen schema.
  • How do you retire a feature without an anomaly every day?
    Set the feature's `lifecycle_stage` to `DEPRECATED` in the schema. The detection conditions for `FEATURE_TYPE_NOT_PRESENT` and `SCHEMA_MISSING_COLUMN` exclude features in the `PLANNED`, `ALPHA`, `DEBUG` and `DEPRECATED` stages, so the feature can disappear quietly once the model stops using it.

A frozen schema is the answer key a teacher writes before the exam. If each student were allowed to write the key from their own paper, every paper would score full marks; the key catches mistakes only because it was fixed, and checked, before the papers came in.

  • Re-inferring the schema every run keeps TFDV validation up to date
  • A drift_comparator reports a feature that has vanished from today's data
  • A frozen schema can never accept a new feature
  • No anomalies means the training batch is safe, whatever schema it was checked against
  • Validating today's statistics against today's inferred schema catches emptied features