YAML error: found character that cannot start any token

Some characters are reserved by YAML and cannot begin an unquoted (plain) value: @ and the backtick are reserved for future use, % starts a directive, and a tab cannot be used as indentation. When a value begins with one of them, the scanner cannot decide what kind of token it is looking at. Quoting the value fixes it.

Seen as:

  • yaml.scanner.ScannerError: while scanning for the next token found character '@' that cannot start any token
  • found character '`' that cannot start any token
  • yaml: line 2: found character that cannot start any token
  • Plain value cannot start with reserved character @ at line 2, column 11
  • YAMLException: bad indentation of a mapping entry (2:11)

Input

Settings

History

Load from URL

Common causes

1. A value that starts with @

Mentions, npm scopes and decorators often start with @. Wrap the value in quotes; an @ in the middle of a value, as in an email address, is fine.

Before
notify: @oncall-team
package: @acme/ui
After
notify: "@oncall-team"
package: "@acme/ui"

2. A key that starts with @

Keys follow the same rule as values. JSON-LD properties and some API schemas use names such as @id and @type, which are fine in JSON because every key is quoted there. Pasting them into YAML unquoted produces this error at the first column.

Before
@id: 42
@type: Person
After
"@id": 42
"@type": Person

3. A shell command in backticks

Backticks are reserved in YAML. Quote the whole command, or use a block scalar for longer scripts. Single quotes are the safest choice here, because they leave $ and backslashes alone.

Before
command: `date +%F`
After
command: '`date +%F`'

4. A value that starts with %

A leading % looks like a directive such as %YAML 1.2. URL-encoded strings and format strings need quotes. A % later in the value, as in 100%, causes no problem.

Before
separator: %20
log_format: %h %l %u
After
separator: "%20"
log_format: "%h %l %u"

5. A tab used as indentation

In PyYAML and Go the same message appears for a tab at the start of a line, shown as '\t'. Replace it with spaces; see tabs in YAML.

Before
deploy:
	notify: oncall
After
deploy:
  notify: oncall

Other characters give other errors

In practice only @, the backtick, % and an indenting tab produce this exact message. Other reserved characters at the start of a value mean something to YAML, so the parser tries to act on them and fails differently. PyYAML reports:

  • a: *foo gives found undefined alias 'foo', because * refers to an anchor.
  • a: & gives while scanning an anchor, because & defines one.
  • a: !foo bar gives could not determine a constructor for the tag '!foo'.
  • version: >=1.0 gives while scanning a block scalar and expected chomping or indentation indicators, but found '=', because > starts a folded string.
  • a: ,b gives expected the node content, but found ','.

The fix is the same in every case: quote the value. Version constraints such as >=1.0 and glob patterns such as *.log are the ones people hit most.

How each parser words it

The same input does not always read the same way, and two parsers give you no clue about which character is at fault:

  • PyYAML, pure Python loader: names the character and prints the line with a caret.
  • PyYAML with CSafeLoader: found character that cannot start any token, without the character, because it comes from libyaml.
  • Go yaml.v2 and yaml.v3: yaml: line 2: found character that cannot start any token. If the problem is on line 1, Go leaves out the line number altogether.
  • js-yaml: bad indentation of a mapping entry (2:11). The indentation is fine, so ignore the wording and go to that column.
  • yaml (npm): Plain value cannot start with reserved character @, or directive indicator character % for a percent sign.

For tabs, js-yaml and the yaml package are more direct: tab characters must not be used in indentation and Tabs are not allowed as indentation.

Finding every occurrence and preventing new ones

A parser stops at the first problem, so fixing a long file one error at a time is slow. These searches list the likely lines in one go:

# Values starting with @, a backtick or %
grep -nP '^\s*(- )?[^#\s]+:\s+[@`%]' file.yaml

# Tabs in the indentation
grep -nP '^\s*\t' file.yaml

PasteKit reports each @ and backtick problem it finds in one pass, with the column.

When you pick quotes, remember that double quotes process escapes such as \n, while single quotes keep everything literal except '', which stands for one apostrophe.

Generated YAML should come from a serialiser, which adds quotes where they are needed. yaml.safe_dump({"notify": "@oncall-team"}) writes notify: '@oncall-team'. In Helm templates, pipe user values through quote, as in {{ .Values.notify | quote }}, because a value that starts with @ otherwise breaks the rendered file. In CI, yamllint reports this as a syntax error with the line and column.

Frequently asked questions

Which characters cannot start a plain YAML value?

The indicators - ? : , [ ] { } # & * ! | > ’ " % @ and the backtick all have special meaning at the start of a value. A few, such as - ? and :, are allowed when followed by a non-space character, but quoting is the simple rule.

Why does Go not say which character it found?

Go’s yaml packages print the generic libyaml message without the character. Use the line number, or paste the file into PasteKit, which names the character and its column.

Why does js-yaml talk about indentation when mine is correct?

js-yaml cannot read a value at that position, so it ends the mapping entry and complains about what follows. The reported line and column are still right, and they point at the reserved character.

Will quoting change the value?

No. “@oncall-team” and an unquoted @oncall-team (if it were allowed) are the same string. Quotes only tell the parser where the string starts and ends.

Related