scripts/dataset_quality.py evaluates local LLM training datasets across pre-training and post-training stages and emits a structured JSON quality receipt (schema_version: 1) plus a Markdown summary. Standard library only (Python 3.9+); supports .jsonl, .ndjson, .json, .csv, .tsv, .txt, .gz variants, directories, and optional .parquet (when pyarrow or duckdb is available).
Run it from this skill's folder:
# Auto-detect dataset type (pretrain, sft, preference, rl) and score it python3 scripts/dataset_quality.py eval /path/to/dataset.jsonl --out receipt.json # Evaluate an SFT dataset and check for train/eval contamination python3 scripts/dataset_quality.py eval train.jsonl --type sft --eval-path eval.jsonl --out sft_receipt.json # Evaluate a code pre-training corpus (relaxes symbol/prose thresholds) python3 scripts/dataset_quality.py eval /path/to/code_corpus/ --type pretrain --domain code --max-rows 20000 # Render a Markdown report or compare before/after cleaning receipts python3 scripts/dataset_quality.py report receipt.json python3 scripts/dataset_quality.py compare before.json after.json
Supported dataset types
Type (--type) | Auto-detected when | Extra checks enabled |
|---|---|---|
pretrain | text, content, document, body, or plain .txt | Stricter min-length (30 words), Gopher/FineWeb heuristics, zlib density, MinHash LSH near-dedup |
sft | messages, conversations, instruction/output, prompt/completion, question/answer | Chat role alternation, refusal rate, AI-slop clichés, truncated fences, <think>...</think> CoT balance |
preference | chosen + rejected (or accepted + rejected) | Verbosity/length bias (len(chosen) > len(rejected)), identical/near-identical pairs, prefix mismatch, refusal-as-chosen |
rl | Prompt column + ground_truth, verifier, tests, solution, oracle, or expected_answer | Empty ground truth, answer distribution entropy, verbatim prompt-answer leakage |
Scoring dimensions (0–100)
Every dimension starts at 100 and applies documented, capped deductions for each measured defect. The overall quality_score is the weighted sum of active dimensions (A: >= 90, B: >= 80, C: >= 70, D: >= 55, F: < 55).
| Dimension | Pretrain weight | SFT / RL weight | Preference weight | Techniques combined |
|---|---|---|---|---|
schema | 12% | 15% / 18% | 15% | JSON/row parse errors, missing required fields, UTF-8 mojibake/U+FFFD, null/control bytes, chat role alternation, identical preference pairs |
dedup | 25% | 18% | 16% | Normalized SHA-256 exact dedup, pure-Python 64-hash MinHash LSH near-dedup (5-shingles), intra-document line/5-gram repetition, duplicate prompts, optional exact + 13-gram train/eval contamination (--eval-path) |
heuristics | 28% | 17% / 16% | 15% | Gopher/FineWeb rules (min words, mean word length, symbol/word ratio, stopword presence), web boilerplate & raw HTML detection, zlib compression ratio (flags repetitive templates < 0.18 and random/encoded noise > 0.85) |
diversity | 20% | 15% / 16% | 14% | Moving-Average Type-Token Ratio (MATTR-50), corpus distinct-2 / distinct-3 n-gram diversity, token-length percentiles (p10/p50/p90/p99), top prompt-prefix concentration, Unicode script distribution |
safety | 15% | 13% / 10% | 12% | Regex + Luhn-validated credit cards, SSNs, emails, phone numbers, IPv4, exposed API/private keys (sk-, ghp_, AKIA, hf_, PEM blocks), unresolved TODO/[INSERT ...] placeholders |
posttrain | — | 22% | 28% | Stage-specific diagnostics for SFT/CoT, Preference/DPO, and RL/verifiable tasks |
Verdicts & critical flags
ready:quality_score >= 80andcritical_flags == 0.needs_cleaning:60 <= quality_score < 80andcritical_flags == 0.high_risk:quality_score < 60orcritical_flags > 0.
Critical flags are triggered immediately by exposed API/private keys or exact train/eval overlap when --eval-path is supplied.
Useful flags
--max-rows N(default10000,0= all): streams large files with deterministic reservoir sampling (--seed 42) so multi-gigabyte corpora can be audited quickly in constant memory.--domain general|code(defaultgeneral):coderelaxes symbol-to-word, HTML, and prose stopword rules.--text-field NAME: explicitly pick the text column forpretraindatasets with custom schemas.--near-dup-threshold 0.80: tune the MinHash LSH Jaccard threshold.--format json|markdown: print raw JSON receipt or Markdown summary to stdout.
Reading scores honestly
- High score != semantic truth or benchmark gain: this skill measures structural, statistical, deduplication, hygiene, and post-training failure modes locally without running a model. It does not verify factual accuracy, mathematical correctness of CoT traces, code execution, or downstream training lift.
- Sampling trade-offs: with
--max-rows 10000on a 10M-row corpus, global exact/near-duplicate rates are lower bounds on true corpus-wide duplication. Increase--max-rowsor run a dedicated full-corpus dedup pass before large pre-training runs. - PII & safety are heuristic: regex + Luhn checks catch common leaks (keys, emails, SSNs, cards) but are not a legal compliance audit or a full toxicity classifier.
- Domain calibration: non-Latin scripts skip the Latin stopword check automatically, and code corpora should be evaluated with
--domain code. Always inspecttop_issuesand the rawdimensions.*.metricsrather than relying on the headline number alone.