YAML Indentation Errors: How to Find and Fix Them
YAML uses whitespace to define structure. A single extra space — or a tab where a space belongs — can silently corrupt your data or cause a cryptic parse error. Here's how to read the error messages and fix each type.
- •
found character '\t' that cannot start any token— tab character used instead of spaces - •
mapping values are not allowed here— colon at wrong indent level or in unquoted string - •
did not find expected key— indentation inconsistency at the same level - •
unexpected end of mapping— child elements over-indented beyond parent
Error 1: Tabs Instead of Spaces
YAML strictly prohibits tab characters (\t) for indentation. This is the most common error for developers whose editor defaults to inserting tabs.
Error 2: Inconsistent Indentation at the Same Level
All keys in the same mapping must align at exactly the same column. Mixing 2-space and 3-space children causes a parse error.
Error 3: "Mapping Values Are Not Allowed Here"
This error appears when a colon appears where the parser doesn't expect one. There are two common causes:
Cause A: Colon in an unquoted string value
Cause B: Wrong indentation of a nested key
Error 4: List Item Indentation
List items use - followed by a space. The dash must be indented at least one space more than the parent key. Keys inside a list item must be indented further than the dash.
Error 5: Indentation After Multi-line Strings
Block scalars (| literal or > folded) must be indented more than their key. Keys after the block scalar must return to the correct level.
Debugging YAML Indentation
Frequently Asked Questions
Why does YAML use indentation instead of braces?
YAML was designed for human readability and editing. Indentation-based structure reads more like natural text and eliminates visual clutter (no brackets, no commas). The trade-off is that whitespace becomes significant, which can be surprising for developers used to languages that ignore whitespace. JSON (which uses braces) was designed for machine-to-machine data exchange and is more lenient about whitespace.
Does it matter if I use 2-space or 4-space indentation in YAML?
No — YAML does not mandate a specific number of spaces. Each level just needs to be indented more than its parent, and all siblings at the same level must match. 2 spaces is the community convention (Kubernetes, GitHub Actions, Ansible all use it) and is recommended for readability. The only rule: tabs are never allowed.
How do I fix YAML indentation in VS Code?
Right-click in the editor and choose "Format Document" if a YAML formatter extension is installed (Prettier or YAML by Red Hat). Alternatively, open the Command Palette (Ctrl+Shift+P), search "Convert Indentation to Spaces", and set tab size to 2. The YAML extension by Red Hat also shows indentation guides and highlights errors inline.
Why does "kubectl apply" fail with an indentation error even though my YAML looks correct?
Common causes: (1) Copy-pasted from a website that converted spaces to HTML non-breaking spaces ( ). These look identical to regular spaces but are not valid YAML. (2) Your terminal editor inserted actual tabs. (3) The file has Windows line endings (CRLF) which confuse some YAML parsers on Linux. Run: cat -A file.yaml | head -20 to see hidden characters (tabs show as ^I, Windows endings as ^M$).
Key Takeaways
- Tabs are never allowed in YAML — configure your editor to insert spaces.
- All siblings at the same level must be at exactly the same column.
- "Mapping values not allowed here" usually means a colon in an unquoted string — wrap it in quotes.
- Enable whitespace visualization in your editor to see spaces vs tabs at a glance.
- Use yamllint or our YAML Validator for exact error location and rule explanation.