IndentationError Python Origin: Application & API OSI layer 7 · Application Last updated 14 Oct 2026

IndentationError: unexpected indent / expected an indented block

Python’s block structure is broken — inconsistent indentation or mixed tabs and spaces.

Exact message you may see unexpected indentexpected an indented blockunindent does not match any outer indentation level
Seen on: Python

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 (use pass)
  • Copy-pasted code with different indentation
  • Stray indentation on a top-level line

⚡ Quick fix

  1. Configure the editor to insert 4 spaces
  2. Convert tabs to spaces for the file
  3. Add pass to empty blocks

Detailed fix by platform

Python

  1. python -m tabnanny file.py reports ambiguous indentation.

Code examples

Empty block needs pass

python
def todo():
    # ❌ IndentationError: expected an indented block
def todo():
    pass        # ✅

Convert tabs to spaces

bash
python -m tabnanny app.py           # reports ambiguous lines
expand -t 4 app.py > app_fixed.py   # tabs → 4 spaces

How to diagnose

  1. Line — Reported line and the block header above it
  2. Whitespace — Show invisible characters in your editor — tabs vs spaces?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

156 — SQL Server Msg 156: Incorrect syntax near the keyword 'X' (SQL)156500 — 500 5.5.1 Command unrecognized / Syntax error, command unrecognized (Email / SMTP)500501 — 501 5.1.7 Bad sender address syntax (invalid MAIL FROM) (Email / SMTP)501501 — 501 5.5.4 Syntax error in parameters or arguments (invalid SMTP command argument) (Email / SMTP)501await outside async — SyntaxError: await is only valid in async functions and the top level bodies of modules (JavaScript)await outside asynccompose validation — docker compose: services.x Additional property y is not allowed / yaml: line N: mapping values are not allowed (Docker)compose validationdouble quotes — "column" not found: double quotes mean identifiers, single quotes mean strings (SQL)double quotesERR wrong number of arguments / syntax error — Redis: (error) ERR wrong number of arguments for 'set' command / ERR syntax error (Redis)ERR wrong number of a…Indentati…

Explore the map of this error Full error map →

🧠 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.