Practical guides

How to catch dropped or duplicated records in an AI-assisted CSV transformation

RedditLinkedInBlueskyXFacebookEmailText message
Copy link

A repair desk asks an assistant to shorten the status on each ticket. Four tickets go in and four rows come back. Count each ticket ID to find the missing ticket and the one that appeared twice.

By Backprop Bulletin.Comment on this guide Published 2026-11-30. Last checked 2026-11-27.

Prerequisites

Use Bash and Python 3. The example runs offline with the standard library and keeps the invented CSVs in memory. The intended transformation preserves one row per input ticket.

Worked example

  1. Keep the original export. Choose a column that identifies the same ticket before and after the transformation. Each input ID must be present, unique and copied exactly, including leading zeros. A row number assigned after sorting will identify a position instead of a ticket.
  2. Run this block. The first output repeats a ticket. The second output groups the checked tickets by status, so their order changes. Both outputs use the same four-row input.
  3. python3 - <<'PY'
    import csv
    from collections import Counter
    from io import StringIO
    
    original = """id,status
    A01,awaiting parts
    A02,ready for pickup
    A03,awaiting parts
    A04,ready for pickup
    """
    
    transformed = """id,status
    A01,waiting
    A02,ready
    A02,ready
    A04,ready
    """
    
    reordered = """id,status
    A02,ready
    A04,ready
    A01,waiting
    A03,waiting
    """
    
    def count_ids(stream):
        reader = csv.DictReader(stream)
        if (reader.fieldnames or []).count("id") != 1:
            raise ValueError("Expected one id column")
        counts = Counter()
        for row in reader:
            key = row["id"]
            if key is None or not key.strip():
                raise ValueError(f"Blank ID at CSV line {reader.line_num}")
            counts[key] += 1
        return counts
    
    def compare(label, before, after):
        missing = before - after
        extra = after - before
        print(f"{label}: {sum(before.values())} input rows, {sum(after.values())} output rows")
        if not missing and not extra:
            print("PASS: every input ID appears exactly once")
            return
        for kind, counts in [("missing", missing), ("extra", extra)]:
            for key, copies in sorted(counts.items()):
                print(f"{kind}: {key} x {copies}")
    
    before = count_ids(StringIO(original))
    if any(n != 1 for n in before.values()):
        raise ValueError("Input IDs must be unique")
    compare("Transformed", before, count_ids(StringIO(transformed)))
    compare("Reordered", before, count_ids(StringIO(reordered)))
    PY
  4. Read each difference as a number of copies. A03 went from one copy to zero. A02 went from one to two, so it contributes one extra copy. Those changes cancel in the total. In the reordered output, each ID still appears once.
  5. To use your own exports, open each CSV with open(path, encoding="utf-8-sig", newline="") in a with block and pass the open file to count_ids. Keep the input uniqueness check. Investigate each reported ID against the original before accepting or repairing the output.

Expected output

Transformed: 4 input rows, 4 output rows
missing: A03 x 1
extra: A02 x 1
Reordered: 4 input rows, 4 output rows
PASS: every input ID appears exactly once

Limitations

Passing establishes that the identifiers and their counts survived. A03 could still say “ready” when the source says “awaiting parts.” Check that separately by looking up each input ID and comparing its output status with the agreed conversion. A task that intentionally filters or combines records needs a different rule for which IDs and counts to expect.

Sources