Validate a YAML File with an AI Agent, Locally
Check YAML syntax on your own machine with a short PyYAML script an AI agent can write, instead of pasting config into an online validator.
Updated · Markdown version
An AI agent can write a small script that parses your YAML file on your own machine, so the config never leaves your disk. The script reports a syntax error with a line and column when one exists, and it exits with a nonzero status so you can call it from a shell or a pre-commit step.
Why check locally
Online validators ask you to paste the file into a web form. Config files often hold hostnames, internal paths, or keys that should not travel to a third-party page. A local parser gives the same syntax answer and keeps the file where it is. The agent's role is to write and adjust the script; the parsing itself is done by a library on your machine.
Steps
- Save the YAML as a file, for example
deploy.yaml. Do not paste its contents into the chat. - Ask the agent for a script that uses PyYAML's
safe_load. Specify that it must catchyaml.YAMLErrorand print the position when one is available. - Save the script as
check_yaml.py. The core logic should look like this:
import sys
import yaml
path = sys.argv[1]
with open(path, encoding="utf-8") as f:
try:
yaml.safe_load(f)
except yaml.YAMLError as exc:
if hasattr(exc, "problem_mark"):
mark = exc.problem_mark
print(f"{path}:{mark.line + 1}:{mark.column + 1}: syntax error")
else:
print(f"{path}: {exc}")
sys.exit(1)
print(f"{path}: parsed OK")- Run it with
python check_yaml.py deploy.yaml. Usesafe_loadrather than the unrestricted loader, becausesafe_loadaccepts only standard YAML tags. - Check the result. Break the file on purpose, for example by removing a colon or mixing tab indentation, and confirm the script prints a position and exits with status 1. Then restore the file and confirm it prints
parsed OK.
The line and column numbers come from the parser's problem_mark. PyYAML's documentation counts both from 0, which is why the script adds 1 to each before printing. Compare the printed position with your editor once to confirm the offset on your own file.
What the check does not cover
The script answers a syntax question. The YAML 1.2.2 specification separates well-formed syntax from validity, meaning whether tags are recognized and whether values match what your application expects. A file can parse cleanly and still contain a wrong port number or a misspelled key that your program rejects. Checking those needs a schema or application-level test, not a parser alone.
Pitfalls
- Tabs in indentation. YAML does not accept tab characters for indentation. The error message may point to a line after the real problem, so look one or two lines above the reported position.
- Duplicate keys. The YAML 1.2.2 specification lists duplicate mapping keys as a loading failure. In a local test on 2026-11-22 with PyYAML 6.0.1,
yaml.safe_loadaccepteda: 1followed bya: 2without an error and returned{'a': 2}. A parse alone will not catch a repeated key, so test a sample with one before you rely on the script. - Unsafe loaders. Some older examples call
yaml.loadwithout a loader argument. Ask the agent to usesafe_load, which restricts the file to standard tags. - Encoding. Open the file with an explicit encoding. A file saved in a non-UTF-8 editor can fail with an error that looks like a YAML problem.
- Empty files.
safe_loadreturnsNonefor a stream with no documents, so an empty config passes the syntax check. Add a check forNoneif your application needs content.
When to use a skill instead
Ad hoc prompting works for a one-time check. A reusable skill helps when you validate the same kind of file across projects, because the steps and checks stay the same. config-file-validator is the closest related skill on this site. It is a script-based checker for single config files, and it covers simple YAML among other formats. If your file is a mix of JSON, TOML, and YAML, that skill fits better than a script you rewrite each time. For a YAML file that needs checks beyond syntax, a skill with your own schema rules would be a better starting point than this guide's script.
FAQ
How do I validate a YAML file from the command line? Write a short script that calls yaml.safe_load on the file and exits nonzero when a YAMLError is raised. Run it with the file path as an argument, as shown in the steps above.
Does a file that parses without errors have correct values? Not necessarily. A parse only confirms the syntax is well-formed. Values such as ports, paths, or required keys need a schema or an application-level test.
Why does the error point to the wrong line? Indentation and quoting errors often surface on the line after the real mistake, because the parser cannot tell the intent until it reads further. Check the lines just above the reported position first.
Related skills in this catalog
Platform Score is our assessment under a published method. It is not a safety guarantee.
Related guides
Sources
- https://pyyaml.org/wiki/PyYAMLDocumentation (accessed 2026-11-22)
- https://yaml.org/spec/1.2.2/ (accessed 2026-11-22)
This page is not offered to search engines yet: The content file does not mark this page indexable yet.