How to test a feature whose output is not deterministic

Features that call a language model, a recommender, or any system with inherent variability cannot be tested the way a deterministic feature can. The same input produces different outputs on different runs. A test that compares the output to a stored reference string fails most of the time for the wrong reason, and the team ends up with a suite that is either flaky or disabled.
The useful tests are different in structure. The structure is the thing most teams get wrong on their first attempt, because the shape of these tests is not the shape teams are used to writing. A team moving from deterministic testing to non-deterministic testing has to learn three new patterns, and the patterns transfer across projects.
Why exact-match tests fail
An exact-match test compares the output of the feature to a stored expected value. For a deterministic feature, this works: the same input produces the same output, and a match confirms the feature is still behaving the way it did when the test was written.
For a non-deterministic feature, the same input produces different outputs. On one run the output is "The user is asking for help with their password" and on the next it is "The user needs assistance with a password issue". Both are correct interpretations. A string match fails on the second one, and the test reports a failure that is not really a failure. The team investigates, finds nothing wrong with the feature, and eventually stops trusting the test.
The common response is to add a tolerance to the match. "Match within 10 percent of the reference." "Match on the first 20 tokens." These tolerances help a little and break on the cases where the output is correct but structurally different. A summariser that correctly produces a shorter output on a shorter input fails a length tolerance even though nothing is wrong. Tolerance-based matching is a workaround that produces a less flaky suite that is still not actually testing the feature.
The deeper issue is that exact-match tests are the wrong shape. The feature's output varies by design, and a test that fights against the variability will keep losing. The useful tests work with the variability rather than against it.
Property-based tests
The first useful pattern is a test that checks properties the output should have, rather than the exact output.
For a language model that summarises a document, the properties might include: the summary is shorter than the input, the summary mentions the main topic that the input is about, the summary does not contain claims that are not in the input, the summary is grammatically well-formed English. Each of these is a property that any correct summary should have, and each is checkable on any specific output.
For a recommender, the properties might include: no banned items appear, the items are from the catalog the user has access to, the top result's relevance score is above a threshold, no duplicate items appear in the list. The properties do not say which items should appear; they say what any list should look like.
For a classifier, the properties might include: the confidence is a number between 0 and 1, the classifier returns exactly one class from the known set, high-confidence predictions are correct more often than low-confidence ones. The properties check the structure and the calibration, not the specific class on a specific input.
Property-based tests catch a specific set of failures: outputs that violate structural invariants, outputs that contain forbidden content, outputs that lose a required piece of information. They do not catch outputs that are structurally correct and semantically wrong, which is the main gap property tests leave.
For related reading, see how to write an acceptance test a machine can run and adding eval tests was our best decision.
Structured grading
The second pattern uses a grader to score the output against a rubric with specific criteria.
For a text-generating feature, the rubric might include: relevance to the input (1 to 5), factual accuracy (1 to 5 with reference material), appropriateness of tone (1 to 5), absence of hallucinations (binary). Each output is graded on each criterion, and the aggregated score is the output's quality.
The grader can be a person, a language model, or (most often) a combination. A language model grader is faster and cheaper per case but introduces variability of its own. A person is more consistent but slower and more expensive. Teams typically use a model grader for routine cases and a person for a sampled subset, to confirm the model's grades are still calibrated.
Structured grading is more informative than pass-fail because it distinguishes degrees of quality. A feature that produces mostly 4s is better than one that produces mostly 3s, even if both pass a binary threshold of "acceptable". The grading detects quality changes that a pass-fail test would miss.
The caveat is that a model grader has its own non-deterministic behaviour, and over time the grader's calibration can drift. Treating the grader as a component with its own monitoring (sample its decisions, compare to human judgements, watch for drift) is part of the discipline. For related reading, see a grader that cannot explain itself and never let the model grade its own work.
Distributional testing
The third pattern checks the distribution of outputs over many runs rather than the content of a single run.
For a feature that should respond in under 100 words on average, a distributional test runs 100 inputs through the feature and checks that the mean response length is in the target range, with the standard deviation inside bounds. The test tolerates the variability of individual runs and still detects a shift in the feature's behaviour.
For a feature that should produce a specific class for a specific input 80 percent of the time, a distributional test runs the same input 50 times and checks that the correct class comes back at least 40 times. A deterministic test would be impossible; a distributional one is informative.
For a feature with a quality metric (from structured grading), a distributional test checks the distribution of grades over a test set. The mean grade, the proportion of cases above a threshold, and the tail (the worst 5 percent of outputs) are all distributional checks. A change in any of these signals a real change in behaviour, even if individual outputs are hard to read.
The sample size for a distributional test depends on the variability of the feature and the size of the change the team wants to detect. For most features, 50 to 200 runs per case gives a stable mean. Below 20 runs the noise dominates the signal, and the test is not informative.
Putting the three together
A mature suite for a non-deterministic feature has all three kinds of test. Each catches something the others do not.
Property-based tests catch structural failures. An output that has the wrong format, contains banned content, or violates a safety constraint is caught by a property test on every run. These are the fast, cheap, specific checks.
Structured-grader tests catch quality failures. An output that is structurally fine but semantically weak (irrelevant to the input, factually incorrect, poorly written) is caught by a grader scoring it low. These are more expensive to run but catch a broader class of issues.
Distributional tests catch drift. A feature that is producing slightly longer outputs, slightly worse quality, or slightly different class distributions than before is caught by a distributional test even when individual outputs look fine. These are the macro-level checks that detect a model regression or a prompt change.
A team that only writes one kind of test misses the failures the other kinds would catch. A suite with all three catches the most relevant failure modes while tolerating the variability inherent to the feature. For related reading, see adding eval tests was our best decision and how to evaluate a vendors eval suite.
What to do when an output drifts but is still correct
A regular issue in non-deterministic testing is that the feature's output changes in a way that is not wrong. The model was upgraded, the prompt was tuned, the data was refreshed, and the outputs are now different but still correct.
The temptation is to update the reference answers and move on. Teams that do this routinely end up with references that have drifted far from where they started, and the drift sometimes includes real quality regressions that nobody investigated.
The better practice is to investigate first. Pull a sample of the newly different outputs, read them against the current references, and decide for each whether the new one is actually better, the same, or worse. If the sample suggests the new outputs are correct, update the references. If the sample reveals that some outputs are worse, fix the feature before updating the references.
This investigation is work, and it is work that is easy to skip. The teams that skip it end up with suites that are slowly hiding regressions. The teams that do it catch quality drift when it is still cheap to fix. For related reading, see how to handle a model upgrade without breaking your evals and how one broken eval hides a regression you care about.
The common mistake of starting with exact-match
Most teams building a non-deterministic feature start with exact-match tests because that is the shape of tests they already know how to write. The first few tests work because the sample is small and the outputs happen to match. The suite scales and starts producing false failures. The team adds tolerances, then workarounds, then eventually disables the flaky tests.
The disabled tests are the signal that the test shape was wrong from the start. The feature needs tests, the team needs to catch regressions, and the exact-match approach cannot provide either. The useful move at that point is not to fix the exact-match tests but to replace them with property-based, grader-based, and distributional tests that actually fit the feature.
Teams that understand this before starting the project build the right kind of suite from the beginning and save the cost of the detour. Teams that do not take the detour, and the detour is usually two or three months of frustration with a suite that does not work. The patterns above are a shortcut around the detour, and they are learnable in a week by a team that has not seen them before.
The suite is part of the product
The deeper point is that a test suite for a non-deterministic feature is not a mechanical check that was added at the end. It is a piece of the product, with its own design and its own maintenance. The suite is as much engineering work as the feature it tests.
Teams that treat the suite as a separate project build better tests and better features. The suite informs the feature's design (if a property cannot be tested, the property should probably not be a requirement) and the feature informs the suite's design (the feature's real failure modes are what the suite should catch).
The teams that treat the suite as overhead end up with tests that pass but do not prove anything, and features that ship with regressions that the suite could have caught if it had been designed for the feature in the first place. Testing a non-deterministic feature well is harder than testing a deterministic one, and the teams that put in the work get a product that keeps its quality over time.


