IndentationError: unexpected indent / expected an indented block
Python’s block structure is broken — inconsistent indentation or mixed tabs and spaces.
unexpected indentexpected an indented blockunindent does not match any outer indentation level Meaning
Indentation is syntax in Python. Every block after : must be indented consistently, and mixing tabs and spaces (TabError) is not allowed.
Common causes
- Mixed tabs and spaces
- Empty block after
if/def/for(usepass) - Copy-pasted code with different indentation
- Stray indentation on a top-level line
⚡ Quick fix
- Configure the editor to insert 4 spaces
- Convert tabs to spaces for the file
- Add
passto empty blocks
Detailed fix by platform
Python
python -m tabnanny file.pyreports ambiguous indentation.
Code examples
Empty block needs pass
def todo():
# ❌ IndentationError: expected an indented block
def todo():
pass # ✅Convert tabs to spaces
python -m tabnanny app.py # reports ambiguous lines
expand -t 4 app.py > app_fixed.py # tabs → 4 spacesHow to diagnose
- Line — Reported line and the block header above it
- Whitespace — Show invisible characters in your editor — tabs vs spaces?
- Empty blocks — Any if/def/for with no body?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- await outside async SyntaxError: await is only valid in async functions and the top level bodies of modules
- compose validation docker compose: services.x Additional property y is not allowed / yaml: line N: mapping values are not allowed
- GraphQL Syntax Error GraphQL: Syntax Error: Expected Name, found <EOF> / Unexpected }
Most viewed in Python
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 14 Oct 2026