libvalkey-py is a Python extension that wraps the protocol parsing code from the underlying libvalkey C library. It primarily speeds up the parsing of multi bulk replies from a Valkey server. The library is actively maintained, with its current version being 4.0.1, and releases occurring regularly to introduce new features, improvements, and bug fixes.
pip install libvalkeyVerified import paths — ran on the pinned version, not inferred.
The quickstart demonstrates how to initialize the `Reader` class, feed raw Valkey protocol bytes, and retrieve parsed replies. It also shows how to handle cases where insufficient data is fed, using a custom sentinel value.
Change `import hiredis` to `import libvalkey` and update any references to `hiredis.Reader` to `libvalkey.Reader`.
Ensure `python3-dev` and `gcc` (or equivalent build tools for your OS) are installed before attempting `pip install libvalkey` if pre-built wheels are not available. For example, `sudo apt-get install python3-dev gcc` on Debian/Ubuntu.
Explicitly check the return value of `reader.gets()` for instances of `libvalkey.ReplyError` if you want to handle server-generated errors. For example: `reply = reader.gets(); if isinstance(reply, libvalkey.ReplyError): handle_error(reply)`.
To handle decoding errors gracefully, initialize the `Reader` with a different `errors` handler, such as 'ignore', 'replace', or 'backslashreplace' (e.g., `libvalkey.Reader(encoding='utf-8', errors='replace')`).
Thoroughly test existing code relying on `Reader` behavior when upgrading to v4.0.0 or later to ensure parsing logic remains consistent with expectations. Refer to the GitHub releases for specific changes.
Install the library using pip: `pip install libvalkey`
Ensure thread safety by using separate `libvalkey.Reader` instances per thread, or re-establish the connection to the Valkey server and the `Reader` instance.
Specify the correct encoding (e.g., 'latin-1' or the actual encoding used by your Valkey data) and an appropriate error handler when initializing the `Reader`: `reader = libvalkey.Reader(encoding='your_encoding', errors='replace')`.
Install a C compiler (e.g., `sudo apt-get install build-essential` on Debian/Ubuntu, or Xcode Command Line Tools on macOS) and Python development headers (e.g., `sudo apt-get install python3-dev` on Debian/Ubuntu).
Check the command being sent to the Valkey server for syntax errors, incorrect command names, or invalid arguments. Ensure the Valkey server version supports the command being used. The error message following `ERR` provides specific details from the server.