YAML Gotchas: Norway, Octals and Anchors
YAML guesses the type of every unquoted value. The guessing rules changed between YAML 1.1 (2005) and YAML 1.2 (2009), and real tools still use both: PyYAML and many older libraries implement 1.1, while yq and most newer parsers follow 1.2. The same file can therefore mean different things to different tools in the same pipeline. This lesson shows the famous traps with two real parsers side by side, then covers anchors, aliases and merge keys.
One file, two parsers
cat > settings.yaml <<'YAML'
country: NO
enabled: yes
debug: off
on: push
version: 1.10
mode: 0755
zip_code: 01234
duration: 1:30
released: 2026-01-15
build: 1e3
YAML
echo "--- PyYAML 6 (YAML 1.1 rules)"
python3 -c 'import yaml; [print(f"{k!r:12} {v!r}") for k, v in yaml.safe_load(open("settings.yaml")).items()]'
echo "--- yq 4 (YAML 1.2 rules)"
yq -o=json settings.yaml--- PyYAML 6 (YAML 1.1 rules)
'country' False
'enabled' True
'debug' False
True 'push'
'version' 1.1
'mode' 493
'zip_code' 668
'duration' 90
'released' datetime.date(2026, 1, 15)
'build' '1e3'
--- yq 4 (YAML 1.2 rules)
{
"country": "NO",
"enabled": "yes",
"debug": "off",
"on": "push",
"version": 1.10,
"mode": 755,
"zip_code": 1234,
"duration": "1:30",
"released": "2026-01-15",
"build": 1e3
}
Read the differences line by line:
| Value | YAML 1.1 (PyYAML) | YAML 1.2 (yq) | What you probably meant |
|---|---|---|---|
NO (Norway's country code) |
False |
"NO" |
the string "NO" |
yes, off |
True, False |
strings | depends: quote it either way |
on: as a key |
the boolean True |
"on" |
the key on (GitHub Actions' trigger key) |
1.10 |
the float 1.1 |
a float written 1.10 |
the version string "1.10" |
0755 |
octal: 493 |
decimal: 755 |
file mode 0755: write 0o755 (1.2) or quote it and convert yourself |
01234 |
octal: 668 |
1234 |
the string "01234" |
1:30 |
sexagesimal: 90 |
"1:30" |
the string "1:30" (a port mapping, a time) |
2026-01-15 |
a date object |
a timestamp (printed as a string) | usually the string |
1e3 |
the string "1e3" |
the number 1000 |
depends |
The "Norway problem" got its name from country lists where NO became false. The on: key is why Python scripts
that read GitHub Actions workflows with PyYAML find a key called True. And Docker Compose's documentation recommends
quoting port mappings because YAML 1.1 parsers read xx:yy as a base-60 number (22:22 is 1342).
The fix is the same for all of them: quote strings that look like something else. Versions, IDs, codes,
port mappings, file modes, anything with a leading zero, and the words yes/no/on/off/y/n. The
yamllint truthy rule flags the boolean-like words automatically.
Anchors and aliases: reuse inside one file
&name marks a node as an anchor, *name inserts it again as an alias. The << merge key merges an
aliased mapping into the current one, with local keys winning. CI files use this to share settings between jobs:
cat > ci.yaml <<'YAML'
.defaults: &defaults
image: python:3.13
retry: 2
tags: [linux]
test:
<<: *defaults
script: pytest
deploy:
<<: *defaults
retry: 0 # overrides the merged value
script: ./deploy.sh
notify_on: &channels [ops, dev]
alerts:
channels: *channels
YAML
yq --yaml-fix-merge-anchor-to-spec=true -o=json 'explode(.) | del(.[".defaults"])' ci.yaml{
"test": {
"image": "python:3.13",
"retry": 2,
"tags": [
"linux"
],
"script": "pytest"
},
"deploy": {
"image": "python:3.13",
"tags": [
"linux"
],
"retry": 0,
"script": "./deploy.sh"
},
"notify_on": [
"ops",
"dev"
],
"alerts": {
"channels": [
"ops",
"dev"
]
}
}
explode(.)asks yq to resolve anchors, aliases and merges, the way an application sees the data after parsing.- Without
--yaml-fix-merge-anchor-to-spec=true, yq 4.54 prints a warning and keeps its old behaviour, in which a merged value overrides a key written before the<<line. The spec says local keys always win. Write<<:as the first key of the mapping and every parser agrees. - GitLab CI supports anchors and
<<merges (and offersextends:as a clearer alternative); GitHub Actions added anchors and aliases in 2025, but its documentation does not cover the<<merge key, so test before relying on it. - The merge key
<<was a YAML 1.1 extension and is not part of the 1.2 spec. Most parsers still support it, yq and PyYAML included. - Anchors only work within one file. Kubernetes and Helm do not share anchors across files; Helm uses templates, Kustomize uses patches.
Duplicate keys
The YAML spec says keys must be unique, but many parsers silently keep the last value:
cat > dup.yaml <<'YAML'
replicas: 2
image: shop-api:1.4.2
replicas: 5
YAML
echo "--- PyYAML: last one wins, silently"
python3 -c 'import yaml; print(yaml.safe_load(open("dup.yaml")))'
echo "--- yq"
yq -o=json -I=0 . dup.yaml; echo "-> status $?"--- PyYAML: last one wins, silently
{'replicas': 5, 'image': 'shop-api:1.4.2'}
--- yq
{"replicas":2,"image":"shop-api:1.4.2","replicas":5}
-> status 0
PyYAML kept the last value. yq kept both keys and wrote JSON with a duplicate key, which the next JSON parser
will resolve in its own way. In a long manifest, a second resources: or env: block quietly replaces the first. yamllint's key-duplicates
rule (on by default) catches it.
yaml.load is unsafe
PyYAML's yaml.load(text, Loader=yaml.Loader) (and the old yaml.load(text)) can construct arbitrary Python objects
from tags such as !!python/object/apply:os.system, so loading an untrusted file can run code. Always use
yaml.safe_load, which only builds plain types:
cat > evil.yaml <<'YAML'
payload: !!python/object/apply:os.system ["echo this ran a shell command"]
YAML
python3 - <<'PY'
import yaml
try:
print(yaml.safe_load(open("evil.yaml")))
except yaml.YAMLError as e:
print("safe_load refused:", str(e).splitlines()[0])
PYsafe_load refused: could not determine a constructor for the tag 'tag:yaml.org,2002:python/object/apply:os.system'
Test yourself
Under YAML 1.1 rules (PyYAML), what is country: NO?
- The string "NO"
- The boolean False
- null
- A syntax error
Why should version numbers like 1.10 be quoted in YAML?
- YAML forbids dots
- Unquoted, they are floats, so
1.10becomes 1.1 - Quotes make parsing faster
- They should not be quoted
What does <<: *defaults do inside a mapping?
- Includes another file
- Merges the mapping anchored as
defaultsinto this one; keys written locally override it - Comments out the block
- Creates an anchor