Guide

YAML gotchas: the Norway problem, indentation and multi-line strings

Published · 7 minute read

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 wroteA YAML 1.1 parser may readWhy
version: 1.10the number 1.1It is a float; the trailing zero is lost
zip: 01234668A leading zero means octal
time: 22:301350Base-60 notation for numbers
id: 1e31000.0Scientific notation
release: 2026-03-14a date objectLooks like an ISO date
value: null or ~nullNot the string "null"
key: (nothing after it)nullAn 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

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:

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.

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

  1. Quote every string that is not obviously a word: versions, IDs, codes, dates, anything with punctuation.
  2. Use spaces, two per level, and let an editor show whitespace.
  3. Choose | for scripts and > for prose, and add - when a trailing newline would matter.
  4. Validate with a schema where the tool offers one, so a mistyped key is an error rather than a silently ignored setting.
  5. When a file behaves strangely, convert it to JSON and read what the parser actually understood.

Tools used in this guide

More guides