YAML gotchas: the Norway problem, indentation and multi-line strings
YAML is pleasant to read and unforgiving to write. Most of its surprises come from one design decision: unquoted values are guessed. A parser looks at the text and decides whether it is a string, a number, a boolean or a date, and its guess is not always yours.
The Norway problem
countries:
- GB
- FR
- NO
Under YAML 1.1, which many widely used parsers still implement, this is a list of two strings and the boolean false. The words yes, no, on, off, y and n, in any capitalisation, are booleans. So the country code for Norway disappears, a key called on: in a workflow file becomes the key true, and a setting such as answer: yes is not the string you typed.
YAML 1.2 reduced booleans to true and false only, but you cannot rely on every tool having moved. The fix works everywhere: quote strings that could be mistaken for something else.
countries: ["GB", "FR", "NO"]
Other values that are not what they look like
| You wrote | A YAML 1.1 parser may read | Why |
|---|---|---|
version: 1.10 | the number 1.1 | It is a float; the trailing zero is lost |
zip: 01234 | 668 | A leading zero means octal |
time: 22:30 | 1350 | Base-60 notation for numbers |
id: 1e3 | 1000.0 | Scientific notation |
release: 2026-03-14 | a date object | Looks like an ISO date |
value: null or ~ | null | Not the string "null" |
key: (nothing after it) | null | An empty value is null, not an empty string |
Version numbers, postal codes, phone numbers, account IDs, commit hashes that happen to be all digits: anything that is an identifier rather than a quantity should be quoted. A YAML formatter that shows the parsed result as JSON makes the guessed types visible at once.
Indentation rules
- Spaces only. A tab character used for indentation is an error. Set your editor to insert spaces in YAML files.
- Consistency within a block, not a fixed width. Children must be indented more than their parent, and siblings must line up exactly. Two spaces is the convention.
- List items may sit at the same level as their key. Both of these are valid and mean the same thing, which is why diffs between tools can look noisy:
steps: steps:
- build - build
- test - test
The error that causes the most confusion is a mapping inside a list item. The keys after the first must align with the first key, not with the dash:
- name: web
port: 80 ← aligned with "name"
- name: api
port: 8080 ← one space too many: an error
When a string needs quotes
Besides values that look like other types, a plain (unquoted) string breaks when it contains certain punctuation:
- A colon followed by a space starts a mapping:
title: Chapter 1: Startis an error. - A space followed by
#starts a comment, socolour: blue #2silently loses its ending. - A value that begins with
*,&,!,|,>,%,@, a backtick,[or{is read as YAML syntax. This is why glob patterns such as*.logmust be quoted.
The two quote styles behave differently. Single quotes are literal: nothing is an escape, and a single quote inside is written twice ('it''s'). Double quotes process backslash escapes such as \n and \t. For Windows paths and regular expressions, single quotes avoid a forest of doubled backslashes.
Multi-line strings: | and >
There are two block styles, and choosing the wrong one is a common source of broken scripts and odd-looking messages.
script: | message: >
echo one This is one long
echo two sentence.
|(literal) keeps every line break. Use it for scripts, certificates and anything where lines matter. The result on the left is two lines.>(folded) joins lines with spaces, like a paragraph. The result on the right is a single line. A blank line still produces a line break.
A suffix controls the line break at the very end: plain | keeps exactly one, |- removes it, and |+ keeps all trailing blank lines. If a value must not end with a newline, such as a token or a password, use |-.
Anchors and aliases
YAML can reuse a block by naming it with & and referring to it with *. With the merge key <<, a mapping can inherit another's keys and override some:
defaults: &defaults
retries: 3
timeout: 30
production:
<<: *defaults
timeout: 60
This removes repetition, but it is resolved by the parser, so converting to JSON expands everything. Not every tool supports the merge key, and it was never part of the core 1.2 specification. Use it sparingly, and expand a file to JSON when you need to see what a section finally contains.
Load it safely
YAML can carry tags that tell the parser to construct arbitrary objects of the host language. In several libraries the default loader historically honoured them, which means that loading an untrusted YAML file could run code. In Python, use yaml.safe_load, never plain yaml.load on input you do not control; other languages have the same distinction under different names. There is also a denial-of-service form: nested aliases that expand to an enormous structure from a few lines of text. Limit input size, and use the safe loader.
Habits that prevent most YAML bugs
- Quote every string that is not obviously a word: versions, IDs, codes, dates, anything with punctuation.
- Use spaces, two per level, and let an editor show whitespace.
- Choose
|for scripts and>for prose, and add-when a trailing newline would matter. - Validate with a schema where the tool offers one, so a mistyped key is an error rather than a silently ignored setting.
- When a file behaves strangely, convert it to JSON and read what the parser actually understood.