RapidYAML
RapidYAML (ryml) is a Python wrapper for a fast C++ library designed to parse and emit YAML. It focuses on performance by exposing a low-level, index-based C++ API that operates with node indices and string views, rather than automatically building Python dict/list structures. The current version is 0.11.1, and it maintains an active release cadence.
Common errors
-
TypeError: parse() argument 'buf' must be bytes, not str
cause Attempting to parse a standard Python string instead of a byte string.fixEncode the input string to bytes before parsing, e.g., `ryml.parse(my_string.encode('utf-8'))`. -
Node not found or invalid node ID
cause Incorrectly traversing the index-based tree, or attempting to access a non-existent child/sibling.fixAlways check the return value of tree traversal methods (e.g., `find_child`, `first_child`, `next_sibling`) against `ryml.NONE` before attempting to use the returned ID. -
Expected a dict or list, got a Tree object
cause Misunderstanding that `ryml.parse()` returns a low-level tree object, not a native Python data structure.fixManually iterate the `ryml.Tree` object using its index-based API to construct Python dictionaries and lists if required. For example, check `tree.is_map(node_id)` or `tree.is_seq(node_id)` to determine the YAML type.
Warnings
- gotcha RapidYAML's Python wrapper exposes a low-level C++ API. It does not automatically convert YAML structures into native Python dictionaries or lists. Users must explicitly traverse the tree using node indices and extract values, which are typically returned as `memoryview` objects of bytes.
- gotcha When parsing, RapidYAML requires input to be `bytes` or `bytearray` objects, not Python `str` objects. This is because the underlying C++ library does not take ownership of the source buffer.
- breaking As of RapidYAML 0.11.1 (and some earlier C++ changes), a default-constructed `ryml.Tree` is no longer empty by default. `Tree::root_id()` will not modify an empty tree to create a root. To create an explicitly empty tree, use `ryml.Tree(0)`.
- gotcha RapidYAML's C++ core is strict about JSON parsing. Attempting to parse JSON with explicitly quoted keys (e.g., `{"a":"b"}`) might result in parse errors, as it expects more 'relaxed' JSON or standard YAML.
Install
-
pip install rapidyaml
Imports
- ryml
from rapidyaml import *
import ryml
Quickstart
import ryml
yaml_bytes = b"""
foo: bar
list:
- item1
- item2
"""
tree = ryml.parse(yaml_bytes)
root_id = tree.root_id()
print(f"Root ID: {root_id}")
# Accessing 'foo: bar'
foo_id = tree.find_child(root_id, b"foo")
if foo_id != ryml.NONE:
print(f"Key: {tree.key(foo_id).decode()}, Value: {tree.val(foo_id).decode()}")
# Accessing 'list'
list_id = tree.find_child(root_id, b"list")
if list_id != ryml.NONE and tree.is_seq(list_id):
print(f"List items:")
child_id = tree.first_child(list_id)
while child_id != ryml.NONE:
print(f" - {tree.val(child_id).decode()}")
child_id = tree.next_sibling(child_id)