OpenAIConverseConverter restores an empty history when one blob or text payload fails to parse

未关闭 适合新手
#751 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
ai

调研方向

从 src/bedrock_agentcore/memory/integrations/strands/converters/openai.py 中的 OpenAIConverseConverter.events_to_messages(L159-L186)开始,并阅读 session_manager.py L795-L814,了解为什么异常逃逸会清空历史记录。运行 issue 中基于 mock 的 MemoryClient 复现,确认三种失败的形状,然后在 AgentCoreMemoryConverter 中照搬 #749 的处理方式:跳过坏 payload,并且只记录 event id。当一个无法解析的 event 被跳过且有效历史仍能返回时即完成;在现有 converter 测试旁边添加覆盖。

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

描述

bug

Hi team, hope you're doing well!

This is a follow up from #743, where @kimnamu spotted it while testing #749. Thank you, @kimnamu!

Describe the bug

With converter=OpenAIConverseConverter, one stored event that the converter can't parse makes AgentCoreMemorySessionManager.list_messages return [], so the restored agent starts with no history, valid messages included. Three shapes trigger it on current main:

  • a dict blob, the shape in create_blob_event's docstring example (client.py L589 to L596)
  • the blob "[3, 4]"
  • deeply nested conversational text

#749 fixes the same kind of gap in AgentCoreMemoryConverter but doesn't touch converters/openai.py, so the branch behaves the same as main here.

To Reproduce

  1. list_messages passes every listed event to self.converter.events_to_messages and catches any exception by logging it and returning [] (session_manager.py L795 to L814).
  2. In OpenAIConverseConverter.events_to_messages (converters/openai.py L159 to L186):
    • a dict blob reaches json.loads(payload_item["blob"]), which raises TypeError, and the blob except only catches (json.JSONDecodeError, ValueError)
    • "[3, 4]" passes the list of length 2 check, then json.loads(blob_data[0]) gets 3 and raises TypeError
    • deeply nested text makes json.loads(conv["content"]["text"]) raise RecursionError, which the conversational except (json.JSONDecodeError, KeyError, ValueError) doesn't catch
  3. The exception escapes events_to_messages, list_messages returns [], and the agent starts with an empty history.

Minimal repro

Mocked MemoryClient, no AWS needed. Tested on main at 8467c9a (1.27.0) with strands-agents 1.56.0:

import json
from unittest.mock import patch

from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig
from bedrock_agentcore.memory.integrations.strands.converters.openai import OpenAIConverseConverter
from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager


def stored(role, text):
    return {"payload": [{"conversational": {"role": role.upper(), "content": {"text": json.dumps({"role": role, "content": text})}}}]}


config = AgentCoreMemoryConfig(memory_id="mem", session_id="sess", actor_id="actor")
with patch("bedrock_agentcore.memory.integrations.strands.session_manager.MemoryClient") as client, patch("boto3.Session"):
    client.return_value.list_events.return_value = []
    manager = AgentCoreMemorySessionManager(config, converter=OpenAIConverseConverter)

history = [stored("assistant", "hi there"), stored("user", "hello")]
odd = {
    "dict blob": {"payload": [{"blob": {"note": "x"}}]},
    "blob [3, 4]": {"payload": [{"blob": "[3, 4]"}]},
    "deeply nested text": {"payload": [{"conversational": {"role": "USER", "content": {"text": "[" * 100000 + "]" * 100000}}}]},
}

manager.memory_client.list_events.return_value = history
print("valid events only:", len(manager.list_messages("sess", "agent")))

for name, event in odd.items():
    manager.memory_client.list_events.return_value = [event] + history
    print(f"plus one {name}:", len(manager.list_messages("sess", "agent")))

Output:

valid events only: 2
Failed to list messages from AgentCore Memory: the JSON object must be str, bytes or bytearray, not dict
plus one dict blob: 0
Failed to list messages from AgentCore Memory: the JSON object must be str, bytes or bytearray, not int
plus one blob [3, 4]: 0
Failed to list messages from AgentCore Memory: maximum recursion depth exceeded while decoding a JSON array from a unicode string
plus one deeply nested text: 0

Expected behavior

The one payload that can't be parsed gets skipped with a log line, and the rest of the history is restored, the same way the converter already skips a conversational payload that isn't JSON.

Related: blob content in the ERROR log

The blob except logs the whole payload: logger.error("Failed to parse blob payload: %s", payload_item) (converters/openai.py L177). Once TypeError is caught there, dict blobs land on that line and their content goes into the ERROR log. #749 switched the matching line in AgentCoreMemoryConverter to log only the event id, logger.error("Failed to parse blob content in event %s", event.get("eventId")), and the same would work here.

Environment

  • bedrock-agentcore: main at 8467c9a (1.27.0)
  • strands-agents: 1.56.0
  • Python 3.10, macOS

Additional context

A possible fix: catch TypeError and RecursionError in both branches, log only the event id, and continue, mirroring #749. Does that sound right to you, or would you rather handle it differently? I'm happy to send a PR, or fold it into #749 if you'd prefer both converters in one place. Thanks so much!

主要语言
Python
星标
786
派生
163
平均合并
4 小时
30 天内合并 PR
22

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

aws/bedrock-agentcore-sdk-python 的其他 Issue

查看 aws/bedrock-agentcore-sdk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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