All articles

Common YAML errors and how to fix them

· 3 min read

YAML's whitespace-based syntax is pleasant to read and easy to break. The good news is that the same few mistakes cause most errors. The examples below use the error messages from the popular yaml JavaScript library; other parsers word them differently but point to the same problems.

Tabs used for indentation

server:
	port: 8080
Tabs are not allowed as indentation at line 2, column 1

YAML forbids tabs for indentation. Many editors insert them silently. Configure your editor to insert spaces for .yml and .yaml files (most do this with an .editorconfig):

[*.{yml,yaml}]
indent_style = space
indent_size = 2

Inconsistent indentation

server:
  port: 8080
   host: localhost
Nested mappings are not allowed in compact mappings at line 2, column 9

host is indented one space more than port, so the parser reads it as a continuation of port's value. Siblings must line up exactly. The same happens with list items:

items:
  - a
  b: 1
All mapping items must start at the same column at line 3, column 1

When the misaligned line is a - item, the message says All sequence items must start at the same column instead. Tools that still bundle version 1 of the yaml package word both as YAMLSyntaxError: All collection items must start at the same column. In every case, find the line it names and line it up with its siblings.

A colon inside a plain value

title: Error: not found
Nested mappings are not allowed in compact mappings at line 1, column 8

A colon followed by a space starts a key-value pair, so YAML sees Error as a nested key. Quote the value:

title: "Error: not found"

The same applies to #, which starts a comment: color: #fff gives color a value of null, because everything from # is a comment. Write color: "#fff".

The opposite: a missing space

key:value

This doesn't error. Without a space after the colon, the whole line is a plain string "key:value", not a key with a value. The parser accepts it, and your program gets the wrong structure, which is why checking the parsed output matters.

Duplicate keys

name: api
replicas: 2
name: worker
Map keys must be unique at line 3, column 1

The YAML spec requires unique keys, but some parsers (including PyYAML) silently keep the last value. In long files, such as Kubernetes manifests or Compose files, a duplicate key often means a block was pasted twice or mis-indented.

Unclosed quotes and brackets

msg: "hello
Missing closing "quote at line 2, column 1
tags: [a, b
Flow sequence in block collection must be sufficiently indented and end with a ] at line 2, column 1

The error points to the line after the problem because the parser keeps looking for the closing character.

Values starting with special characters

Some characters can't start an unquoted value:

user: @admin
cmd: `ls -la`
format: %d items
Plain value cannot start with reserved character @
Plain value cannot start with reserved character `
Plain value cannot start with directive indicator character %

Quote them. Values starting with * or & are read as aliases and anchors, ! starts a tag, and { or [ start a flow collection, so those need quotes too.

A list where a key was expected

steps:
- run: npm ci
  - run: npm test
A block sequence may not be used as an implicit map key at line 3, column 1

The second item is indented under the first item's mapping. List items at the same level must start in the same column:

steps:
  - run: npm ci
  - run: npm test

Valid YAML, wrong values

The trickiest problems don't produce errors. YAML guesses types for unquoted values:

version: 1.10       # becomes the number 1.1
zip: 01234          # becomes 1234
country: NO         # false in YAML 1.1 parsers
time: 12:30         # 750 in YAML 1.1 parsers (base 60)

Quote anything that must stay a string. The YAML vs JSON article has the full table of how 1.1 and 1.2 parsers treat these.

Multi-line strings

script: |
  npm ci
  npm test
description: >
  This long sentence is folded
  into a single line.

| keeps line breaks; > replaces them with spaces. Add - (|-) to remove the final newline. The text must be indented more than the key.

Tools that prevent these errors

  • An editor with YAML support (VS Code's YAML extension, JetBrains IDEs) underlines errors as you type and validates against JSON schemas for Kubernetes, GitHub Actions, Compose and more.
  • yamllint checks style and catches duplicate keys, tabs and trailing spaces in CI.
  • Schema validation catches wrong types and misspelled keys that parse fine; see JSON Schema validation.

For cron schedules inside YAML (GitHub Actions, Kubernetes CronJobs), always quote the expression: cron: "*/5 * * * *". An unquoted value starting with * is read as an alias. The cron cheat sheet covers the expression itself.

Frequently asked questions

How many spaces should YAML use for indentation?

Any number, as long as siblings use the same amount. Two spaces is the common convention in Kubernetes, GitHub Actions and Docker Compose files.

Do list items need to be indented under their key?

No. Both key:\n- item and key:\n - item are valid. Pick one style per file; yamllint can enforce it.

Why does my error point to a line after the real mistake?

The parser often notices a problem only when the next line doesn't fit, for example after an unclosed quote or bracket. Check the line above the reported one.

How do I write a multi-line string?

Use | to keep line breaks or > to fold them into spaces, then indent the text below the key.

Further reading