Sub topic 2
How AI-Generated Code Typically Goes Wrong
This chapter assumes you can read a Git diff, write basic unit tests, and have used an AI coding assistant. It describes the failure patterns to look for when you review AI output. Later chapters cover how to check for each one.
Why the failures look different
A human author's mistakes often show up as sloppy style or obvious gaps. AI-generated code is usually well formatted, consistently named, and confident. The mistakes sit underneath that polish. A model predicts likely code from patterns it has seen. It does not run your program or read your requirements the way you do. So the code tends to be plausible, and plausible is not the same as correct.
The common failure modes
Hallucinated APIs and packages
The model may call a function, method, or option that does not exist, or import a package that was never published. The call often has a believable name and a believable signature. A made-up package name is also a supply-chain risk, because an attacker can publish a real package under that name.
Missed edge cases
The model writes for the typical input. Empty collections, None, zero, negative numbers, very large inputs, and unusual characters are often left out. The function below looks correct and works on every example you would try first.
def average(values):
return sum(values) / len(values)Calling average([]) raises ZeroDivisionError. The requirement never said what to do with an empty list, so the code never decided.
Outdated patterns
Training data includes years of older code. The model may use a deprecated function, an old library version's interface, or a pattern the ecosystem has since replaced. The code can still run today and then break on upgrade, or run with warnings you never see.
Insecure defaults
The model often copies the shortest working form of a task. For database access, that can mean building SQL with string formatting.
import sqlite3
def find_user_unsafe(conn: sqlite3.Connection, name: str):
# Builds SQL by string formatting: vulnerable to injection.
query = f"SELECT id, name FROM users WHERE name = '{name}'"
return conn.execute(query).fetchall()Passing "' OR '1'='1" as name returns every row. Hardcoded secrets, disabled certificate checks, and missing input validation are other common cases.
Scope drift and unrequested changes
You ask for a bug fix, and the diff also renames variables, reformats a file, or changes a function you did not mention. Each extra change is something you have to review and something that can break.
Tests that prove little
Generated tests can mirror the implementation, so they pass whatever the code does. They may assert only the happy path, or mock away the very behavior you wanted tested. A green test run is evidence only if the tests could have failed.
Where each failure shows up
| Failure mode | Where to look in the diff | What it looks like |
|---|---|---|
| Hallucinated API | Imports and call sites | Unfamiliar package or method name |
| Missed edge case | Branches and arithmetic | No handling for empty, zero, or `None` |
| Outdated pattern | Library calls | Deprecated functions or old idioms |
| Insecure default | Queries, file paths, config | String-built SQL, literal keys |
| Scope drift | File list in `git diff --stat` | Files you did not expect |
| Weak tests | Test files | Only the happy path is asserted |
How the pieces connect
Practice
Exercise
A generated function load_config(path) imports configparser_plus, a package you have never heard of, and its tests pass. Which failure modes might apply, and what should you do first?
Show answer
The most likely mode is a hallucinated or unvetted package, which is also a supply-chain risk. Passing tests do not show the package is legitimate. First check that configparser_plus exists on the official package index, who maintains it, and whether the standard library or an existing dependency already does the job. Then check whether the tests exercise failure cases such as a missing file.
Exercise
A generated median(values) function passes three tests, all using lists of odd length. Name two edge cases the tests miss.
Show answer
Lists of even length, where the median is the mean of the two middle values, and the empty list. Single-element lists, unsorted input, and None values are also worth testing.