Skip to main content

Shared Valkey Search (FT.*) helpers: float32 vector encoding, FT.SEARCH reply parsing, version-skew FT.INFO parsing, TAG escaping, and error classification.

Project description

@betterdb/valkey-search-kit (Python)

betterdb-valkey-search-kit — shared low-level helpers for working with Valkey Search (FT.*) from Python. This is the Python equivalent of the TypeScript @betterdb/valkey-search-kit package, and the shared foundation the betterdb-retrieval and betterdb-agent-memory packages build on.

It has no runtime dependencies and exposes only pure functions, so it stays trivial to vendor and test.

Install

pip install betterdb-valkey-search-kit

API

Vector encoding

from betterdb_valkey_search_kit import encode_float32, decode_float32

blob = encode_float32([0.1, 0.2, 0.3])   # little-endian Float32 bytes
vec = decode_float32(blob)               # back to list[float]

Use encode_float32 to store embeddings as binary HSET field values and as the PARAMS vector for a KNN FT.SEARCH.

TAG escaping

from betterdb_valkey_search_kit import escape_tag

f"@model:{{{escape_tag('gpt-4o')}}}"   # -> "@model:{gpt\\-4o}"

Escapes every character with special meaning in the TAG filter syntax, including spaces (unescaped spaces are treated as OR term separators).

FT.SEARCH reply parsing

from betterdb_valkey_search_kit import parse_ft_search_response

raw = await client.execute_command("FT.SEARCH", index, query, ...)
hits = parse_ft_search_response(raw)
# [{"key": "cache:entry:abc", "fields": {"prompt": "...", "__score": "0.05"}}]

Handles valkey-py's mixed bytes/str replies, RETURN 0 mode (keys with no field list), and odd-length field lists. Binary field values that are not valid UTF-8 (e.g. raw embedding bytes) are skipped. Never raises — returns [] on any malformed input.

FT.INFO parsing (version-skew tolerant)

from betterdb_valkey_search_kit import (
    parse_dimension_from_info,
    parse_ft_info_stats,
)

info = await client.execute_command("FT.INFO", index)
dims = parse_dimension_from_info(info)        # 1536, or 0 if no vector field
stats = parse_ft_info_stats(info)             # FtIndexStats(num_docs=..., indexing_state=...)

parse_dimension_from_info understands both the flat DIM attribute pair and the nested index/dimensions shape introduced in Valkey Search 1.2.

Error classification

from betterdb_valkey_search_kit import is_index_not_found_error

try:
    await client.execute_command("FT.INFO", index)
except Exception as err:
    if is_index_not_found_error(err):
        ...  # index does not exist yet
    else:
        raise

Matches the "index does not exist" message variants emitted across Valkey Search / RediSearch versions, case-insensitively.

Development

uv run --extra dev pytest tests -q

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betterdb_valkey_search_kit-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

betterdb_valkey_search_kit-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file betterdb_valkey_search_kit-0.1.0.tar.gz.

File metadata

File hashes

Hashes for betterdb_valkey_search_kit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 549b0f5dbb7790b93a53cff5ed085e6abfb265da47a60e9fe6ab34063cac799e
MD5 3a0d34dd1f5417b35724008387df9df9
BLAKE2b-256 ea6e51e0fea389566c21a2c11ada5e8dd48e5a39660ac8981b80922cbb094d99

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterdb_valkey_search_kit-0.1.0.tar.gz:

Publisher: valkey-search-kit-py-release.yml on BetterDB-inc/monitor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file betterdb_valkey_search_kit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for betterdb_valkey_search_kit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 37e883972117edd6ec2f032110e7580dd8efcf5bca38270beb8ba4e8bb932f42
MD5 422dd4fdfd3e884158841c94b8184ef6
BLAKE2b-256 5ee0c9d095b721cca1acdf358d5216690b5d5b5fd28b31581e52aafd22f27568

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterdb_valkey_search_kit-0.1.0-py3-none-any.whl:

Publisher: valkey-search-kit-py-release.yml on BetterDB-inc/monitor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page