yaml-rs
raw JSON → 0.1.0 verified Sat May 09 auth: no python
A high-performance YAML parser for Python written in Rust. Current version 0.1.0. Uses the saphyr YAML library under the hood. Provides loads/dumps with a DuplicateKeyPolicy enum (default Error). Active development, weekly releases.
pip install yaml-rs Common errors
error yaml_rs.YAMLDecodeError: Duplicate key found: x ↓
cause Default behavior in 0.1.0+ is to error on duplicate keys. Previous versions allowed overwriting.
fix
Use DuplicateKeyPolicy.LastWins: loads(yaml, duplicate_key_policy=DuplicateKeyPolicy.LastWins)
error ModuleNotFoundError: No module named 'yaml_rs' ↓
cause Package not installed or wrong Python environment.
fix
Run
pip install yaml-rs and ensure you're using the correct interpreter (Python >=3.10). Warnings
breaking In version 0.1.0, the default DuplicateKeyPolicy changed from 'LastWins' to 'Error'. Parsing YAML with duplicate keys will now raise YAMLDecodeError instead of silently using the last value. ↓
fix To preserve old behavior, pass DuplicateKeyPolicy.LastWins: loads(yaml_str, duplicate_key_policy=DuplicateKeyPolicy.LastWins)
gotcha yaml-rs does not support all YAML 1.2 features. It is based on the saphyr crate which targets YAML 1.1. Some edge cases may parse differently than PyYAML. ↓
fix Test your YAML data thoroughly. Use strict YAML 1.1 compliant documents.
deprecated No deprecations known at this time.
Imports
- loads wrong
import yaml_rs yaml_rs.loads(yaml)correctfrom yaml_rs import loads - dumps
from yaml_rs import dumps - YAMLDecodeError wrong
yaml_rs.YAMLDecodeErrorcorrectfrom yaml_rs import YAMLDecodeError - DuplicateKeyPolicy
from yaml_rs import DuplicateKeyPolicy
Quickstart
from yaml_rs import loads, dumps
data = {"name": "yaml-rs", "version": [0, 1, 0]}
yaml_str = dumps(data)
print(yaml_str)
# 'name: yaml-rs\nversion: [0, 1, 0]\n'
parsed = loads(yaml_str)
print(parsed)
# {'name': 'yaml-rs', 'version': [0, 1, 0]}