YAML in Python

Direct answer

In Python, load YAML with a maintained library such as PyYAML / ruamel.yaml, prefer safe/typed APIs for untrusted input, and validate files before deploy.

Libraries

Common choice: PyYAML / ruamel.yaml.

Safe load example

pip install pyyaml
import yaml
with open('config.yaml') as f:
    data = yaml.safe_load(f)

Common pitfalls

  • Always use yaml.safe_load, not load
  • Install the PyYAML package (import name is yaml)
  • ruamel.yaml preserves comments when rewriting files

Related

Validation tips

  • Unit-test loading of fixture YAML in CI.
  • Reject documents that fail safe/typed parse.
  • Cross-check ambiguous scalars with the 1.1 vs 1.2 guide.
  • Use the browser validator for quick private checks.

When not to use YAML

If you need comment-preserving round trips, binary data, or strict schema-first APIs, consider whether JSON Schema + JSON, or a purpose-built config format, fits better — then convert only at boundaries.