YAML API Reference¶
Auto-generated API documentation for the YAML parser module.
yaml
¶
YAML parser and serializer (common subset) — zero dependencies, stdlib only, Python 3.10+.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Supports the most commonly used YAML features: mappings, sequences, scalars (str/int/float/bool/null), flow style, block scalars, multi-document streams, and comments.
Does NOT implement: anchors/aliases, tags, merge keys, complex keys.
Example::
data = load("name: Alice
age: 30") # {'name': 'Alice', 'age': 30} print(dump(data)) # age: 30 # name: Alice
YAMLError
¶
load(text)
¶
Parse a YAML string and return a Python object.
Only produces safe types: dict, list, str, int, float, bool, None.
Equivalent to PyYAML's yaml.safe_load().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
YAML document string. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Parsed Python object. |
Raises:
| Type | Description |
|---|---|
YAMLError
|
If the YAML is malformed. |
Source code in yaml/yaml.py
load_all(text)
¶
Parse a multi-document YAML string.
Yields one Python object per YAML document (separated by ---).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Multi-document YAML string. |
required |
Yields:
| Type | Description |
|---|---|
Any
|
Parsed Python objects, one per document. |
Source code in yaml/yaml.py
dump(data, stream=None, *, default_flow_style=None, indent=2, sort_keys=True, allow_unicode=True)
¶
Serialize a Python object to a YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Python object to serialize. |
required |
stream
|
IO[str] | None
|
If provided, write to this stream and return None. |
None
|
default_flow_style
|
bool | None
|
True for flow (inline) style, False for block, None for auto (empty collections use flow). |
None
|
indent
|
int
|
Number of spaces per indentation level. |
2
|
sort_keys
|
bool
|
Sort mapping keys alphabetically. |
True
|
allow_unicode
|
bool
|
Allow unicode characters in output. |
True
|
Returns:
| Type | Description |
|---|---|
str | None
|
YAML string if stream is None, otherwise None. |
Source code in yaml/yaml.py
dump_all(documents, stream=None, *, default_flow_style=None, indent=2, sort_keys=True, allow_unicode=True)
¶
Serialize multiple Python objects as a multi-document YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list[Any] | tuple[Any, ...]
|
Iterable of Python objects to serialize. |
required |
stream
|
IO[str] | None
|
If provided, write to this stream and return None. |
None
|
default_flow_style
|
bool | None
|
True for flow style, False for block, None for auto. |
None
|
indent
|
int
|
Number of spaces per indentation level. |
2
|
sort_keys
|
bool
|
Sort mapping keys alphabetically. |
True
|
allow_unicode
|
bool
|
Allow unicode characters in output. |
True
|
Returns:
| Type | Description |
|---|---|
str | None
|
YAML string if stream is None, otherwise None. |