Skip to main content

@betterdb/valkey-search-kit (Python)

PyPI version total downloads license: MIT python GitHub stars

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.

See it live in BetterDB Monitor

BetterDB Monitor gives you live dashboards for the AI workloads running on your Valkey:

  • AI Cache & Memory - hit rate, cost saved, evictions, and index size across all your caches and memory stores, with history.
  • AI Traces - OpenTelemetry waterfalls for each request, correlated with live Valkey state to explain every cache hit and miss.

AI Cache & Memory tab in BetterDB Monitor

AI Traces waterfall in BetterDB Monitor

Run it self-hosted (docker run -p 3001:3001 betterdb/monitor), or use BetterDB Cloud - which can also provision a managed, TLS-enabled Valkey instance with the Search module in one click - exactly what this library needs.

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

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.1.tar.gz (8.2 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.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for betterdb_valkey_search_kit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1dc5cf7793f0d1cd3762f2a5a53a57214d71b04a07a41c982f081afac4ad6cf1
MD5 909b867eed706e4089b7933a0d7956b0
BLAKE2b-256 13a60c2a1a25b44f84ca4f7bd1a1ce499f19516b01f3de5a9b7c4e002223ffc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterdb_valkey_search_kit-0.1.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for betterdb_valkey_search_kit-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6513c9c24fc42b30f18cd344e2578d71c6cd7bd4e2132a0d66ba8a3cce24aa6
MD5 185b2f119363808e24c35e5b3b9c7ca0
BLAKE2b-256 90320a5f190d5f02d6ea6b0411aab22c5f2f9295a4d2808f0c1b218b7a79b6e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterdb_valkey_search_kit-0.1.1-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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page