AttributeError: ‘str’ object has no attribute ‘decode’. Did you mean: ‘encode’?

用户使用 LlamaIndex 的 RedisKVStore (通过 RedisDocumentStore 配合 IngestionPipeline)时,在调用 pipeline.run() 或 docstore.get_all_document_hashes() 时触发。通常发生在 Redis 连

AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

AttributeError: ‘str’ object has no attribute ‘decode’. Did you mean: ‘encode’?

快速结论:此报错发生在 LlamaIndex 的 RedisKVStore 中,当 Redis 客户端配置了 decode_responses=True 时触发。优先排查 Redis 连接池是否开启了自动解码,或代码中 base.pyget_all / aget_all 方法是否缺少 bytes 类型判断。

问题场景

用户使用 LlamaIndex 的 RedisKVStore(通过 RedisDocumentStore 配合 IngestionPipeline)时,在调用 pipeline.run()docstore.get_all_document_hashes() 时触发。通常发生在 Redis 连接池显式设置了 decode_responses=True 的情况下。

报错原文

File "/opt/.../.venv/lib/python3.13/site-packages/llama_index/storage/kvstore/redis/base.py", line 161, in get_all
    collection_kv_dict[key.decode()] = value
                       ^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?

原因分析

RedisKVStoreget_allaget_all 方法(base.py 第 161 行和第 169 行)无条件调用了 key.decode()。当 Redis 客户端配置 decode_responses=True 时,返回的 key 已经是 Python 字符串,不再需要 .decode() 调用,从而引发此错误。这是一个已知的 BUG,且之前有修复(PR #12324)但被后续 PR #13201 回退。

环境排查

  • LlamaIndex 版本:0.14.22(此 BUG 在该版本中确认存在)
  • Redis 连接配置:是否在 ConnectionPool 中设置了 decode_responses=True
  • Python 版本:3.13(用户环境中出现)
  • 受影响文件:llama-index-integrations/storage/kvstore/llama-index-storage-kvstore-redis/llama_index/storage/kvstore/redis/base.py,第 156-170 行

解决步骤

  1. 优先尝试:移除 ConnectionPool 中的 decode_responses=True,或在连接 Redis 时不设置此参数(使用默认值 False)。
  2. 手动修复代码:编辑 base.py,将第 161 行和第 169 行的 collection_kv_dict[key.decode()] = value 替换为:
    collection_kv_dict[key.decode() if isinstance(key, bytes) else key] = value

    确保在 get_allaget_all 两个方法中都应用此修改。

  3. 等待上游修复:关注 LlamaIndex 官方仓库,该 BUG 已标记并正在修复中。

验证方法

修改后,重新运行触发报错的 pipeline.run() 或直接调用 docstore.get_all_document_hashes(),确认不再抛出 AttributeError。也可编写简单测试:用 decode_responses=True 连接 Redis,调用 kvstore.get_all(),应正常返回字典。

参考来源

run-llama/llama_index #22115

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 14735

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注