The five errors you'll see over and over
Python has dozens of built-in error classes. AI-generated code, in practice, hits maybe five of them again and again. If you can recognize those five on sight, you can diagnose almost any crash in the first second of reading the traceback — before you read the message, before you check the line number.
This is one of those force multipliers that turns "AI's code is
broken and I have no idea what's wrong" into "oh, that's a KeyError,
let me check the JSON shape."
The five core errors and what they actually mean
| Error | Plain English | Where it comes from |
|---|---|---|
NameError | A variable name that doesn't exist | Typo in a variable name, forgot an import, used something before defining it |
TypeError | The operation doesn't work on this type | Mixed types ("5" + 10), passed None where a value was expected, wrong number of args to a function |
KeyError | A dict doesn't have the key you asked for | Typoed key, key missing from API response, capitalization mismatch |
IndexError | A list isn't long enough | Off-by-one bug, list came back empty, asked for items[5] on a 3-item list |
AttributeError | You called a method on the wrong type | Almost always None.something() — a function returned None and you tried to use it |
Read the table once. Come back to it whenever a crash surprises you. Within a week the translation is automatic.
The diagnostic shortcut
Each error class is a fingerprint — a signal of which kind of bug you're looking at, before you even read the message. When AI hands you a stack trace:
- See
NameError? It's a typo or a missing import. Look at the exact name in the error and search the file for it. - See
TypeError? Something is the wrong type. OftenNoneshowing up where a value should be — which means a function returnedNoneinstead of what you expected. Hello, missing-return bug from the read-the-script lab. - See
KeyError? A dict is missing a key. Either the key is typoed in the code, or the data shape isn't what AI assumed. Print the dict's keys and compare. - See
IndexError? A list is shorter than expected. Empty list, off-by-one, range mismatch. - See
AttributeError? You called.something()on something that didn't have it. UsuallyNone. Find the function whose return value you used and check why it gave you nothing.
Where AI specifically gets each one wrong
Cursor doesn't just read these errors — it also generates them predictably:
NameErrorfrom refactor drift. AI renames a variable in one spot and forgets the other usage. The script crashes the first time it hits the unrenamed line.TypeErrorfromNonepropagation. AI writes a function with a missingreturn. Calling code uses the result, blows up on the next operation. Symptom is aTypeErrortwo lines downstream of the actual bug.KeyErrorfrom optimistic API shape assumptions. AI writesresponse["data"]["users"]based on the docs — but the live API returned{"error": "rate limited"}. No"data"key, no defensive check, instant crash.AttributeError: NoneType has no attribute X. The all-time champion of Python bugs. Some function returnedNone. Find which one. The fix is usually upstream of where the crash is.
And one more misuse to check for: when AI can only see the message
and not the full traceback, it guesses at fixes — sometimes by adding
defensive lookups everywhere when only one was needed. A TypeError: 'NoneType' object is not subscriptable is almost always upstream
of where it crashed. The right fix is to find which function
returned None and why, not to wrap the crash site in a
try/except.
The two extras you'll also see
Two more errors that don't make the top-five but appear weekly:
ValueError— right type, wrong content.int("hello"). The argument type is correct, but the value can't be converted. Almost always from user input or upstream data that's dirtier than AI assumed.ZeroDivisionError— divided by zero. Almost always means the denominator came from a count that was empty. The fix is to check the denominator, not to hide the division.
When you see these, the fix is content validation, not type
validation. You'll drill both again in the try/except lesson next.
The reflex to build
For the next month, every time you see a traceback:
- Read the last line first — the class plus the message.
- Identify the class. Use the tables above to know what kind of bug it is.
- Form a one-sentence hypothesis: "a dict is missing a key" or "a function returned None."
- Then look at the line numbers and source code to confirm.
The class does most of the diagnostic work for you. Use it.