How to Check YAML Syntax
YAML's flexibility is also its trap: a file can parse cleanly yet hold the wrong data. Here are the errors that break YAML, the ones that silently change it, and how to catch both.
Two kinds of "wrong"
YAML problems come in two flavors. The first is a parse error — the document is malformed and won't load at all. The second, more insidious, is a silent type surprise — the document parses fine, but YAML's automatic type inference turned a value into something you didn't intend. A good check catches both, which is why "does it parse?" alone isn't enough for YAML.
Errors that break parsing
- Tabs for indentation. Forbidden by the spec. A single tab where spaces are expected is an immediate error, and it's invisible in most editors — the #1 cause of broken YAML.
- Inconsistent indentation. Siblings in the same block must align at the same column. Mixing 2 and 4 spaces, or under-indenting a nested key, fails to parse or nests under the wrong parent.
- Missing space after the colon. A mapping needs
key: value. Without the space,key:valueis read as one plain scalar string. - Unquoted special characters. A value containing a colon-space, a leading
@,{,[, or#often needs quoting.time: 12:30is ambiguous;time: "12:30"is safe. - Duplicate keys. The same key twice in one mapping is invalid YAML (many parsers accept it and silently keep the last — strict ones reject it).
Parsers report the first error with a line and column, then stop.
The silent one: the Norway problem
YAML infers types from bare values, and that's where data quietly changes. The classic example: a list of country codes.
countries:
- GB
- US
- NO # parsed as the boolean false, not "Norway"
NO becomes false because YAML treats yes/no/on/off/true/false as booleans. Similarly, a ZIP code 01234 can lose its leading zero or be read as octal, and a version like 1.20 becomes the number 1.2. The fix is always the same: quote it. - "NO" stays the string "NO". A syntax check that shows you the parsed types — not just "valid/invalid" — is what surfaces this.
Checking in the browser
In JavaScript, the js-yaml library parses and throws a descriptive error on malformed input — the same approach an online checker uses, running entirely client-side:
import yaml from "js-yaml";
try {
const data = yaml.load(yamlString);
console.log("Valid ✓", data);
} catch (e) {
console.log("Invalid:", e.message); // includes line & column
}
Checking on the command line
# Quick parse check with Python (no output = valid)
python -c "import yaml,sys; yaml.safe_load(open(sys.argv[1]))" file.yaml
# yamllint — also flags indentation and style issues
yamllint file.yaml
The Python one-liner answers "does it parse?" yamllint goes further, catching things a bare parser accepts — inconsistent indentation, trailing spaces, lines that are too long, and missing document markers — making it the better choice in CI.
Checking in Python
import yaml
try:
data = yaml.safe_load(open("file.yaml"))
print("Valid ✓")
except yaml.YAMLError as e:
print(f"Invalid: {e}") # mark.line / mark.column on the error
Always use safe_load, not load — the latter can construct arbitrary Python objects from untrusted YAML and is a security risk.
Check your YAML syntax now
Paste your YAML and get an instant validity check with the exact line of any error. Runs in your browser — nothing is uploaded.
Open YAML Validator →Frequently Asked Questions
What are the most common YAML syntax errors?
Tabs used for indentation (forbidden), inconsistent indentation, a missing space after a colon, unquoted values that look like other types (yes, no, on, off, leading-zero numbers), and unquoted strings with special characters. Any of these can break parsing or silently change a value's type.
Why does YAML read my value as a boolean or number?
YAML infers types automatically. Bare words like yes, no, true, false, on, off become booleans, and digits become numbers — so country: NO becomes false and 01234 may lose its leading zero. Quote the value (country: "NO") to keep it a string.
How do I check YAML syntax on the command line?
Use yamllint for style and syntax, or a quick Python parse check: python -c "import yaml,sys; yaml.safe_load(open(sys.argv[1]))" file.yaml. It prints a traceback with line and column on failure, nothing on success. yamllint additionally flags indentation and style issues a bare parser accepts.
Is valid YAML always correct YAML?
No. A file can parse cleanly yet hold the wrong data because of type inference — a string read as a boolean, a code read as a number. Checking the parsed types, not just validity, is how you catch these silent surprises.