Every generated Python script I get is happy-path only: no retries, bare excepts, crashes on the first weird input. I've been experimenting with a checklist-style prompt suffix that forces defensive code, pasted below.
It works decently, but it makes the model over-engineer tiny scripts. A 10-line CSV filter comes back as 150 lines with custom exception classes.
How do you calibrate the amount of defensiveness to the size of the task?
Write the Python script described above, then apply this hardening pass before showing me anything:
- Every file/network/subprocess operation wrapped with specific exception types (never bare except)
- Retries with exponential backoff (3 attempts) on network calls only
- Input validation at every public function boundary with actionable error messages ("expected a CSV with columns a,b,c; got columns x,y")
- Exit codes: 0 success, 1 user error, 2 environment error, plus a --verbose flag that enables debug logging
- If the script takes arguments, use argparse with examples in the epilog
- End with a self-check: list each failure mode you handled and each one you consciously did NOT handle and why
Keep the core logic readable; hardening must not triple the line count.