Sub topic 11
Split the data and avoid leakage
This chapter assumes you can read Python and JSONL, have a dataset with stable case ids and tags (from the schema chapter), and know what a proportion and a sample size are.
Why split at all
You will tune your prompt, retrieval settings, and model choice against the eval score. Each tuning step fits the app a little more to the cases you look at. If you report the score from those same cases, it is optimistic. The fix is to divide the data:
- Dev split: cases you read, debug, and tune against.
- Test split: cases you do not look at while tuning. You run them rarely, to get an honest score before a release.
A common starting point is 80% dev and 20% test. Small datasets may need more in test so the score is not too noisy. The dataset size chapter covers that trade-off.
Split by hashing the id
A random shuffle works once. When the dataset grows, a new shuffle moves old cases between splits, and test cases you have already seen end up in dev or the reverse. Hashing the case id avoids this. The same id always lands in the same split.
import hashlib
def split_for(case_id, test_percent=20):
digest = hashlib.sha256(case_id.encode("utf-8")).hexdigest()
return "test" if int(digest, 16) % 100 < test_percent else "dev"Apply it when you load the data:
cases = load_cases("cases.jsonl")
dev = [c for c in cases if split_for(c["id"]) == "dev"]
test = [c for c in cases if split_for(c["id"]) == "test"]
print(len(dev), len(test))The split is approximate. With 50 cases you may get 7 or 13 in test, not exactly 10. Check the counts, and check that each important tag appears in both splits.
Common leakage paths
Leakage means information from the test cases reaches the app or your tuning decisions. The score then measures memory, not ability.
- Near-duplicates across splits. "Can I return a laptop after 45 days?" and "Can I return a laptop after 45 days??" hash to different splits. Normalize text and remove duplicates or group them before splitting.
- Few-shot examples taken from the test set. If a prompt example is also a test case, the model sees the answer.
- Tuning on test results. Reading failed test cases and editing the prompt to fix them turns test into dev.
- Synthetic variants of one seed. If you generate ten paraphrases from one production case, keep them together in one split.
- Model training data. A public benchmark may already be in the model's training data. Prefer cases from your own logs and experts.
Keep variants together
To keep related cases in one split, hash a group id instead of the case id. Add a group field, such as the id of the seed case, and fall back to id when there is none:
def split_for_case(case, test_percent=20):
return split_for(case.get("group", case["id"]), test_percent)Test discipline
- Do not read individual test failures during tuning. Look at the aggregate score.
- If you must inspect a test failure, move that case to dev and replace it in test with a fresh one.
- Record the dataset version with every test run, so you can tell later which cases were in it.
Exercise
You generate five paraphrases of each production case, then split by each paraphrase's own id. The test score is much higher than the score on fresh production cases. What went wrong, and how do you fix it?
Show answer
Paraphrases of one seed case landed in both dev and test, so the app was tuned on near-copies of the test cases. Give all paraphrases of a seed the same group id and split by that group id, as in splitforcase.