YAML error: did not find expected key (or could not find expected ':')

Inside a block mapping, every line at the mapping’s indentation must be a key: value pair. “Did not find expected key” means the parser found something else at that level, usually a line indented by a different number of spaces than its siblings. “Could not find expected ‘:’” is the same family: a line looks like the start of a key but never reaches its colon. The reported line is where the parser gave up; the mistake is often one or two lines earlier.

Seen as:

  • yaml: line 4: did not find expected key
  • error converting YAML to JSON: yaml: line 4: did not find expected key
  • (<unknown>): did not find expected key while parsing a block mapping at line 2 column 3
  • yaml.scanner.ScannerError: while scanning a simple key could not find expected ':'
  • yaml: line 2: could not find expected ':'

Input

Settings

History

Load from URL

Common causes

1. A sibling key indented by the wrong number of spaces

Here ports is indented by three spaces while image uses four. YAML cannot tell which mapping it belongs to. Align it exactly with its siblings.

Before
web:
    image: nginx:1.27
   ports:
      - "80:80"
After
web:
    image: nginx:1.27
    ports:
      - "80:80"

2. Template output with the wrong indentation

In Helm, {{ toYaml .Values.resources }} keeps the indentation of the template line only for its first line. Use nindent (or indent) so every rendered line lands at the right level.

Before
resources:
    limits:
      cpu: 500m
  requests:
      cpu: 250m
After
resources:
  limits:
    cpu: 500m
  requests:
    cpu: 250m

3. A missing colon after a key

Without the colon, image nginx is a plain scalar, and the next line makes it look like a multi-line key. This is what PyYAML and Go report as “could not find expected ‘:’”.

Before
image nginx:1.27
ports:
  - "80:80"
After
image: nginx:1.27
ports:
  - "80:80"

4. An unclosed quote earlier in the file

A missing closing quote makes the string swallow the following lines, and the parser fails later where the structure no longer makes sense. Look for an odd number of quotes above the reported line.

Before
name: "web
replicas: 2
After
name: "web"
replicas: 2

Frequently asked questions

Why does the error line not match where my mistake is?

Parsers report the point where the structure became impossible, which can be after the actual mistake. Check the reported line, the line above it, and any quotes that might still be open.

How many spaces should I indent YAML with?

Any consistent number works, but every key in the same mapping must start in the same column. Two spaces is the most common convention, and PasteKit can re-indent a file for you once it parses.

Why does the line number differ between tools?

Go, PyYAML and the yaml JavaScript library detect the problem at slightly different tokens. They all describe the same broken indentation, so fix the structure rather than chasing one tool’s line number.

Related