Sub topic 12
Testing Offline and Online Parity
This chapter assumes basic Python and pandas, a supervised machine-learning workflow, and a model that runs in a batch job or API. You should also understand missing values and simple aggregations.
Offline features are computed during training or batch scoring. Online features are computed when a prediction request arrives. Offline/online parity means both paths produce the same feature values from the same raw event. A model can return valid predictions while parity is broken, so this error often remains silent.
What a parity test checks
A parity test sends identical raw inputs through both implementations and compares the resulting feature values.
The test should cover ordinary records and boundary cases:
- missing numeric and categorical values
- unknown categories
- zero and negative values where valid
- timestamps near window boundaries
- large values and empty collections
- different input column orders
A useful test compares values with a small tolerance for floating-point calculations. It should compare feature names and types as well as values.
A simple parity test
The example below uses one shared training statistic, age_median. Both transformations apply the same rule. The online function returns one dictionary per request, while the offline function returns a DataFrame.
import pandas as pd
from pandas.testing import assert_frame_equal
age_median = 40.0
def offline_transform(events: pd.DataFrame) -> pd.DataFrame:
result = pd.DataFrame(index=events.index)
result["age"] = events["age"].fillna(age_median).astype(float)
result["is_us"] = events["country"].fillna("unknown").eq("US").astype(int)
return result[["age", "is_us"]]
def online_transform(event: dict) -> dict:
age = event["age"]
country = event.get("country") or "unknown"
return {
"age": float(age if age is not None else age_median),
"is_us": int(country == "US"),
}
def test_offline_online_parity():
raw_events = [
{"age": 25, "country": "US"},
{"age": None, "country": "CA"},
{"age": 70, "country": None},
]
offline = offline_transform(pd.DataFrame(raw_events))
online = pd.DataFrame([online_transform(event) for event in raw_events])
assert_frame_equal(
offline.reset_index(drop=True),
online[offline.columns].reset_index(drop=True),
check_exact=False,
rtol=1e-6,
atol=1e-8,
)
test_offline_online_parity()
print("parity check passed")The test uses the same fixed median in both paths. In a real system, that value should come from fit-time state saved with the model or feature definition. Do not calculate a new median from each request.
Common parity failures
Code can differ even when the feature name is identical:
- One path uses a 30-day window and the other uses 24 hours.
- One path treats a missing value as zero and the other treats it as unknown.
- One path uses local time and the other uses UTC.
- One path rounds before aggregation and the other rounds afterward.
- One path has a newer category vocabulary.
- One path includes the current event and the other excludes it.
For time-dependent features, include event timestamps in test inputs and assert the expected boundary behavior. For aggregations, compare a small hand-calculated example before relying on a larger randomized test.
Exercise
Exercise
Why should a parity test use the same raw events for both transformations instead of comparing unrelated training and serving samples?
Show answer
Using the same raw events isolates transformation differences. Different samples can have different feature values even when both transformations are correct.