Rock-solid YAML for Python, written in Rust.
YAMLRocks is the rock-solid YAML library for Python: a Rust-backed extension that parses and emits YAML fast, follows the YAML 1.2 specification (with a YAML 1.1 compatibility mode), and, unlike PyYAML, round-trips documents while preserving comments, anchors, and formatting.
Rock-solid means three things: correct, secure by default, and fast, with a Rust core doing the heavy lifting. (The R in Rock is for Rust.)
The Python YAML ecosystem has long forced a trade-off. YAMLRocks refuses it:
| Library | Fast | YAML 1.2 | Comments / round-trip | Native includes |
|---|---|---|---|---|
| PyYAML | with C loader | ✗ (1.1 only) | ✗ | ✗ |
| ruamel.yaml | ✗ (pure Python) | ✓ | ✓ | ✗ |
| YAMLRocks | ✓ (Rust) | ✓ | ✓ | ✓ |
It is also fast. Release-build benchmarks (python bench/bench.py) show how
many times faster YAMLRocks is:
| Operation | vs PyYAML (C loader) | vs ruamel.yaml |
|---|---|---|
Parse (loads) |
~5-10x faster | ~85-135x faster |
Serialize (dumps) |
~15-19x faster | ~155-210x faster |
Split config (!include, hundreds of files) |
~18x faster | n/a |
It is safe against the common YAML attack classes, free-threaded (nogil) ready,
and ships with JSON Schema validation, a PyYAML-compatible shim, rich standard-library
type support, and the !secret and !env_var config tags.
YAMLRocks is also tested against a reproducible real-world corpus covering Home
Assistant, ESPHome, Ansible, Kubernetes, Docker Compose, GitHub Actions,
CloudFormation, GitOps, Helm, OpenAPI, dbt, CircleCI, Serverless, and Tekton.
Each standalone YAML file must parse and round-trip byte-for-byte; selected Home
Assistant configs are additionally tested through their full !include graph.
See real-world verification for the current corpus and
scope.
YAMLRocks is pre-1.0 software. The core promises are already explicit: safe loading by default, YAML 1.2 semantics, reproducible real-world verification, and byte-for-byte round-trip for unmodified documents. Some advanced APIs may still change before 1.0 while the project gathers production feedback. See the stability and roadmap page for the 1.0 contract.
pip install yamlrocksBuilding from source requires a Rust toolchain; see Setting up development environment below for the uv-based build.
The API stays small: loads returns native Python objects and dumps
returns bytes.
import yamlrocks
# Parse YAML into native Python objects
data = yamlrocks.loads(b"key: value\nlist:\n - 1\n - 2")
# {'key': 'value', 'list': [1, 2]}
# Serialize back to YAML bytes (dumps() returns bytes)
yamlrocks.dumps(data)
# b'key: value\nlist:\n - 1\n - 2\n'
# Multiple documents
yamlrocks.loads_all(b"---\na: 1\n---\nb: 2")
# [{'a': 1}, {'b': 2}]
# Load and dump files directly
config = yamlrocks.load("config.yaml")
yamlrocks.dump(config, "config.yaml")YAML 1.2 is the default, so yes/no/on/off are plain strings. Opt into the
1.1 schema when you need its booleans and octals:
yamlrocks.loads(b"enabled: yes") # {'enabled': 'yes'}
yamlrocks.loads(b"enabled: yes", option=yamlrocks.OPT_YAML_1_1) # {'enabled': True}OPT_UPGRADE_1_1 reads 1.1 and always emits canonical 1.2, so a project can
ease off the legacy spellings without a manual conversion step.
doc = yamlrocks.loads(content, option=yamlrocks.OPT_ROUND_TRIP)
doc["server"]["host"] = "example.com" # deep edits write through to the AST
print(doc.to_yaml().decode()) # comments and formatting preservedAn unmodified document re-emits byte-for-byte identical; only the nodes you touch are rewritten.
doc = yamlrocks.loads(
content,
option=yamlrocks.OPT_ROUND_TRIP | yamlrocks.OPT_INCLUDES,
include_dir="/config",
)
doc["automation"][0]["trigger"] = "state" # edit a value from an included file
yamlrocks.dump_includes(doc, include_dir="/config")
# Only the modified included file is rewritten; the root config is untouched.Supported tags: !include, !include_dir_named, !include_dir_list,
!include_dir_merge_named, !include_dir_merge_list.
data = yamlrocks.loads(content, option=yamlrocks.OPT_ANNOTATED)
data.__line__ # 1
data["server"].__line__ # 3YAMLRocksAnnotatedDict/YAMLRocksAnnotatedList/YAMLRocksAnnotatedStr subclass dict/list/str, so
they behave exactly like the built-ins while carrying __line__, __column__,
and __file__, compatible with Home Assistant's annotated YAML.
Options compose with |, the integer bit-flag pattern:
| Flag | Effect |
|---|---|
OPT_YAML_1_1 |
YAML 1.1 schema (yes/no booleans, octals, etc.) |
OPT_UPGRADE_1_1 |
Read 1.1, always emit canonical 1.2 |
OPT_ROUND_TRIP |
Return a YAMLRocksDocument preserving comments and formatting |
OPT_ANNOTATED |
Return subclasses with source locations |
OPT_INCLUDES |
Resolve !include tags (needs include_dir) |
OPT_DUPLICATE_KEYS_ERROR |
Reject a repeated mapping key |
OPT_INDENT_2 / OPT_INDENT_4 |
Indentation width for dumps |
OPT_SORT_KEYS |
Sort mapping keys when dumping |
OPT_FLOW_STYLE |
Emit flow style ({}/[]) |
OPT_LITERAL_STRINGS |
Emit multi-line strings as literal blocks (|) |
OPT_EXPLICIT_START / OPT_EXPLICIT_END |
Emit --- / ... markers |
See the documentation for the full option set, including the standard-library type and datetime flags.
Full documentation lives at yaml.rocks: getting-started guides, recipes (Home Assistant, config editors), the complete API reference, security notes, and head-to-head comparisons with PyYAML and ruamel.yaml.
This repository keeps a change log using GitHub's releases functionality. The format of the log is based on Keep a Changelog.
Releases are based on Semantic Versioning, and use the format
of MAJOR.MINOR.PATCH. In a nutshell, the version will be incremented
based on the following:
MAJOR: Incompatible or major changes.MINOR: Backwards-compatible new features and enhancements.PATCH: Backwards-compatible bugfixes and package updates.
This is an active open-source project. We are always open to people who want to use the code or contribute to it.
We've set up a separate document for our contribution guidelines.
Using AI tools to help is fine, but you must review and understand everything you submit. Please read our AI Policy first; autonomous agents are not allowed, and unreviewed AI output will be closed.
Thank you for being involved! 😍
The easiest way to start is by opening a CodeSpace here on GitHub, or by using the Dev Container feature of Visual Studio Code.
YAMLRocks is a Rust extension built with maturin and managed with uv, which
handles the Python version, the virtual environment, and every dependency from
pyproject.toml. You need:
- A Rust toolchain
- uv (it installs a suitable Python 3.12+ for you)
- Node.js 22+ (only for the documentation site and some lint hooks)
To set up the environment and build the extension:
uv sync # create the venv and install all dev dependencies
uv run maturin develop # build and install the extension (rerun after Rust changes)As this repository uses the prek framework, all changes are linted and tested with each commit. You can run all checks manually:
prek run --all-filesTo run just the tests (always under a memory guard during development):
timeout 120 bash -c 'ulimit -v 3000000; uv run pytest'See CONTRIBUTING.md for the full workflow, including the
just task runner and how to fetch the optional test-data submodules.
The original setup of this repository is by Franck Nijhof.
For a full list of all authors and contributors, check the contributor's page.
MIT License
Copyright (c) 2026 Franck Nijhof
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.