Skip to content

Commit db5f2e0

Browse files
jona62Jonathan James
andauthored
fix: allow optional strategies on create memory (#225)
Co-authored-by: Jonathan James <[email protected]>
1 parent 6ea350f commit db5f2e0

File tree

2 files changed

+118
-35
lines changed

2 files changed

+118
-35
lines changed

src/bedrock_agentcore_starter_toolkit/operations/memory/manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def _create_memory(
235235
def _create_memory_and_wait(
236236
self,
237237
name: str,
238-
strategies: List[Dict[str, Any]],
238+
strategies: Optional[List[Dict[str, Any]]],
239239
description: Optional[str] = None,
240240
event_expiry_days: int = 90,
241241
memory_execution_role_arn: Optional[str] = None,
@@ -309,7 +309,7 @@ def _create_memory_and_wait(
309309
def create_memory_and_wait(
310310
self,
311311
name: str,
312-
strategies: List[Union[BaseStrategy, Dict[str, Any]]],
312+
strategies: Optional[List[Union[BaseStrategy, Dict[str, Any]]]] = None,
313313
description: Optional[str] = None,
314314
event_expiry_days: int = 90,
315315
memory_execution_role_arn: Optional[str] = None,
@@ -358,7 +358,7 @@ def create_memory_and_wait(
358358
)
359359
"""
360360
# Convert typed strategies to dicts for internal processing
361-
dict_strategies = convert_strategies_to_dicts(strategies)
361+
dict_strategies = convert_strategies_to_dicts(strategies) if strategies else None
362362

363363
return self._create_memory_and_wait(
364364
name=name,

tests_integ/memory/memory-manager.ipynb

Lines changed: 115 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 3,
5+
"execution_count": 1,
66
"metadata": {
77
"ExecuteTime": {
88
"end_time": "2025-09-27T19:47:26.324483Z",
@@ -17,7 +17,7 @@
1717
},
1818
{
1919
"cell_type": "code",
20-
"execution_count": 4,
20+
"execution_count": null,
2121
"metadata": {
2222
"ExecuteTime": {
2323
"end_time": "2025-09-27T19:47:26.904721Z",
@@ -30,9 +30,37 @@
3030
"output_type": "stream",
3131
"text": [
3232
"\n",
33-
"========================= CONTROL PLANE DEMO =========================\n",
34-
"🔍 DEBUG: Starting control plane operations...\n",
35-
"🔍 DEBUG: Short-term memory created successfully\n"
33+
"========================= CONTROL PLANE DEMO =========================\n"
34+
]
35+
},
36+
{
37+
"name": "stderr",
38+
"output_type": "stream",
39+
"text": [
40+
"✅ MemoryManager initialized for region: None\n"
41+
]
42+
},
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"🔍 DEBUG: Starting control plane operations...\n"
48+
]
49+
},
50+
{
51+
"name": "stderr",
52+
"output_type": "stream",
53+
"text": [
54+
"Memory already exists. Using existing memory ID: DemoLongTermMemory1-AJRz1XDfS6\n",
55+
"🔎 Retrieving memory resource with ID: DemoLongTermMemory1-AJRz1XDfS6...\n",
56+
" ✅ Found memory: DemoLongTermMemory1-AJRz1XDfS6\n"
57+
]
58+
},
59+
{
60+
"name": "stdout",
61+
"output_type": "stream",
62+
"text": [
63+
"🔍 DEBUG: long-term memory created successfully\n"
3664
]
3765
},
3866
{
@@ -41,7 +69,7 @@
4169
"{'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/DemoLongTermMemory1-AJRz1XDfS6', 'id': 'DemoLongTermMemory1-AJRz1XDfS6', 'name': 'DemoLongTermMemory1', 'description': 'A temporary memory for short-lived conversations.', 'eventExpiryDuration': 90, 'status': 'ACTIVE', 'createdAt': datetime.datetime(2025, 9, 27, 15, 35, 20, 523000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 15, 44, 51, 65000, tzinfo=tzlocal()), 'strategies': [{'strategyId': 'SemanticStrategy-AYF3Xl8OoD', 'name': 'SemanticStrategy', 'description': 'A strategy for semantic understanding.', 'type': 'SEMANTIC', 'namespaces': ['support/user/{actorId}/{sessionId}'], 'createdAt': datetime.datetime(2025, 9, 27, 15, 35, 20, 523000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 15, 35, 20, 754000, tzinfo=tzlocal()), 'status': 'ACTIVE'}, {'strategyId': 'SummaryStrategy-o93FeU6Zdd', 'name': 'SummaryStrategy', 'description': 'A strategy for summarizing the conversation.', 'type': 'SUMMARIZATION', 'namespaces': ['support/user/{actorId}/{sessionId}'], 'createdAt': datetime.datetime(2025, 9, 27, 15, 44, 51, 175000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 15, 44, 51, 175000, tzinfo=tzlocal()), 'status': 'ACTIVE'}]}"
4270
]
4371
},
44-
"execution_count": 4,
72+
"execution_count": 2,
4573
"metadata": {},
4674
"output_type": "execute_result"
4775
}
@@ -64,25 +92,34 @@
6492
" )\n",
6593
" ],\n",
6694
")\n",
67-
"print(\"🔍 DEBUG: Short-term memory created successfully\")\n",
95+
"print(\"🔍 DEBUG: long-term memory created successfully\")\n",
6896
"memory1"
6997
]
7098
},
7199
{
72100
"cell_type": "code",
73-
"execution_count": 5,
101+
"execution_count": null,
74102
"metadata": {
75103
"ExecuteTime": {
76104
"end_time": "2025-09-27T19:47:27.197308Z",
77105
"start_time": "2025-09-27T19:47:26.910254Z"
78106
}
79107
},
80108
"outputs": [
109+
{
110+
"name": "stderr",
111+
"output_type": "stream",
112+
"text": [
113+
"Memory already exists. Using existing memory ID: DemoLongTermMemory2-O63lir7uK6\n",
114+
"🔎 Retrieving memory resource with ID: DemoLongTermMemory2-O63lir7uK6...\n",
115+
" ✅ Found memory: DemoLongTermMemory2-O63lir7uK6\n"
116+
]
117+
},
81118
{
82119
"name": "stdout",
83120
"output_type": "stream",
84121
"text": [
85-
"🔍 DEBUG: Short-term memory created successfully\n"
122+
"🔍 DEBUG: Long-term memory created successfully\n"
86123
]
87124
},
88125
{
@@ -91,7 +128,7 @@
91128
"{'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/DemoLongTermMemory2-O63lir7uK6', 'id': 'DemoLongTermMemory2-O63lir7uK6', 'name': 'DemoLongTermMemory2', 'description': 'A temporary memory for short-lived conversations.', 'eventExpiryDuration': 90, 'status': 'ACTIVE', 'createdAt': datetime.datetime(2025, 9, 27, 15, 38, 14, 362000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 15, 41, 9, 247000, tzinfo=tzlocal()), 'strategies': [{'strategyId': 'SummaryStrategy-j4HeHK8XCR', 'name': 'SummaryStrategy', 'description': 'A strategy for summarizing the conversation.', 'type': 'SUMMARIZATION', 'namespaces': ['support/user/{actorId}/{sessionId}'], 'createdAt': datetime.datetime(2025, 9, 27, 15, 41, 8, 278000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 15, 41, 8, 278000, tzinfo=tzlocal()), 'status': 'ACTIVE'}]}"
92129
]
93130
},
94-
"execution_count": 5,
131+
"execution_count": 3,
95132
"metadata": {},
96133
"output_type": "execute_result"
97134
}
@@ -100,7 +137,7 @@
100137
"# Memory creation using strategies without data classes\n",
101138
"memory2: Memory = manager.get_or_create_memory(\n",
102139
" name=\"DemoLongTermMemory2\",\n",
103-
" description=\"A temporary memory for short-lived conversations.\",\n",
140+
" description=\"A temporary memory for long-lived conversations.\",\n",
104141
" strategies=[\n",
105142
" {\n",
106143
" \"semanticMemoryStrategy\": {\n",
@@ -111,29 +148,38 @@
111148
" }\n",
112149
" ],\n",
113150
")\n",
114-
"print(\"🔍 DEBUG: Short-term memory created successfully\")\n",
151+
"print(\"🔍 DEBUG: Long-term memory created successfully\")\n",
115152
"memory2"
116153
]
117154
},
118155
{
119156
"cell_type": "code",
120-
"execution_count": 17,
157+
"execution_count": null,
121158
"metadata": {},
122159
"outputs": [
160+
{
161+
"name": "stderr",
162+
"output_type": "stream",
163+
"text": [
164+
"Memory already exists. Using existing memory ID: DemoLongTermMemory3-mP3Knz4YTX\n",
165+
"🔎 Retrieving memory resource with ID: DemoLongTermMemory3-mP3Knz4YTX...\n",
166+
" ✅ Found memory: DemoLongTermMemory3-mP3Knz4YTX\n"
167+
]
168+
},
123169
{
124170
"name": "stdout",
125171
"output_type": "stream",
126172
"text": [
127-
"🔍 DEBUG: Short-term memory created successfully\n"
173+
"🔍 DEBUG: Long-term memory created successfully\n"
128174
]
129175
},
130176
{
131177
"data": {
132178
"text/plain": [
133-
"{'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/DemoLongTermMemory3-mP3Knz4YTX', 'id': 'DemoLongTermMemory3-mP3Knz4YTX', 'name': 'DemoLongTermMemory3', 'description': 'A temporary memory for short-lived conversations.', 'eventExpiryDuration': 90, 'status': 'CREATING', 'createdAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'strategies': [{'strategyId': 'UserPreferenceStrategy-PMYICVDWE5', 'name': 'UserPreferenceStrategy', 'description': 'A strategy for user preference.', 'type': 'USER_PREFERENCE', 'namespaces': ['/strategies/{memoryStrategyId}/actors/{actorId}'], 'createdAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'status': 'CREATING'}]}"
179+
"{'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/DemoLongTermMemory3-mP3Knz4YTX', 'id': 'DemoLongTermMemory3-mP3Knz4YTX', 'name': 'DemoLongTermMemory3', 'description': 'A temporary memory for short-lived conversations.', 'eventExpiryDuration': 90, 'status': 'ACTIVE', 'createdAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 484000, tzinfo=tzlocal()), 'strategies': [{'strategyId': 'UserPreferenceStrategy-PMYICVDWE5', 'name': 'UserPreferenceStrategy', 'description': 'A strategy for user preference.', 'type': 'USER_PREFERENCE', 'namespaces': ['/strategies/{memoryStrategyId}/actors/{actorId}'], 'createdAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 484000, tzinfo=tzlocal()), 'status': 'ACTIVE'}]}"
134180
]
135181
},
136-
"execution_count": 17,
182+
"execution_count": 4,
137183
"metadata": {},
138184
"output_type": "execute_result"
139185
}
@@ -142,18 +188,17 @@
142188
"# Memory creation using strategies without data classes\n",
143189
"from bedrock_agentcore_starter_toolkit.operations.memory.models.strategies.user_preference import UserPreferenceStrategy\n",
144190
"\n",
145-
"\n",
146191
"memory3: Memory = manager.get_or_create_memory(\n",
147192
" name=\"DemoLongTermMemory3\",\n",
148-
" description=\"A temporary memory for short-lived conversations.\",\n",
193+
" description=\"A temporary memory for long-lived conversations.\",\n",
149194
" strategies=[\n",
150195
" UserPreferenceStrategy(\n",
151196
" name=\"UserPreferenceStrategy\",\n",
152197
" description=\"A strategy for user preference.\",\n",
153198
" )\n",
154199
" ],\n",
155200
")\n",
156-
"print(\"🔍 DEBUG: Short-term memory created successfully\")\n",
201+
"print(\"🔍 DEBUG: Long-term memory created successfully\")\n",
157202
"memory3"
158203
]
159204
},
@@ -168,7 +213,7 @@
168213
"[{'strategyId': 'UserPreferenceStrategy-PMYICVDWE5', 'name': 'UserPreferenceStrategy', 'description': 'A strategy for user preference.', 'type': 'USER_PREFERENCE', 'namespaces': ['/strategies/{memoryStrategyId}/actors/{actorId}'], 'createdAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 484000, tzinfo=tzlocal()), 'status': 'ACTIVE'}]"
169214
]
170215
},
171-
"execution_count": 18,
216+
"execution_count": 5,
172217
"metadata": {},
173218
"output_type": "execute_result"
174219
}
@@ -180,7 +225,7 @@
180225
},
181226
{
182227
"cell_type": "code",
183-
"execution_count": 6,
228+
"execution_count": null,
184229
"metadata": {
185230
"ExecuteTime": {
186231
"end_time": "2025-09-27T19:47:27.306295Z",
@@ -194,6 +239,7 @@
194239
"text": [
195240
"🔍 DEBUG: Memory found : {'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/DemoLongTermMemory1-AJRz1XDfS6', 'id': 'DemoLongTermMemory1-AJRz1XDfS6', 'status': 'ACTIVE', 'createdAt': datetime.datetime(2025, 9, 27, 15, 35, 20, 523000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 15, 44, 51, 65000, tzinfo=tzlocal()), 'memoryId': 'DemoLongTermMemory1-AJRz1XDfS6'}\n",
196241
"🔍 DEBUG: Memory found : {'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/DemoLongTermMemory2-O63lir7uK6', 'id': 'DemoLongTermMemory2-O63lir7uK6', 'status': 'ACTIVE', 'createdAt': datetime.datetime(2025, 9, 27, 15, 38, 14, 362000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 15, 41, 9, 247000, tzinfo=tzlocal()), 'memoryId': 'DemoLongTermMemory2-O63lir7uK6'}\n",
242+
"🔍 DEBUG: Memory found : {'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/DemoLongTermMemory3-mP3Knz4YTX', 'id': 'DemoLongTermMemory3-mP3Knz4YTX', 'status': 'ACTIVE', 'createdAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 264000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 29, 12, 8, 26, 484000, tzinfo=tzlocal()), 'memoryId': 'DemoLongTermMemory3-mP3Knz4YTX'}\n",
197243
"🔍 DEBUG: Memory found : {'arn': 'arn:aws:bedrock-agentcore:us-east-1:328307993871:memory/TypedMemory-BMWCYo4kT8', 'id': 'TypedMemory-BMWCYo4kT8', 'status': 'ACTIVE', 'createdAt': datetime.datetime(2025, 9, 27, 16, 25, 2, 264000, tzinfo=tzlocal()), 'updatedAt': datetime.datetime(2025, 9, 27, 16, 25, 3, 666000, tzinfo=tzlocal()), 'memoryId': 'TypedMemory-BMWCYo4kT8'}\n"
198244
]
199245
}
@@ -206,7 +252,7 @@
206252
},
207253
{
208254
"cell_type": "code",
209-
"execution_count": 7,
255+
"execution_count": null,
210256
"metadata": {
211257
"ExecuteTime": {
212258
"end_time": "2025-09-27T19:47:27.462204Z",
@@ -234,7 +280,7 @@
234280
},
235281
{
236282
"cell_type": "code",
237-
"execution_count": 8,
283+
"execution_count": null,
238284
"metadata": {
239285
"ExecuteTime": {
240286
"end_time": "2025-09-27T19:47:27.468260Z",
@@ -267,7 +313,7 @@
267313
},
268314
{
269315
"cell_type": "code",
270-
"execution_count": 9,
316+
"execution_count": null,
271317
"metadata": {
272318
"ExecuteTime": {
273319
"end_time": "2025-09-27T19:47:27.477723Z",
@@ -293,22 +339,31 @@
293339
},
294340
{
295341
"cell_type": "code",
296-
"execution_count": 10,
342+
"execution_count": null,
297343
"metadata": {
298344
"ExecuteTime": {
299345
"end_time": "2025-09-27T19:47:27.636202Z",
300346
"start_time": "2025-09-27T19:47:27.486734Z"
301347
}
302348
},
303-
"outputs": [],
349+
"outputs": [
350+
{
351+
"name": "stderr",
352+
"output_type": "stream",
353+
"text": [
354+
"🔎 Retrieving memory resource with ID: DemoLongTermMemory1-AJRz1XDfS6...\n",
355+
" ✅ Found memory: DemoLongTermMemory1-AJRz1XDfS6\n"
356+
]
357+
}
358+
],
304359
"source": [
305360
"# Fetch the memory again to see the updated strategies\n",
306361
"get_response = manager.get_memory(memory_id=memory1.id)"
307362
]
308363
},
309364
{
310365
"cell_type": "code",
311-
"execution_count": 11,
366+
"execution_count": null,
312367
"metadata": {
313368
"ExecuteTime": {
314369
"end_time": "2025-09-27T19:47:27.644091Z",
@@ -333,7 +388,7 @@
333388
},
334389
{
335390
"cell_type": "code",
336-
"execution_count": 12,
391+
"execution_count": null,
337392
"metadata": {
338393
"ExecuteTime": {
339394
"end_time": "2025-09-27T19:47:27.813467Z",
@@ -359,7 +414,7 @@
359414
},
360415
{
361416
"cell_type": "code",
362-
"execution_count": 13,
417+
"execution_count": null,
363418
"metadata": {
364419
"ExecuteTime": {
365420
"end_time": "2025-09-27T19:47:27.923072Z",
@@ -371,6 +426,7 @@
371426
"name": "stdout",
372427
"output_type": "stream",
373428
"text": [
429+
"ACTIVE\n",
374430
"ACTIVE\n",
375431
"ACTIVE\n",
376432
"ACTIVE\n"
@@ -384,7 +440,7 @@
384440
},
385441
{
386442
"cell_type": "code",
387-
"execution_count": 14,
443+
"execution_count": null,
388444
"metadata": {
389445
"ExecuteTime": {
390446
"end_time": "2025-09-27T19:47:28.097043Z",
@@ -409,7 +465,7 @@
409465
},
410466
{
411467
"cell_type": "code",
412-
"execution_count": 15,
468+
"execution_count": null,
413469
"metadata": {
414470
"ExecuteTime": {
415471
"end_time": "2025-09-27T19:47:28.106744Z",
@@ -428,19 +484,46 @@
428484
},
429485
{
430486
"cell_type": "code",
431-
"execution_count": 16,
487+
"execution_count": null,
432488
"metadata": {
433489
"ExecuteTime": {
434490
"end_time": "2025-09-27T19:47:28.514982Z",
435491
"start_time": "2025-09-27T19:47:28.113961Z"
436492
}
437493
},
438494
"outputs": [
495+
{
496+
"name": "stderr",
497+
"output_type": "stream",
498+
"text": [
499+
"🔎 Retrieving memory resource with ID: DemoLongTermMemory1-AJRz1XDfS6...\n",
500+
" ✅ Found memory: DemoLongTermMemory1-AJRz1XDfS6\n",
501+
"🔎 Retrieving memory resource with ID: DemoLongTermMemory2-O63lir7uK6...\n",
502+
" ✅ Found memory: DemoLongTermMemory2-O63lir7uK6\n",
503+
"🔎 Retrieving memory resource with ID: DemoLongTermMemory3-mP3Knz4YTX...\n"
504+
]
505+
},
439506
{
440507
"name": "stdout",
441508
"output_type": "stream",
442509
"text": [
443510
"🔍 DEBUG: Memory found with status: ACTIVE\n",
511+
"🔍 DEBUG: Memory found with status: ACTIVE\n"
512+
]
513+
},
514+
{
515+
"name": "stderr",
516+
"output_type": "stream",
517+
"text": [
518+
" ✅ Found memory: DemoLongTermMemory3-mP3Knz4YTX\n",
519+
"🔎 Retrieving memory resource with ID: TypedMemory-BMWCYo4kT8...\n",
520+
" ✅ Found memory: TypedMemory-BMWCYo4kT8\n"
521+
]
522+
},
523+
{
524+
"name": "stdout",
525+
"output_type": "stream",
526+
"text": [
444527
"🔍 DEBUG: Memory found with status: ACTIVE\n",
445528
"🔍 DEBUG: Memory found with status: ACTIVE\n"
446529
]

0 commit comments

Comments
 (0)