Build a group-safe dataset gate
Implement gate_examples(rows, heldout_groups) to validate records, protect evaluation groups, deduplicate canonical prompt-response pairs, and report deterministic reasons without mutating inputs.
Your task
- Each row is a dict with string id, group, prompt, and response. Missing, non-string, or blank required fields are invalid.
- Reject invalid rows first, then rows whose group is held out, then duplicate normalized prompt-response pairs. Normalize with NFKC, casefold, and whitespace collapse.
- Keep the first acceptable record for each pair in input order. A rejected held-out row must not consume a deduplication key.
- Return {"kept": [row ids], "rejected": [(row id or None, reason)]}, where reason is invalid, heldout, or duplicate. Inputs are not modified.
Examples
EXAMPLE 1
InputTwo rows with equivalent normalized prompt-response pairs
Output{"kept": ["first"], "rejected": [("second", "duplicate")]}
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor