Ffile2fix
Sign in Get started

Fix YAML "mapping values are not allowed here"

yaml.scanner.ScannerError: mapping values are not allowed here
  in "config.yml", line 3, column 15

The parser found a 'key: value' colon where it did not expect a new mapping, usually inside a value. The most common cause is an unquoted value that contains a colon followed by a space; quoting the value or fixing the indentation on the reported line resolves it.

Also appears as: yaml: line 3: mapping values are not allowed in this context · (<unknown>): mapping values are not allowed in this context at line 3 column 15 · YAMLException: bad indentation of a mapping entry (3:15) · error converting YAML to JSON: yaml: line 12: mapping values are not allowed in this context

Common causes

  • An unquoted value containing ': ', such as title: Error: file missing
  • A child key indented under a key that already has a scalar value
  • A missing space after the colon on the line above (key:value), which turns it into a plain string
  • A list item written without its - dash, or a dash with wrong indentation
  • A Windows path or time string with a colon and space inside an unquoted value
  • Copying YAML from a web page that changed spaces or indentation

How to fix it

  1. Go to the reported line and column. The column usually points at the colon the parser rejected. Look at that line and the one directly above it, since the real mistake is often on the previous line.
  2. Quote values that contain colons. Change title: Error: file missing to title: "Error: file missing" or use single quotes. Inside double quotes escape \ and " ; inside single quotes double a single quote ('').
  3. Check the indentation. A key with a value on the same line cannot also have indented child keys. Either move the value into a child key or remove the extra indentation, and use spaces, never tabs.
  4. Add missing spaces and dashes. Make sure every key has a space after the colon (key: value) and every list item starts with '- '. For multi-line text use a block scalar with | or >.
  5. Validate before deploying. Run the file through a YAML parser, for example python3 -c "import yaml,sys; yaml.safe_load(open(sys.argv[1]))" config.yml, or the linter for your tool (docker compose config, kubectl apply --dry-run=client).

YAML

# wrong: unquoted ': ' inside the value
message: Error: file missing

# right
message: "Error: file missing"

# wrong: key has a value and children
server: web1
  port: 8080

# right
server:
  name: web1
  port: 8080

How to stop it happening again

  • Quote any string value that contains ': ' or starts with special characters like *, &, ! or @
  • Configure your editor to insert spaces for tabs and show indentation guides in YAML files
  • Run a YAML linter such as yamllint in CI
  • Use block scalars (| or >) for long or multi-line text

Frequently asked questions

Why does a URL with a colon work but my sentence does not?

YAML only treats a colon as a key separator when it is followed by a space or the end of a line. http://example.com has no space after the colon, while 'Note: text' does.

Why do Kubernetes and Docker Compose say 'in this context'?

They use the Go YAML library, which words the same error as 'mapping values are not allowed in this context'. The cause and fix are identical.

Do I have to quote every string in YAML?

No, but quoting is the safe choice for values with colons, #, leading special characters, or words like yes, no, on and off that some parsers read as booleans.