Testing

How to test a feature whose output is not deterministic

Editorial · Reveneau · December 12, 2026

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.

Common questions

Why can't you test a non-deterministic feature with exact-match assertions?

Because the same input produces different outputs on different runs, and a strict comparison fails on outputs that are correct. An exact-match test for a feature with variability fails for the wrong reason most of the time and succeeds only when the output happens to match a stored reference. Either outcome is misleading, and the test is not doing the job the team hired it for.

What kinds of features have non-deterministic output?

Anything that calls a language model, a recommender system, a search ranker, a classifier with calibrated confidence, a system that uses randomness in its internal logic, or an external service whose output varies. In modern products, this covers most AI features and many search and personalisation features.

What is a property-based test?

A test that checks properties the output should have rather than the exact output. For a language model that summarises a document, the property might be: the summary is shorter than the input, the summary contains the main topic, the summary does not contradict the input. These properties can be checked on any output, including ones the team has not seen before.

How do you decide what properties to check?

Start from the feature's requirements. What does the team want the output to have? What would make an output wrong? The first list becomes the positive properties (things the output must have), and the second list becomes the negative ones (things the output must not have). Both kinds are useful, and the negative ones often catch more real bugs.

What is structured grading?

An evaluation method where each output is graded against a rubric with specific criteria, usually by a model or by a reviewer following the rubric. The grade is a score (often 1 to 5) on each criterion, and the aggregated score is the output's quality. Structured grading is more informative than pass-fail, because it distinguishes between near-misses and total failures.

When should you use a language model as a grader?

When the thing being graded is hard to specify with a rule but easy for a competent reader to judge. A grader can judge tone, relevance, factual correctness (with reference material), and appropriateness, which rule-based tests cannot. The caveat is that the grader has its own variability, so the grader's behaviour has to be monitored like any other non-deterministic component.

What is distributional testing?

A test that checks the distribution of outputs over many runs rather than the single output of one run. A feature that should produce a response under 100 words on average can be tested by running 100 inputs and checking that the mean length is in range, with the standard deviation inside bounds. The test tolerates the variability of individual runs and still catches drift in the feature's behaviour.

How many runs are needed for a distributional test?

Depends on the variability of the feature and the size of the change you want to detect. A feature with low variability needs fewer runs; one with high variability needs more. For most features, 50 to 200 runs per test case gives a stable mean and a usable standard deviation. Below 20 runs the noise dominates the signal and the test is not informative.

How do you handle a test that fails because the output drifted but is still correct?

Investigate first, update second. If the output is genuinely correct but different from the stored reference, the reference should be updated. If the reference was correct and the output is wrong, the feature needs to be fixed. Most teams update references too readily, which lets quality drift silently; investigating first catches the drift when it matters.

What does a mature test suite for a non-deterministic feature look like?

A combination of three kinds of test: property-based tests that check specific attributes on each output, structured-grader tests that score outputs against rubrics, and distributional tests that check behaviour over many runs. Each kind catches something the others do not, and a suite with all three catches the most relevant failure modes while tolerating the variability that is inherent to the feature.

Start a project