A full-featured YAML processing framework for Python with support for free-threading
Why a fork?
PEP 703 introduced free-threaded Python as a
separate build of CPython 3.13. Thread-safety issues that might have otherwise gone
unnoticed are now much easier to trigger because of the absence of protection from
the GIL. Also, because the free-threaded build is ABI-incompatible, extension
modules need to be separate, free-threaded wheels and declare support for
it.
The PyYAML maintainers
decided to not port PyYAML to the free-threaded build
before the latter, along with Cython support for it, has been tested more extensively
in real-world applications. Our rationale with this fork is to implement support for
the free-threaded build, so that PyYAML can be tested with it by its users, even before
the port is merged upstream.
Differences compared with upstream
This fork uses Cython 3.1 which supports the free-threaded build, but whose support
is still experimental.
add_constructor, add_representer and add_*resolver now all use thread-local
registries, so you will have to explicitly register your custom constrcutors,
representers and resolvers in each thread. Here's a small test showcasing that:
❯ python3.13t tmp/t.py
Thread MainThread
objs=[Dice(1, 6), Dice(4, 4)]
Thread Thread-1 (load_dice)
Exception occurred: could not determine a constructor for the tag '!dice'
in "<file>", line 1, column 3
If you see new errors in multithreaded programs using PyYAML-ft that work with
, you may need to add calls to ,
or in your thread worker
function or worker initialization function.
PyYAML
yaml_ft.add_constructor
yaml_ft.add_representer
yaml_ft.add_implicit_resolver
yaml_ft.add_path_resolver
Python versions support
Because PyYAML-ft is only aiming to exist for as long as upstream PyYAML
does not support the free-threaded build, we recommend that users only
conditionally switch to PyYAML-ft.
At this time, PyYAML-ft only supports Python 3.13 and 3.13t (i.e. the
free-threaded build of 3.13). To switch to it, you can do the following
in your requirements.txt file:
PyYAML-ft uses a different module name (namely yaml_ft) than upstream PyYAML
on purpose, so that both can be installed in an environment at the same time.
If your library depends on both for different Python versions, you can do
the following for ease of use:
try:
import yaml_ft as yaml
except ModuleNotFoundError:
import yaml
Installation
To install, type pip install PyYAML-ft.
When LibYAML bindings are installed (enabled by default), you may use the fast
LibYAML-based parser and emitter as follows:
If you don't trust the input YAML stream, you should use:
>>> yaml.safe_load(stream)
Building from source and testing
To build PyYAML-ft from source:
Clone the fork repository with
git clone https://github.com/Quansight-Labs/pyyaml-ft.
Create a new virtual environment with python3.13 -m venv .venv.
Run source .venv/bin/activate to activate your new virtual environment.
Run python3.13 -m pip install .. This will build PyYAML-ft
from source and install it in the newly created virtual environment. By default, the
installation process will try to install the LibYAML bindings as well, but will
fail silently if it cannot and instead use the pure-Python version.
To get the latest version of the LibYAML bindings, you can use the libyaml.sh
installation script:
./packaging/build/libyaml.sh # This will install the bindings
After the bindings are installed, you can force installing the bindings with the
following invocation:
The PyYAML module was written by Kirill Simonov xi@resolvent.net.
It is currently maintained by the YAML and Python communities.
PyYAML-ft was forked by Quansight-Labs and is currently maintained by
a team of engineers working on ecosystem-wide support for free-threading.
It is released under the MIT license.
See the file LICENSE for more details.
Alternatives
Use instead of PyYAML-ft: tools that do the same job, closest first.
HyperPyYAMLPythie’s Index: 39Extensions to YAML syntax for better python interaction1.5M downloads/mo
yamlloaderPythie’s Index: 30Ordered YAML loader and dumper for PyYAML.280.8K downloads/mo
yamlcorePythie’s Index: 50YAML 1.2 Support for PyYAML139.3K downloads/mo
saneyamlPythie’s Index: 23Read and write readable YAML safely preserving order and avoiding bad surprises with unwanted infered type conversions. This library is a PyYaml wrapper with sane behaviour to read and write readable YAML safely, typically when used for configuration.175.4K downloads/mo