Lunchbox Hands

yaml

YAML Error Messages Decoded: One Mistake, Five Parsers' Wording

Mapping values not allowed here, did not find expected key, bad indentation of a mapping entry: which parser says each YAML error, what causes it, and the fix.

By ·

mapping values are not allowed here, mapping values are not allowed in this context, bad indentation of a mapping entry and Nested mappings are not allowed in compact mappings are four messages for the same file: title: Note: read this. Every YAML library words the same mistake its own way, so searching for the exact message often turns up pages about a different parser, and sometimes a different problem. This post maps each message to the parser that prints it, the cause behind it, and the fix.

Everything below comes from running 43 inputs, most of them broken, through PyYAML 6.0.3 twice (once with the pure-Python loader, once with the libyaml C loader), Ruby’s Psych 5.3.1 (Ruby 4.0.9, libyaml 0.2.5), js-yaml 4.1.1 and 4.3.2 (identical on every input), js-yaml 5.5.0, and the yaml npm package 2.8.2, on Node 22.18. Go isn’t installed on the machine we tested on, so the Go messages are quoted from the go-yaml source (v3 scannerc.go, parserc.go, and the v4 error test data), not run.

Find your message

Message (as printed)Who prints itUsual cause
mapping values are not allowed herePyYAML, pure-Python loader: inside an unquoted value, or a line indented too far
mapping values are not allowed in this contextlibyaml: PyYAML C loader, Ruby, Gosame as above
Nested mappings are not allowed in compact mappingsyaml (npm)same as above
bad indentation of a mapping entryjs-yaml: in a value, wrong indentation, or a value starting with @, ` or %, or text after a closing quote
found character '\t' that cannot start any token (or '@', '`', '%')PyYAML, pure-Pythona tab, or a value starting with @, ` or %
found character that cannot start any tokenlibyaml, Gosame, but the character isn’t named
found a tab character where an indentation space is expectedlibyaml, Gotab-indented line inside a | or > block
found a tab character that violates indentationlibyaml, Gotab-indented line after a plain value
tab characters must not be used in indentationjs-yamlany tab indentation
Tabs are not allowed as indentationyaml (npm)any tab indentation
did not find expected keylibyaml, Goa key indented less than its siblings, a - item under a mapping, or text after a quoted or {{ }} value
expected <block end>, but found ...PyYAML, pure-Pythonthe pure-Python wording of did not find expected key
did not find expected '-' indicatorlibyamla line inside a list that doesn’t line up with the - items
could not find expected ':'PyYAML (both), Ruby, Goa line with no colon in the middle of a mapping
All mapping items must start at the same columnyaml (npm)a key indented less than its siblings
Implicit keys need to be on a single lineyaml (npm)a line with no colon, or a - item under a mapping
found unexpected end of streamPyYAML, Rubyunclosed quote
expected ',' or ']', but got ':' / did not find expected ',' or ']'PyYAML pure / libyamlunclosed [
missed comma between flow collection entriesjs-yaml 4unclosed [ or {
found unhashable keyPyYAMLan unquoted {{ jinja }} value
expected a single document in the streamPyYAML, js-yamla --- separator passed to a single-document loader

Two quick tells. If the message names the character (found character '\t'), it came from PyYAML’s pure-Python loader; libyaml and Go leave it out. If it ends in (3:11), it’s js-yaml.

Which parser is your tool using?

These are the parsers behind tools that people paste errors from. Each one is checked against the project’s own source or dependency manifest:

  • Home Assistant pins PyYAML==6.0.3 in its pyproject.toml. Its loader, annotatedyaml, tries the C loader first. If that fails, it re-parses with the Python loader, “which has more readable exceptions”. So Home Assistant shows the pure-Python wording: mapping values are not allowed here.
  • Ansible requires PyYAML (requirements.txt) and uses libyaml’s C parser when it’s installed. ansible --version tells you which one you have: pyyaml version = … (with libyaml v…) or (without libyaml).
  • docker-compose v1 (the Python one) calls yaml.safe_load (config.py), which is PyYAML’s pure-Python loader.
  • GitLab CI loads .gitlab-ci.yml with Ruby’s YAML.safe_load and passes Psych’s message through (yaml.rb). Ruby’s Psych is built on libyaml.
  • Kubernetes uses sigs.k8s.io/yaml, which imports go.yaml.in/yaml/v2 and wraps failures as error converting YAML to JSON: … (yaml.go). Helm’s go.mod lists both sigs.k8s.io/yaml and go.yaml.in/yaml/v3. Docker Compose v2 reads files through compose-go, which uses go.yaml.in/yaml/v4 (go.mod). actionlint also uses v4. All of these are go-yaml, a Go port of libyaml, and every message we checked in its source matches libyaml’s word for word. Go formats them as yaml: line 4: could not find expected ':', with a line number and no column.
  • ESLint’s .eslintrc.yaml loader, @eslint/eslintrc, depends on js-yaml 4.
  • Prettier formats YAML with the yaml package, through yaml-unist-parser. GitHub’s open-source workflow parser in actions/languageservices (the code behind the Actions language server for editors) also uses yaml. We couldn’t find out which parser github.com itself runs on workflow files, so we don’t make a claim about it.

The “in this context” wording that Kubernetes, Helm and Compose users search for isn’t Go-specific. libyaml uses it too. Ansible with libyaml, GitLab and Ruby all print it.

Colon-space inside a value

title: Note: read this
PyYAML (pure)   mapping values are not allowed here                     line 1, column 12
libyaml / Ruby  mapping values are not allowed in this context          line 1, column 12
js-yaml 4       bad indentation of a mapping entry                      (1:12)
yaml (npm)      Nested mappings are not allowed in compact mappings     line 1, column 8

A colon followed by a space separates a key from its value, so the parser reads Note as a second key on a line that already has one. The fix is to quote the whole value: title: "Note: read this". A colon with no space after it (http://x, 10:30) is fine. The YAML 1.2 spec explains that the : “can be used inside plain scalars, as long as it is not followed by white space”. Once the value parses, the type traps take over (NO, 0755, 1:30). Those are covered in the YAML Norway problem.

The same messages appear when a line is indented too far:

items:
  - name: a
     value: 1

PyYAML and libyaml report mapping values at line 3, column 11, and js-yaml says bad indentation of a mapping entry (3:11). The yaml package flags line 2 instead, because it reads value as a continuation of a. The fix is to line value up with name.

Tabs

server:
	port: 80
PyYAML (pure)   found character '\t' that cannot start any token        line 2, column 1
libyaml / Ruby  found character that cannot start any token             line 2, column 1
js-yaml 4       tab characters must not be used in indentation          (2:1)
yaml (npm)      Tabs are not allowed as indentation                     line 2, column 1

The spec says “tab characters must not be used in indentation, since different systems treat tabs differently.” Inside a | block, libyaml has its own wording: found a tab character where an indentation space is expected. After a plain value it says found a tab character that violates indentation. Both strings are also in go-yaml’s scanner. The fix is to convert the tabs to spaces.

Tabs that aren’t indentation are where the parsers split:

InputPyYAML purelibyaml / Rubyjs-yaml 4yaml (npm)
port:<tab>80error{port: 80}{port: 80}{port: 80}
-<tab>one (list item)errorerror["one"]["one"]

The 1.2 spec says separation white space between tokens “may safely include tab characters”, and its Example 6.3 has a - followed by a tab. libyaml is stricter after -, and pure-Python PyYAML is stricter still. So a file that passes one linter can fail in Ansible.

A key or list item at the wrong level

server:
    host: a
  port: 80
PyYAML (pure)   while parsing a block mapping … expected <block end>, but found '<block mapping start>'   line 3, column 3
libyaml (C)     while parsing a block mapping … did not find expected key                                line 3, column 3
Ruby Psych      did not find expected key while parsing a block mapping                                  at line 1 column 1
js-yaml 4       bad indentation of a mapping entry                                                       (3:3)
yaml (npm)      All mapping items must start at the same column                                          line 3, column 1

Look at Ruby’s location. Psych reports where the enclosing mapping starts (line 1), not the line that broke it (line 3). GitLab passes Psych’s message through, so in a long .gitlab-ci.yml you may be told “line 1” about a problem far below it. go-yaml v3’s error formatter also prefers that context position whenever it isn’t on the first line (decode.go, func (p *parser) fail). With either one, check the block that starts at the reported line, not just that line.

The list version reads differently again:

items:
  - name: a
   value: 1

libyaml says did not find expected '-' indicator, js-yaml says bad indentation of a sequence entry (3:4), and yaml says Sequence item without - indicator. Mixing a list into a mapping (key: value then - item at the same column) gives did not find expected key from libyaml and expected <block end>, but found '-' from pure PyYAML. js-yaml says end of the stream or a document separator is expected when this happens at the top level of the file, and bad indentation of a mapping entry when it happens inside a nested mapping. In every case, the fix is to make siblings start in the same column.

A line with no colon

a: 1
just text
b: 2

Both PyYAML loaders and Ruby say could not find expected ':'. js-yaml says can not read a block mapping entry; a multiline key may not be an implicit key, and yaml says Implicit keys need to be on a single line. Ruby reports line 2, where the bad line starts. PyYAML shows line 2 as context but puts its final marker on line 3, the line after the culprit, because that’s where it gave up. go-yaml’s own test case does the same: c 2 on line 3 produces yaml: line 4: could not find expected ':'. So look one line up. The fix is to add the missing : or remove the stray line.

Values that start with @, ` or %

user: @admin
PyYAML (pure)   found character '@' that cannot start any token         line 1, column 7
libyaml / Ruby  found character that cannot start any token             line 1, column 7
js-yaml 4       bad indentation of a mapping entry                      (1:7)
yaml (npm)      Plain value cannot start with reserved character @      line 1, column 7

The spec says @ and ` “are reserved for future use” and can’t start an unquoted value. % marks a directive. cmd: `ls` and pct: %50 fail the same way, and yaml calls % a “directive indicator character”. js-yaml’s “bad indentation” has nothing to do with indentation here. The fix is to quote the value: "@admin".

Unclosed quotes and brackets

a: "hello
b: 2

PyYAML and Ruby say found unexpected end of stream and give the position of the opening quote (line 1, column 4). PyYAML also marks the end of the file. js-yaml (unexpected end of the stream within a double quoted scalar) and yaml (Missing closing "quote) point at the end of the file. In a long file, the opening quote can be hundreds of lines above the reported position. Search upward for an odd number of ".

For a: [1, 2 followed by b: 3, pure PyYAML says expected ',' or ']', but got ':', libyaml says did not find expected ',' or ']', js-yaml 4 says missed comma between flow collection entries, and yaml says Flow sequence in block collection must be sufficiently indented and end with a ]. { gives the same messages with }, except that yaml says Flow map instead of Flow sequence.

js-yaml 5.5.0 rewords all of these. Unclosed quotes and unclosed brackets both come out as deficient indentation (2:1). If a tool shows “deficient indentation” for a file whose indentation looks fine, check for an unclosed quote or bracket.

Unquoted {{ jinja }} and Helm templates

name: {{ app_name }}

PyYAML fails with while constructing a mapping … found unhashable key. In YAML, { opens a flow mapping, so this is a mapping whose key is another mapping. The spec allows that (“keys may be arbitrary nodes”), but a Python dict can’t use a dict as a key. The other parsers accept it without complaint and produce nonsense: Ruby gives a hash keyed by a hash, js-yaml 4 gives {"name": {"[object Object]": null}}, and yaml gives {"name": {"{ app_name }": null}}. js-yaml 5.5.0 does reject it, with object-based map does not support complex keys. Add a suffix ({{ app_name }}-svc) and it becomes a syntax error everywhere: libyaml says did not find expected key, and js-yaml says bad indentation of a mapping entry.

The fix is to quote any value that starts with {{: name: "{{ app_name }}". A template in the middle of a value (path: /srv/{{ app }}) is fine unquoted, because the value doesn’t start with {.

What parses without a word

These inputs give no error in at least one parser:

InputPyYAML (both)Ruby Psychjs-yaml 4yaml (npm)
Duplicate key a: 1 / a: 2{a: 2}, silently{a: 2}, silentlyduplicated mapping keyMap keys must be unique
a: 1, ---, b: 2 (single-document load)expected a single document in the streamreturns {a: 1}, drops the restexpected a single document in the stream, but found moreSource contains multiple documents
-a / -b under items: (no space after -)"-a -b""-a -b""-a -b""-a -b"
a:1 (no space after :)"a:1""a:1""a:1""a:1"

Go’s yaml.v3 rejects duplicates by default: uniqueKeys: true in decode.go, with mapping key "a" already defined at line 1. yaml.v2 only rejects them in strict mode. The two “no space” rows are valid YAML. They’re just not what you meant: -a is a string, not a list item. Ruby quietly dropping every document after the first is the nastiest row, since nothing tells you the rest of the file was ignored. A UTF-8 byte order mark at the start of the file, and Windows CRLF line endings, parsed cleanly in every parser we ran.

What our validator and converter show

Our YAML validator and YAML ⇄ JSON converter both parse with the yaml package 2.8.2 (YAML 1.2), so they show the yaml column above. The validator collects every error, not just the first. For each one it shows the message, the line and column, an excerpt of your source, and a plain-English hint for that error. For title: Note: read this, for example, the hint says to wrap the whole value in quotes. The converter shows the first error’s raw message with its location, and no hint. Both run in your browser, so nothing you paste is uploaded.

Running this post’s inputs through our own code turned up several hints that pointed the wrong way, which we fixed. How the validator handles the awkward cases now, including one it still misses:

  • Unclosed quote: the error’s line and column are still the end of the file, which is the same as js-yaml. The hint names where the quoted value opens: line 1, column 4 for the example above.
  • Unquoted {{ x }}: we report valid YAML with a key of "{ app_name }", plus a warning that the value is parsed as a nested mapping and that templating braces must be quoted, since PyYAML would reject the file.
  • Hints: the hint for Sequence item without - indicator says to indent the line to the column of the first key after the - to continue that item, or to put - in front of it to start a new one. For an unclosed [, the hint says the [ is probably never closed with ]. The % hint covers the directive indicator as well as @ and `.
  • Tabs after -: we accept them, as the spec does, so the validator won’t catch the -<tab> case that libyaml rejects.

A triage routine

  1. Identify the parser from the wording, using the first table. If it’s Ruby or Go, the reported line may be the start of the enclosing block.
  2. mapping values or Nested mappings: quote the value that contains : , or fix a line that’s indented too far.
  3. did not find expected key, All mapping items, bad indentation: line up the siblings, then check for text after a closing quote, a {{ at the start of a value, or a value starting with @, ` or %.
  4. could not find expected ':': look one line above the reported line.
  5. end of stream, Missing closing: search upward for the quote or bracket that was opened.
  6. It parses, but the data is wrong: check the silent table above, then the Norway problem for values that change type.

To see every problem in a file at once, paste it into the YAML validator. If your target is JSON, the YAML ⇄ JSON converter shows how a 1.2 parser reads it. For the same kind of decoder for JSON.parse messages, see JSON “Unexpected token” errors explained.