Skip to content

Commit 2b6736e

Browse files
authored
fix: fix npe in memory session manager when messages have no text content (#293)
1 parent bb4a1b7 commit 2b6736e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/bedrock_agentcore/memory/integrations/strands/session_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ def append_message(self, message: Message, agent: "Agent", **kwargs: Any) -> Non
590590
**kwargs: Additional keyword arguments for future extensibility.
591591
"""
592592
created_message = self.create_message(self.session_id, agent.agent_id, SessionMessage.from_message(message, 0))
593+
if created_message is None:
594+
return
593595
session_message = SessionMessage.from_message(message, created_message.get("eventId"))
594596
self._latest_agent_message[agent.agent_id] = session_message
595597

tests/bedrock_agentcore/memory/integrations/strands/test_agentcore_memory_session_manager.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,58 @@ def test_list_messages_with_limit_calculates_max_results(self, session_manager,
12061206
call_kwargs = mock_memory_client.list_events.call_args[1]
12071207
assert call_kwargs["max_results"] == 550 # limit + offset
12081208

1209+
def test_append_message_handles_none_from_create_message(self, session_manager, test_agent):
1210+
"""Test that append_message gracefully handles None return from create_message."""
1211+
# Create a tool use message (no text content, only toolUse block)
1212+
tool_use_message = {
1213+
"role": "assistant",
1214+
"content": [
1215+
{
1216+
"toolUse": {
1217+
"toolUseId": "tooluse_abc123",
1218+
"name": "calculator",
1219+
"input": {"operation": "add", "a": 5, "b": 3},
1220+
}
1221+
}
1222+
],
1223+
}
1224+
1225+
# Mock create_message to return None (simulating the behavior for messages with no text)
1226+
session_manager.create_message = Mock(return_value=None)
1227+
session_manager._latest_agent_message = {}
1228+
1229+
# This should NOT crash - it should handle None gracefully
1230+
session_manager.append_message(tool_use_message, test_agent)
1231+
1232+
# Verify create_message was called
1233+
session_manager.create_message.assert_called_once()
1234+
1235+
# Verify that _latest_agent_message was NOT updated (since message was skipped)
1236+
assert test_agent.agent_id not in session_manager._latest_agent_message
1237+
1238+
def test_append_message_normal_message_still_works(self, session_manager, test_agent):
1239+
"""Test that append_message still works correctly for normal messages with text."""
1240+
# Create a normal message with text content
1241+
normal_message = {
1242+
"role": "assistant",
1243+
"content": [{"text": "The answer is 8."}],
1244+
}
1245+
1246+
# Mock create_message to return a valid event (normal behavior)
1247+
mock_event = {"eventId": "event_123456", "memoryId": "test-memory"}
1248+
session_manager.create_message = Mock(return_value=mock_event)
1249+
session_manager._latest_agent_message = {}
1250+
1251+
# This should work normally
1252+
session_manager.append_message(normal_message, test_agent)
1253+
1254+
# Verify create_message was called
1255+
session_manager.create_message.assert_called_once()
1256+
1257+
# Verify that _latest_agent_message WAS updated
1258+
assert test_agent.agent_id in session_manager._latest_agent_message
1259+
assert session_manager._latest_agent_message[test_agent.agent_id].message_id == "event_123456"
1260+
12091261

12101262
class TestBatchingConfig:
12111263
"""Test batch_size configuration validation."""

0 commit comments

Comments
 (0)