Back to the blog

Tony CantaveHow I Check a Python Code Sample Before It Goes Into AI Training Data

By Tony Cantave ·

Part of my work as a freelance AI evaluator is writing and debugging small Python and JavaScript samples that are used for model training. A sample in that setting has a different job than code in a normal project. Nobody will run it in production, but a model may learn from it, so every habit in it gets copied. A sample that almost works teaches a model to almost work.

Over time I settled into a short routine that I run on every Python sample before I call it done. None of it is fancy. It is the same discipline I used for years in logistics and manufacturing: check the thing before it leaves the dock, because fixing it later always costs more.

1. Run it exactly as written

The first check is to copy the sample into a fresh file and run it with nothing else around it.

python3 sample.py

A fresh file catches missing imports, variables defined somewhere I forgot about, and functions that depend on leftovers from an earlier experiment. If the sample only works inside my editor session, it does not work.

2. Read the prompt again, then read the code

Before testing anything clever, I reread the task the sample is supposed to solve. A surprising number of problems are not crashes at all. The code runs fine and solves a slightly different problem than the one asked. If the prompt says "return the average of the scores, or None when there are no scores," then a function that raises an error on an empty list is wrong, even if it is perfectly good Python.

3. Write the asserts the prompt implies

Say the sample looks like this:

def average(scores):
    return sum(scores) / len(scores)

The prompt asked for None on an empty list, so I write checks straight from the prompt wording:

assert average([2, 4, 6]) == 4
assert average([5]) == 5
assert average([]) is None

The third assert fails with a ZeroDivisionError, which shows exactly where the sample falls short. The fix is small:

def average(scores):
    if not scores:
        return None
    return sum(scores) / len(scores)

I keep these asserts in a scratch file. They are not part of the sample unless the task asks for tests, but they are how I prove the sample does what it claims.

4. Try the inputs people actually send

After the happy path I try the inputs real users send: an empty string, a list with one item, very large numbers, text with extra spaces or mixed case, and floats that do not divide cleanly. For float results I compare with math.isclose instead of ==, because a correct answer can still print as 0.30000000000000004.

import math

assert math.isclose(average([0.1, 0.2]), 0.15)

5. Make the output predictable

If a sample prints results, the output should be the same on every run. I look for things that quietly change: looping over a set and printing in that order, using the current date, or calling random without a seed. A model that learns from unpredictable output learns that unpredictable output is fine.

6. Check that the explanation matches the code

Most samples come with a short explanation. I read it line by line against the code. If the explanation says the function handles empty input, I confirm there is a line that does that. If it mentions how fast the code runs, I look at the loops. Mismatches here are easy to miss because the code and the text each look fine on their own.

7. Keep it as simple as the task allows

The last pass is about style. I remove clever one liners that a beginner would trip over, rename variables like x and tmp to names that say what they hold, and drop imports that are no longer used. A training sample should look like code you would want a new developer to copy, because in a sense that is exactly what will happen.

The checklist I keep next to me

  1. Runs in a fresh file with no hidden setup.
  2. Solves the task as written, not a nearby task.
  3. Asserts written straight from the prompt all pass.
  4. Empty, single item, large and messy inputs are tested.
  5. Floats are compared with math.isclose.
  6. Output is the same on every run.
  7. The explanation matches what the code actually does.
  8. Names are clear and unused code is gone.

Why this matters beyond training data

I started this routine for AI evaluation work, but it has made my own scripts better too. The same checks apply to anything I build, including the screen management software I am writing for Local Flyer Ads. Code that has been run fresh, tested against its own promises and explained honestly is code I trust to leave the dock.

If you write samples, tutorials or snippets that other people will copy, try running them through this list once. The first pass usually turns up at least one thing worth fixing.