Skip to content

YAML error

“could not find expected ':'”

A line that sits where a key belongs has no colon on it. Usually the colon is missing or has no space after it, or a long value wrapped onto a new line without quotes. Add the colon, or quote or block-format the value.

By yamltojsonfree · Published · Updated

What the parser is telling you

When a line starts at the indentation of a mapping, the scanner assumes it is reading a key and looks for : on the same line, because a key must fit on one line. If the line ends without one, the scanner waits, and when the next line starts it reports that the key it began scanning never got its colon. The “while scanning a simple key” location is the line that is wrong; the second location is where the parser noticed.

The same mistake on the very first line of a file produces a different message, “mapping values are not allowed in this context”, because no mapping has been opened yet. Deeper in the file it produces this one.

Who prints this message

PyYAML, libyaml and go-yaml all use this wording, so it appears from Python, Ruby, kubectl, Helm and Docker Compose. The full message starts with “while scanning a simple key”.

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 missing colon

The line image nginx has no colon, so it cannot be a key. The parser reports the following line, replicas: 3, as the place it gave up.

Broken

name: api
image nginx
replicas: 3

Fixed

name: api
image: nginx
replicas: 3

Fix: Also check for a colon with no space after it: image:nginx is one plain word to YAML, not a key and a value.

A long value wrapped onto the next line

The second line starts at the key column, so YAML reads it as a new key rather than a continuation, and it has no colon.

Broken

description: Restarts the worker when the
queue backs up
owner: platform

Fixed

description: >
  Restarts the worker when the
  queue backs up
owner: platform

Fix: Use > for folded text or | to keep line breaks, and indent the continuation lines. Quoting the whole value also works when the continuation is indented.

A missing colon inside a list item

The same rule applies inside a sequence entry: every line at the item’s indentation is a key and needs its colon.

Broken

containers:
  - name: api
    image nginx
    ports: [80]

Fixed

containers:
  - name: api
    image: nginx
    ports: [80]

Fix: The reported line is ports, one below the mistake.

Find it fast

  1. 1.Read the first location in the full message, after “while scanning a simple key”. That is the line without a colon.
  2. 2.If the line is meant to be a value rather than a key, indent it under its key or switch the value to > or | block style.
  3. 3.Paste the document into the YAML validator on this site; it reports “A key spans more than one line” with the line marked.

The same error in other parsers

ParserMessage
PyYAML, libyaml, go-yaml (kubectl, Helm, Compose), Ruby Psychcould not find expected ':'
js-yaml (Node, most browser tools)end of the stream or a document separator is expected
yaml (npm)Implicit keys need to be on a single line
This site’s validatorA key spans more than one line.

Frequently asked questions

The line it reports has a colon. What is wrong?

The reported line is where the parser noticed, one below the real problem. The line above it is the one without a colon, or with a colon that is not followed by a space. The full PyYAML and libyaml message prints both locations; the first one, after “while scanning a simple key”, is the mistake.

Can a value span several lines without quotes?

Yes, if the continuation lines are indented further than the key. A plain value that continues at the key’s own column is read as a new key. For anything longer than a line, use a block scalar: > folds the lines into one paragraph, | keeps the line breaks.

Why does Helm report this on a line that does not exist in my template?

Helm reports lines in the rendered output, not the template. A template expression that renders to an empty string, or an include without nindent, can leave a line without a colon in the output. Run helm template with --debug to see the rendered YAML and count lines there.

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.