HASH-backed `SearchIndex.fetch()` raises `UnicodeDecodeError` with `decode_responses=True`

未关闭
#875 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

@mohsinaliwork686-rgb 已经在做这个了。

开始于 2026年12月2日。

  • #879 来自 @guangyuli2026 —— 已关闭,未合并
  • #893 来自 @mohsinaliwork686-rgb —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
64/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python, redis
领域
backend, databases

调研方向

Start in the HASH storage read paths, HashStorage._get() and its async counterpart, which call hgetall; compare the search paths in redisvl/index/index.py and redisvl/redis/utils.py for NEVER_DECODE handling. Add sync and async regression coverage for both decode_responses settings, verifying the text and original embedding bytes, then run the relevant tests.

由索引模型根据 Issue 内容生成。

描述

Summary

SearchIndex.fetch() raises UnicodeDecodeError when fetching an existing HASH record containing a binary embedding through a Redis client configured with decode_responses=True.

Loading the record succeeds. Fetching it with decode_responses=False also succeeds.

Verified with Python 3.13.14, RedisVL 0.33.0, redis-py 8.1.0, and Redis 8.4.8 using Docker.

Related: [redis/redis-vl-python#844](https://github.com/redis/redis-vl-python/pull/844) also edits HashStorage.get()/aget() to handle missing records.

Cause

HashStorage._get() calls client.hgetall(key). With decode_responses=True, redis-py attempts to decode every HASH value, including the binary embedding. Embedding bytes that are invalid UTF-8 cause the read to fail before RedisVL receives the record.

The async path through aget()/_aget() makes the same hgetall call. That path has not been tested in this reproduction.

Reproduction

Requires Redis with Search support. REDIS_URL defaults to redis://localhost:6379.

import os
from uuid import uuid4

from redis import Redis
from redisvl.index import SearchIndex
from redisvl.redis.utils import array_to_buffer

for decode in (False, True):
    client = Redis.from_url(
        os.getenv("REDIS_URL", "redis://localhost:6379"),
        decode_responses=decode,
    )
    name = f"fetch_decode_{uuid4().hex}"
    index = SearchIndex.from_dict(
        {
            "index": {
                "name": name,
                "prefix": name,
                "storage_type": "hash",
            },
            "fields": [
                {"name": "text", "type": "text"},
                {
                    "name": "embedding",
                    "type": "vector",
                    "attrs": {
                        "algorithm": "flat",
                        "dims": 4,
                        "datatype": "float32",
                        "distance_metric": "cosine",
                    },
                },
            ],
        },
        redis_client=client,
    )
    index.create()
    try:
        index.load(
            [{
                "text": "hello",
                "embedding": array_to_buffer([0.1] * 4, "float32"),
            }],
            keys=[index.key("doc")],
        )
        try:
            record = index.fetch("doc")
            print(
                f"decode_responses={decode}: {record['text']!r}, "
                f"embedding type={type(record['embedding']).__name__}"
            )
        except UnicodeDecodeError as exc:
            print(
                f"decode_responses={decode}: "
                f"{type(exc).__name__}: {exc}"
            )
    finally:
        index.delete(drop=True)
        client.close()
Current behaviour

Fetching succeeds with decode_responses=False and raises with decode_responses=True.

decode_responses=False: 'hello', embedding type=bytes
decode_responses=True: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 0: invalid continuation byte
Expected behaviour

Both fetches should succeed and return the stored record. In each case, record["text"] should equal "hello" and record["embedding"] should contain the exact bytes originally written.

The reproduction should print the following output.

decode_responses=False: 'hello', embedding type=bytes
decode_responses=True: 'hello', embedding type=bytes

Suggested fix

Read the HASH reply with redis-py’s NEVER_DECODE option, following the search paths in redisvl/index/index.py and redisvl/redis/utils.py, then decode the non-vector fields while preserving vector fields as bytes.

NEVER_DECODE applies to the entire reply. Keep these reads in the existing non-transactional pipelines, since the option is ignored inside MULTI/EXEC.

Apply the handling to both synchronous and asynchronous HASH reads. Add regression coverage for decode_responses=True and False, asserting that fetching succeeds and the original embedding bytes are preserved.

主要语言
Python
星标
435
派生
123
平均合并
8 天 1 小时
30 天内合并 PR
39

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

redis/redis-vl-python 的其他 Issue

查看 redis/redis-vl-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。