← Practice guides

How to verify AI-generated code

By PraxisAIUpdated 4 min read

To verify AI-generated code, state the required behavior, run a test that fails on the original bug, then run it after the change and inspect the diff. Check nearby regressions and read the test output. A passing test supports only the behavior it exercised. An agent's explanation is not test evidence.

This guide describes a review method you can use with an AI agent or by yourself. It does not require a particular model. Keep the task's existing tests and conventions as your starting point, then add the smallest check needed to cover the missing behavior.

Write the expectation before the fix

Read the requirement and the affected call path. Specify inputs, outputs, and any behavior that must stay intact. Ask whether the proposed check would fail on the original implementation. A test written to match the new code can pass while preserving the original bug.

For example, suppose a helper should remove duplicate integers while preserving their first appearance. Sorting a set would remove duplicates but change their order. Checking only that the result contains distinct numbers would miss that mistake. The order is part of the contract, so it needs an assertion.

Example: check order, empty input, and zero

This is a small synthetic example, not a solution to a benchmark task. Save it as test_unique.py and run python -m pytest test_unique.py. The first assertion checks order, the second checks empty input, and the third protects zero from accidental filtering. Pytest reports a failed assertion with the observed values.

Replace the helper's return value with sorted(set(values)) and the order check fails. That failure demonstrates that the check can distinguish one plausible wrong implementation. It doesn't establish correctness for a larger program or for types outside the stated integer contract.

Terminal Session
# Synthetic example: integers, first occurrence wins.
def unique_in_order(values):
    return list(dict.fromkeys(values))


def test_unique_in_order():
    assert unique_in_order([2, 1, 2]) == [2, 1]
    assert unique_in_order([]) == []
    assert unique_in_order([0, 0, 1]) == [0, 1]

What if the AI-generated test passes before the fix?

Run the same test against the original implementation. If both versions pass, it has not demonstrated that this bug was fixed. Add an assertion for the behavior in the requirement. For duplicate removal that must preserve order, checking only the number of returned items misses the wrong sorted result.

Save the synthetic example below as test_review_contract.py and run python -m pytest -q test_review_contract.py. With the sorted-set implementation, the size check passes and the order check fails: one passed, one failed. Change only the return statement to list(dict.fromkeys(values)) and rerun. Both checks pass. This is a small demonstration, not a benchmark solution.

Terminal Session
# Synthetic example: integers, first occurrence wins.
def unique_in_order(values):
    return sorted(set(values))  # Bug: sorting changes the required order.


def test_size_alone_misses_the_bug():
    assert len(unique_in_order([2, 1, 2])) == 2


def test_first_occurrence_order():
    assert unique_in_order([2, 1, 2]) == [2, 1]

What should I do if the agent removes tests?

Read the removed assertions before accepting the patch. If they cover required behavior, keep them and fix the implementation. A smaller suite can go green because the failing case disappeared. Run the relevant checks again and explain any test change separately from the code change.

Tests can need legitimate updates when a requirement changes. Tie each removed or rewritten assertion to that documented change and preserve coverage for behavior that still matters. When the interview clock is nearly out, report the evidence you have rather than claiming the remaining patch is verified.

Expand from the failing case

For a repository fix, run the targeted check first, then tests for callers or neighboring behavior affected by the edit. Use the project's runner and the relevant test file. The example's pytest command belongs to that example. Other projects may need their own runner, configuration, or environment.

Read the output closely enough to establish that tests actually ran. Collection errors, skipped cases, and missing dependencies are different from passing assertions. Keep the exact command and result. If you can't run a relevant check, say which behavior remains unverified rather than replacing the result with the agent's prediction.

Review the patch independently

Inspect working-tree changes with git diff and staged changes with git diff --cached. Check untracked files separately. Confirm that the change fixes the source behavior and hasn't weakened tests, changed unrelated configuration, or added a dependency without a task-specific need. A clean diff is easier to reason about than a broad rewrite.

In a live practice round, the graded-test action checks the task's recorded required and existing tests. Submission produces a separate grading result. Read both outcomes and any environment notes. Passing the evaluated tests supports those observed behaviors. It doesn't prove all inputs are correct, that the code is secure, or that every test in the repository passed.

Practice on a real Django bug

In this historical Django task, a trailing newline gets past the username validator. Work with an agent and check its patch. Your score and test result are free. Detailed coaching and replay are paid.

Open the Django task

Read the brief, then create an account to start the exercise. Browse other tasks