YAML Indentation

Spaces, tabs, common errors, and how to fix indentation fast.

The one rule

YAML uses spaces to show nesting. Tabs are not allowed for indentation. Mixing spaces and tabs is the most common cause of a YAML parse error.

How many spaces?

The YAML spec does not mandate a number, but the convention is 2 spaces per indentation level. Four spaces also works as long as every level in the same file uses the same step.

# 2-space indentation (recommended)
config:
  debug: true
  services:
    - api
    - db

# 4-space indentation (valid, but be consistent)
config:
    debug: true
    services:
        - api
        - db

Common indentation errors

# Bad: mixing depths
items:
  - name: api
   port: 8080   # one space too many

# Bad: tab before "debug"
config:
	debug: true

How to fix YAML indentation

  1. Open the YAML formatter.
  2. Paste your messy YAML into the input box.
  3. Copy the output, which is re-indented with consistent 2-space spacing.

For structural problems, run the same text through the YAML validator to see the exact line number of the error.

Why does indentation matter in YAML?

Unlike JSON, YAML has no braces or closing tags. The structure is entirely defined by indentation. If the indentation is wrong, the parser cannot tell what belongs to what.

Related guides