Skip to content

YAML error

“mapping values are not allowed in this context”

The parser hit a colon followed by a space in the middle of a value, where YAML only allows a colon after a key. Quote the value if the colon is part of it, or fix the line above if the parser was still reading it.

By yamltojsonfree · Published · Updated

What the parser is telling you

A colon followed by a space is how YAML separates a key from its value. When the scanner meets that pair while it is still reading a plain, unquoted value, it cannot start a second key inside a value, so it stops with this message. The column it reports is the offending colon.

The trap is that the colon it complains about is often not the mistake. A plain scalar can continue onto the next line, so if the line above is missing a colon, or the current line is indented deeper than it should be, the parser is still reading the previous value when it reaches your perfectly good key: value and reports it here.

Who prints this message

libyaml prints this, so it appears in everything built on it: kubectl and Helm through go-yaml, Docker Compose, Ruby and the GitLab CI linter through Psych, and Ansible or PyYAML when the C loader is installed. The pure-Python PyYAML loader prints the shorter “mapping values are not allowed here”; it is the same error.

The causes, with fixes

Broken YAML on the left, the corrected form on the right. The fix under each pair is the one that applies to that cause.

A colon inside an unquoted value

Any : in a value ends the value early. Text such as Deploy: production, Error: something or Note: see below all contain one.

Broken

title: Deploy: production
owner: platform team

Fixed

title: "Deploy: production"
owner: platform team

Fix: Quote the whole value. Colons not followed by a space are fine: http://host, 12:30 and C:\path parse without quotes.

A key indented deeper than the value above it

The line ports: 80 sits one space deeper than host, so the parser reads it as a continuation of localhost and then trips over its colon. libyaml reports the error on line 3 even though the mistake is the extra space.

Broken

server:
  host: localhost
   ports: 80

Fixed

server:
  host: localhost
  ports: 80

Fix: Sibling keys must share the same indentation. The same thing happens under a list item when a key is indented deeper than the one after the dash.

A missing colon on the first line

Without the colon, name api is a plain value that continues onto the next line, so the next line’s colon is a colon inside a value. When the missing colon is on a later line the message changes to “could not find expected ':'”.

Broken

name api
image: nginx

Fixed

name: api
image: nginx

Fix: The same applies to name:api with no space after the colon: YAML treats name:api as one word.

Find it fast

  1. 1.Go to the reported line and column. If the value there contains : , quote the value.
  2. 2.If the line looks correct, look at the line above it: a missing colon, no space after the colon, or one more space of indentation than its siblings.
  3. 3.Paste the document into the YAML validator on this site. It reports this error as “A value contains a colon, so YAML read it as a second key” with the line marked in the editor.

The same error in other parsers

ParserMessage
libyaml, go-yaml (kubectl, Helm, Compose), Ruby Psychmapping values are not allowed in this context
PyYAML, pure-Python loadermapping values are not allowed here
js-yaml (Node, most browser tools)bad indentation of a mapping entry
yaml (npm)Nested mappings are not allowed in compact mappings
SnakeYAML (Java)mapping values are not allowed here
This site’s validatorA value contains a colon, so YAML read it as a second key.

Frequently asked questions

Why does the error point at a line that looks correct?

Because the parser was still reading the value from the line above. A plain, unquoted YAML value can continue onto following lines, so a missing colon or an extra space of indentation on line 2 is only noticed when the colon on line 3 turns up inside that value. Check the line above the one reported.

Do I have to quote every value that contains a colon?

Only when the colon is followed by a space or ends the value. URLs such as http://example.com, times such as 12:30 and Windows paths such as C:\Users parse without quotes. Quoting them anyway is harmless and protects you from YAML 1.1 reading 12:30 as the number 750.

Is “mapping values are not allowed here” the same error?

Yes. It is the wording of the pure-Python PyYAML scanner; libyaml and its ports, including go-yaml behind kubectl and Helm, say “in this context”. The cause and the fix are identical.

Related errors

All six underlying mistakes, ordered by cause rather than by message, are in the guide Common YAML errors and how to fix them. Every one of them is caught with the exact line and a suggested fix by the free tools below; nothing is uploaded.