YAML error: did not find expected '-' indicator

A block sequence (a YAML list) is a run of lines that each start with - at the same indentation. The parser was inside such a list and found a line that was neither a new - item nor correctly indented content of the current item. Usually a property of a list item is indented one space too few, or a key that belongs elsewhere sits at the dash column.

Seen as:

  • yaml: line 4: did not find expected '-' indicator
  • error converting YAML to JSON: yaml: line 4: did not find expected '-' indicator
  • (<unknown>): did not find expected '-' indicator while parsing a block collection at line 2 column 3
  • yaml.parser.ParserError: while parsing a block collection expected <block end>, but found '<block mapping start>'
  • YAMLException: bad indentation of a sequence entry (5:4)

Input

Settings

History

Load from URL

Common causes

1. An item property indented one space short

The properties of a list item must line up with the first key after the dash. Here value starts one column left of name, so it belongs to neither the item nor the list.

Before
- name: REGION
 value: "eu-west-1"
After
- name: REGION
  value: "eu-west-1"

2. A key placed at the dash column inside a list

In a GitHub Actions or CI file, writing a key at the same indentation as the dashes ends the list as far as you are concerned, but not for the parser. Move it out to its parent’s level or make it an item.

Before
steps:
  - name: Test
    run: npm test
  name: Deploy
After
name: Deploy
steps:
  - name: Test
    run: npm test

3. A new item written without its dash

Adding an item by copying the properties of the previous one but forgetting - leaves a mapping inside the list. Every item needs its own dash.

Before
ports:
  - containerPort: 80
  containerPort: 443
After
ports:
  - containerPort: 80
  - containerPort: 443

4. A nested block aligned with the dash instead of the item

Kubernetes manifests hit this when resources: or env: is pasted two spaces too far left, or a Helm nindent value is too small. The block belongs inside the list item, so it must line up with name.

Before
containers:
  - name: web
    image: nginx:1.27
  resources:
    limits:
      cpu: "1"
After
containers:
  - name: web
    image: nginx:1.27
    resources:
      limits:
        cpu: "1"

Go's line number is not the line to edit

This error most often comes from Go tools, and their line numbers need translating. For the sample on this page the mistake is on line 5:

  • yaml.v2 and sigs.k8s.io/yaml (the form seen as error converting YAML to JSON) report line 4. The number is zero-based here, so add one.
  • yaml.v3 reports line 1. That is the context mark, the start of the list, counted from zero, so it tells you which list and not which line.
  • Ruby’s Psych prints the start of the list as well: while parsing a block collection at line 2 column 3.
  • PyYAML gives both, an in "<unicode string>", line 2 for the list and line 5, column 4 for the problem. Use the second one.

The message also changes with the shape of the mistake. In PyYAML, a badly indented property reads expected <block end>, but found '<block mapping start>', and a key sitting at the dash column reads but found '?'. In js-yaml the two cases are bad indentation of a sequence entry and bad indentation of a mapping entry. The yaml package says Sequence item without - indicator and All mapping items must start at the same column.

The three legal positions for the next line

Inside a list, each new line has only three valid starting columns:

env:
  - name: REGION       # dash in column 3, first key in column 5
    value: eu-west-1   # column 5: same item
  - name: LOG_LEVEL    # column 3 with a dash: new item
other: value           # column 1: the list has ended

Column 4 fits none of them, and neither does column 3 without a dash, so both raise this error. Work upwards from the reported line to the most recent dash, put a cursor on the first key after it, and check the failing line starts in that column.

The opposite mistake is worse because it raises no error. A line indented more than the dash, with its own dash, is read as a continuation of the previous plain string:

items:
  - one
   - two

That parses as a single item, the string one - two. A file with no errors can still have the wrong shape, so check the structure as well. In PasteKit, the Tree view shows which keys landed in which item once the file parses.

Catching it before deployment

For hand-edited files, turn on visible whitespace and a two-space indent for YAML in your editor, since most of these mistakes are one column wide.

  • yamllint reports the failure as a syntax error with the real line and column, and its indentation rule can enforce how sequences are indented across a repository.
  • helm template ./chart > out.yaml shows the rendered output, so you debug the result and not the template. The reported line refers to the rendered file, not to your chart source.
  • kubectl apply --dry-run=client -f out.yaml runs the manifest through kubectl’s own parsing without changing the cluster.

Wrong indent and nindent values in Helm charts are a frequent source of this error. Counting the columns in the rendered output is quicker than counting braces in the template.

Frequently asked questions

Do list dashes need to be indented under their key?

No. Both “key:\n- item” and “key:\n - item” are valid. What matters is that every dash in the same list sits in the same column and that each item’s properties line up after the dash.

What does PyYAML's "expected <block end>, but found '?'" mean?

It is PyYAML’s wording for the same problem: inside a block collection it expected the list to continue or end and found a mapping key (‘?’) instead.

Why is the reported line one above the mistake?

Tools built on sigs.k8s.io/yaml print a zero-based line number for this parser error, so the mistake is one line below the number shown. With Helm, the number refers to the rendered output and not to your template.

How can I see the structure the parser is building?

Fix the first error PasteKit reports, then open the Tree view. It shows which keys belong to which list item, which quickly reveals an item that swallowed a sibling’s property.

Related