YAML (YAML Ain't Markup Language) is the go-to configuration format for Docker, Kubernetes, CI/CD pipelines, and countless other tools. Despite its human-friendly syntax, YAML is surprisingly easy to get wrong. This guide covers the most common YAML errors and how to fix them.
Where YAML Is Used
- Docker Compose (docker-compose.yml)
- Kubernetes manifests
- GitHub Actions / GitLab CI workflows
- Ansible playbooks
- Configuration files (Prettier, ESLint, etc.)
Basic YAML Syntax Rules
Indentation
YAML uses spaces (never tabs) for indentation. The number of spaces must be consistent within the same level:
# β
Correct (2-space indentation)
server:
host: localhost
port: 8080
database:
name: myapp
pool: 10
# β Wrong (tab characters or inconsistent spaces)
server:
host: localhost # Tab character - INVALID!
port: 8080 # 3 spaces - inconsistent!Key-Value Pairs
Keys and values are separated by a colon followed by a space:
# β
Correct
name: John Doe
port: 8080
# β Wrong - missing space after colon
name:John DoeMulti-line Strings
YAML offers two block scalar styles for multi-line strings:
# | (literal block) - preserves newlines
description: |
This is line one.
This is line two.
Each newline is preserved.
# > (folded block) - folds newlines to spaces
description: >
This is a long paragraph
that will be joined into
a single line.YAML Data Types
| Type | Syntax | Example |
|---|---|---|
| String | plain, "quoted", 'quoted' | name: John |
| Integer | 42, 0xFF, 0o77 | port: 8080 |
| Float | 3.14, .inf, .nan | pi: 3.14159 |
| Boolean | true/false | debug: true |
| Null | null, ~, (empty) | value: null |
| Date | YYYY-MM-DD | date: 2026-02-10 |
The "Norway Problem"
In YAML 1.1, `NO` (as in the country code for Norway) is interpreted as boolean `false`. This is because YAML 1.1 treats yes/no, on/off, true/false as booleans. Always quote country codes and similar values:
# β YAML 1.1 interprets these as booleans!
country: NO # β false
answer: yes # β true
switch: on # β true
# β
Fix: quote the values
country: "NO" # β string "NO"
answer: "yes" # β string "yes"
switch: "on" # β string "on"Collections: Lists and Maps
# List (sequence)
fruits:
- apple
- banana
- cherry
# Nested map
database:
host: localhost
port: 5432
credentials:
user: admin
password: secret
# Inline / Flow style
fruits: [apple, banana, cherry]
point: {x: 10, y: 20}Top 10 Common YAML Errors
| # | Error | Cause | Fix |
|---|---|---|---|
| 1 | TabCharacterError | Using tab characters for indentation | Replace all tabs with spaces (2 or 4) |
| 2 | Inconsistent indentation | Mixing 2-space and 4-space indentation | Use consistent indentation throughout the file |
| 3 | Missing space after colon | Writing `key:value` instead of `key: value` | Always add a space after the colon |
| 4 | Unquoted special characters | Values containing : or # without quotes | Quote strings containing special characters |
| 5 | Boolean coercion | yes/no/on/off/NO interpreted as boolean | Quote values that look like booleans: `"yes"`, `"NO"` |
| 6 | Strings with colons | `url: http://example.com` breaks at second colon | Quote the entire value: `url: "http://example.com"` |
| 7 | Duplicate keys | Same key appears twice in a mapping | Remove duplicate keys (last one wins but this is error-prone) |
| 8 | Trailing whitespace | Invisible spaces after values | Configure editor to trim trailing whitespace |
| 9 | Wrong list indentation | List items not aligned with parent key | Align `-` with child indentation level |
| 10 | Missing document separator | Multiple documents without `---` separator | Add `---` between YAML documents |
YAML vs JSON vs TOML
| Feature | YAML | JSON | TOML |
|---|---|---|---|
| Comments | # support | None | # support |
| Multi-line strings | | and > | \\n only | """ triple quotes |
| Data types | Rich (dates, etc.) | Basic (6 types) | Rich (dates, etc.) |
| Readability | High | Medium | High |
| Machine parsing | Complex | Simple | Medium |
| Best for | Config files | APIs, data exchange | App config |
Validation Best Practices
- Always use a YAML linter in your CI/CD pipeline
- Configure your editor to show whitespace characters
- Use 2-space indentation (most common convention)
- Quote strings that could be misinterpreted (booleans, numbers with leading zeros)
- Use YAML schema validation for complex configurations
- Keep YAML files small and modular when possible
FAQ
Can I use tabs in YAML?
No. YAML strictly forbids tab characters for indentation. You must use spaces. Most editors can be configured to insert spaces when you press the Tab key.
What's the difference between single and double quotes in YAML?
Single quotes preserve literal strings (no escape sequences). Double quotes allow escape sequences like \n, \t, and Unicode escapes \uXXXX. Use single quotes for simple strings and double quotes when you need escape characters.
How do I represent null in YAML?
You can use `null`, `~`, or simply leave the value empty. All three are equivalent: `key: null`, `key: ~`, and `key:` (with nothing after colon+space).
Why does YAML treat "NO" as false?
YAML 1.1 specification treats yes/no, on/off, true/false (case-insensitive) as boolean values. YAML 1.2 fixed this to only recognize true/false. To be safe, always quote strings that could be interpreted as booleans.