By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
There is a red build that means the product is broken and a red build that means the model phrased it differently today. If your team cannot tell them apart, every red build gets a shrug — and the real failure ships.
Flaky AI tests are not inevitable. They are usually one of five design mistakes, and each has a known fix.
| Cause | Symptom | Fix |
|---|---|---|
| Brittle assertion | Fails on harmless rewording | Assert properties, not strings |
| Single-run judgment | Different verdict each run for a borderline case | N-of-M voting or a tolerance band |
| Judge variance | Score wobbles ±0.05 with no product change | Pin the judge model; lower its temperature; average two judgments on borderline cases |
| External dependency | Timeouts, 429s, provider incidents | Retry with backoff; separate transport failures from quality failures |
| Test order coupling | Passes alone, fails in the suite | Isolate state; never share a conversation between cases |
For a borderline case, run it three times and require two passes. This is not cheating — it is measuring a distribution instead of a point. Document it, and only apply it where the variance is genuine.
def n_of_m_pass(run_once, runs=3, needed=2) -> bool:
"""Borderline cases: sample the distribution instead of trusting one draw."""
passes = sum(1 for _ in range(runs) if run_once())
return passes >= neededIf your faithfulness mean has historically been 0.87 ± 0.03, a threshold of exactly 0.85 will flip red on ordinary variance. Two better options:
When a test is genuinely flaky and you have not fixed it yet, move it to a quarantined suite that runs nightly and reports but does not block. Put an owner and a date on it. Deleting it loses the coverage; leaving it in the blocking suite destroys trust.
# pytest marker for a quarantined case
# @pytest.mark.quarantine(reason="judge variance", owner="priya", review="2026-12-15")
markers:
quarantine: known-flaky, reported nightly, does not block releaseKey takeaways
Flakiness comes from brittle assertions, single-run judgment, judge variance, external dependencies or shared state.
Assert properties; use N-of-M only where variance is measured and accepted.
Set thresholds below the noise band and add a separate regression rule.
Quarantine flaky tests with an owner and a review date — never delete them.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.