
AttributeError: ‘str’ object has no attribute ‘decode’. Did you mean: ‘encode’?
快速结论:此报错发生在 LlamaIndex 的 RedisKVStore 中,当 Redis 客户端配置了 decode_responses=True 时触发。优先排查 Redis 连接池是否开启了自动解码,或代码中 base.py 的 get_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'?
原因分析
RedisKVStore 的 get_all 和 aget_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 行
解决步骤
- 优先尝试:移除
ConnectionPool中的decode_responses=True,或在连接 Redis 时不设置此参数(使用默认值False)。 - 手动修复代码:编辑
base.py,将第 161 行和第 169 行的collection_kv_dict[key.decode()] = value替换为:collection_kv_dict[key.decode() if isinstance(key, bytes) else key] = value确保在
get_all和aget_all两个方法中都应用此修改。 - 等待上游修复:关注 LlamaIndex 官方仓库,该 BUG 已标记并正在修复中。
验证方法
修改后,重新运行触发报错的 pipeline.run() 或直接调用 docstore.get_all_document_hashes(),确认不再抛出 AttributeError。也可编写简单测试:用 decode_responses=True 连接 Redis,调用 kvstore.get_all(),应正常返回字典。


![[Feature] Centralized Upload Validation (Extension Blacklist & Filename Security)](https://www.chat-gpts.plus/wp-content/uploads/2026/07/13443-2d0f3adc-768x403.jpg)
