PyYAML

6.0.3 · active · verified Fri Mar 27

PyYAML is a YAML 1.1 parser and emitter for Python, providing safe and unsafe loaders, a complete Unicode-aware parser, pickle-compatible serialisation, and a capable extension API for custom tags and representers. Current version is 6.0.3 (released September 2025), which adds Python 3.14 and experimental free-threading support. The 6.x line follows an irregular maintenance cadence with patch releases typically spaced 1–2 years apart; a 7.0 dev branch exists but has no published release date.

Warnings

Install

Imports

Quickstart

Round-trip: parse a YAML config string then dump it back to YAML text, with safe loader/dumper throughout.

import yaml

# --- Parse YAML (safe, no arbitrary code execution) ---
raw = """
service:
  host: localhost
  port: 8080
  debug: false
tags:
  - web
  - api
"""

config = yaml.safe_load(raw)
print(config['service']['host'])   # 'localhost'
print(config['service']['port'])   # 8080  (int, not str)
print(type(config['service']['debug']))  # <class 'bool'>

# --- Dump back to YAML text ---
output = yaml.safe_dump(config, allow_unicode=True, sort_keys=False)
print(output)

# --- Load multiple documents ---
multi = """
---
name: alpha
---
name: beta
"""
import io
docs = list(yaml.safe_load_all(io.StringIO(multi)))
print(docs)  # [{'name': 'alpha'}, {'name': 'beta'}]

# --- Error handling ---
try:
    yaml.safe_load("key: [unclosed")
except yaml.YAMLError as exc:
    print(f"Parse error: {exc}")

view raw JSON →