YAML is the configuration language of modern infrastructure. Kubernetes manifests, Docker Compose files, GitHub Actions and GitLab CI pipelines, Ansible playbooks and OpenAPI specifications are all written in it. It is pleasant to read, but it is also one of the few formats where invisible characters and a single space can change the meaning of a file or stop a deployment.
This guide covers the errors you are most likely to meet, explains YAML's surprising automatic typing, and shows how to validate and re-format a file so the whole team writes it the same way.
How YAML structure works
YAML expresses structure with indentation instead of brackets:
services:
web:
image: nginx:1.27
ports:
- "8080:80"
environment:
LOG_LEVEL: info
Three rules explain most of what happens:
- Indentation defines nesting. Keys at the same depth must line up exactly. The number of spaces is up to you, but it must be consistent within a block.
- A mapping entry is
key: valuewith a space after the colon. Without the space, it is not a key at all. - List items start with
-(dash and space), indented under their parent key.
YAML is a superset of JSON in practice, so {a: 1} and [1, 2] "flow style" also works, but most configuration files use the indented "block style".
The errors you will meet most often
"Tab characters must not be used in indentation"
The YAML specification forbids tabs for indentation. Many editors insert a tab when you press the Tab key, and it looks identical to spaces. Configure your editor to insert spaces in .yml and .yaml files, or convert existing tabs with a Tab to Spaces converter.
"Bad indentation of a mapping entry"
A key is indented by a different amount from its siblings:
services:
web:
image: nginx
ports:
- 80
ports has three spaces while image has four. The parser cannot tell whether ports belongs to web or services. Line the keys up. The error often points to the line after the real problem, so look one line up as well.
"Mapping values are not allowed here"
This is how several parsers, including PyYAML, report an unquoted value that contains : (a colon followed by a space), which YAML reads as the start of another mapping:
title: Note: prices exclude tax
js-yaml reports the same line as "bad indentation of a mapping entry", which is confusing because the indentation is fine. Quote the value: title: "Note: prices exclude tax".
A missing space after a colon
name:app is not a key with a value; it is the plain string name:app. On its own line it may even parse without an error, and then fail later when the next line turns out not to fit. Always write name: app.
"End of the stream or a document separator is expected"
The parser found content it cannot attach to the structure, very often because a previous line was misread (see the missing space above) or indentation drifted. Check the lines just before the reported position.
Implicit types: when values are not what you wrote
YAML guesses types for unquoted values, and the rules differ between YAML versions and parsers. The classic surprise is the "Norway problem":
country: NO
enabled: yes
version: 1.10
zip: 01234
- Under YAML 1.1 rules, still used by several popular parsers,
NOandyesare booleans, so the country code becomesfalse. Under YAML 1.2's core schema, used by js-yaml (and therefore by our formatter), both stay strings. Your file may be read differently by different tools in the same pipeline. 1.10is a number, so it becomes1.1. That is a real problem for version strings.01234loses its leading zero and becomes1234in js-yaml. Some YAML 1.1 parsers treat a leading zero as octal instead, which is even more confusing.
The fix is the same in every case: quote anything that is text, even if it looks like a number or a boolean. Version numbers, postcodes, country codes, phone numbers and IDs should be quoted strings.
Validate and format YAML with our tool
The YAML Formatter & Validator parses your YAML with js-yaml and writes it back out cleanly.
- Open the YAML Formatter & Validator and paste into YAML input, click Open file to load a
.yamlor.ymlfile, or use Load sample. - If the YAML is invalid, the error box shows the parser's reason plus the line and column. Fix it and the output appears.
- Pick an Indent of 2 spaces or 4 spaces, a Quote style (Single quotes or Double quotes), and optionally Sort keys A–Z or Wrap long strings at 80.
- Review the Formatted YAML, then Copy or Download it.
Files with several documents separated by ---, such as a multi-resource Kubernetes manifest, are formatted document by document and joined again with ---.
When writing output, the formatter quotes strings that other parsers might misread. For example, the string NO is written as 'NO', and the string 1.10 as '1.10', so the formatted file means the same thing everywhere.
What formatting changes
Because the YAML is parsed into data and serialised again, some things are not preserved:
- Comments are removed. They are not part of the data. Keep your original file if comments matter, and use the formatter to check validity or normalise indentation, then copy the fix back.
- Anchors and aliases are expanded.
<<: *defaultsbecomes a full copy of the referenced block, so the output can be longer but is equivalent. - Flow collections become block style.
[a, b]is written as a two-item list.
Other ways to validate YAML
- Editor support. Code editors with a YAML extension highlight syntax errors as you type, and many can check files against a schema, for example for Kubernetes or GitHub Actions, which catches wrong keys as well as bad syntax.
- yamllint. A Python command-line linter that checks syntax and style rules such as line length, indentation and trailing spaces. Good for CI.
- The consuming tool itself.
kubectl apply --dry-run=client -f file.yamlordocker compose configvalidate the file against what that tool actually expects.
Keep in mind that syntax validation is only step one. A file can be perfectly valid YAML and still use a key Kubernetes does not recognise.
Tips for keeping YAML healthy
- Use 2-space indentation unless your project has another convention, and set up an
.editorconfigso every editor agrees. - Quote strings liberally, especially values starting with special characters (
*,&,!,%,@, a backtick) or containing:or#. - For multi-line text, use a block scalar:
|keeps line breaks,>folds them into spaces. - To convert data for a program that needs JSON, use YAML to JSON; to go the other way, use JSON to YAML. Converting to JSON is also a quick way to see exactly how a parser interpreted each value.
Frequently asked questions
Can I use tabs in YAML?
Not for indentation. The specification forbids it, and parsers report an error. Tabs inside quoted strings are fine.
How many spaces should YAML indentation be?
Any consistent number works; 2 spaces is the most common convention in Kubernetes, Compose and CI files. What matters is that sibling keys line up exactly.
Why did my YAML value turn into true or false?
Unquoted words such as yes, no, on and off are booleans in YAML 1.1 parsers. Quote them if they are meant as text.
Does formatting YAML remove comments?
With a parse-and-rewrite formatter like this one, yes. Comments are not part of the data model. Keep your original file when comments are important.
Is JSON valid YAML?
In practice, yes: YAML 1.2 was designed so that JSON documents are valid YAML, so you can paste JSON into a YAML parser. The reverse is not true.