AssertionError
Python Programming Language
Severity: ModerateWhat Does This Error Mean?
AssertionError means an assert statement in your code evaluated to False. Assert statements are checks you write to verify your code's assumptions — for example, that a value is not None, or that a list has items before you process it. When the assumption is wrong, Python raises AssertionError to stop execution immediately so you can catch the problem early.
Affected Models
- Python 2.x
- Python 3.x
- All Python versions
Common Causes
- An assert statement whose condition evaluated to False — the assumption your code was making turned out to be wrong
- Unexpected data coming from an external source (user input, an API, a file) that violated an assumption your code made
- A bug in a previous step of the program left a variable in a state your code did not expect
- Test code using assert to verify expected results — failing tests always raise AssertionError
- Third-party libraries use assert internally — passing incorrect arguments can trigger their internal assertions
How to Fix It
-
Find the failing assert statement. The traceback shows the file and line number. Read the condition in the assert to understand what it expected.
Example: assert user is not None — this fails if user is None. The fix is to find why user ended up None.
-
Trace back to where the variable was set. Use print statements or a debugger to inspect the variable's value at each step. Find where it got the unexpected value.
Add a descriptive message to your assert: assert x > 0, f'x should be positive but got {x}' — this makes the error much easier to diagnose.
-
Fix the underlying cause. If the assertion found a real bug (wrong value was computed), fix the logic that produced it. Do not just remove the assert — it exists for a good reason.
If the assertion is wrong and the condition is actually valid, update the assertion to match the real requirement.
-
If the AssertionError comes from test code (like pytest or unittest), read the test to understand what the expected output is. Fix your code or test so they agree.
Test frameworks use assert heavily. AssertionError from tests means your code's output does not match what the test expected.
-
Do not use assert for input validation in production code. Assertions can be disabled globally by running Python with the -O (optimise) flag. Use if/raise ValueError instead for production validation.
assert is for developer-time debugging. For user-facing validation, always use explicit if checks and raise appropriate exceptions.
When to Call a Professional
AssertionError is always something you fix in code. Read the assertion that failed and understand what assumption it was checking. Fix the root cause — either the logic that produces the bad value, or update the assertion if your assumptions changed.
Frequently Asked Questions
Should I use assert for input validation?
No. Assert statements are meant for developer debugging, not production validation. If you run Python with the -O flag (optimise mode), all assert statements are silently removed. For production input validation, use if statements and raise ValueError, TypeError, or a custom exception. Assert is best used in tests and for checking internal logic invariants during development.
What is the difference between AssertionError and a regular exception?
AssertionError is a regular Python exception — it inherits from Exception just like ValueError or TypeError. The difference is intent. Regular exceptions handle expected failure conditions. AssertionError signals 'something that should never be False was False' — a bug in the program's logic. You should almost never catch AssertionError with try/except; instead, fix the underlying bug.
How do I add a message to an assertion to make it more helpful?
Write the message after a comma: assert condition, 'Helpful message here'. Example: assert len(items) > 0, 'Cannot process an empty list — check data loading step'. The message appears in the AssertionError traceback, making it much easier to understand what went wrong without digging into the code.