Skip to content

Commit 723943f

Browse files
authored
Update samples and CHANGELOG to enhance memory CRUD demonstrations an… (#47267)
* Update samples and CHANGELOG to enhance memory CRUD demonstrations and clean up test samples * Update assets.json tag and clean up skipped tests in test_samples_async.py * Update CHANGELOG to reflect self-contained dataset generation job samples and extend memory CRUD demonstrations
1 parent b74de7e commit 723943f

6 files changed

Lines changed: 123 additions & 43 deletions

File tree

sdk/ai/azure-ai-projects/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
### Sample updates
66

77
* Added Routines samples `sample_routines_crud.py` demonstrating CRUD operations and `sample_routines_with_timer_trigger.py` demonstrating triggering a routine by a timer.
8-
* `samples/datasets/sample_dataset_generation_job_traces_for_evaluation.py` and `samples/datasets/sample_dataset_generation_job_traces_for_finetuning.py` are now self-contained: each sample creates a temporary agent, seeds conversations, retries the data generation job over the trace window, and cleans up all created resources.
8+
* `sample_dataset_generation_job_traces_for_evaluation.py` and `sample_dataset_generation_job_traces_for_finetuning.py` are now self-contained: each sample creates a temporary agent, seeds conversations, retries the data generation job over the trace window, and cleans up all created resources.
9+
* Extended `sample_memory_crud.py` and `sample_memory_crud_async.py` to also demonstrate memory item CRUD (`create_memory`, `get_memory`, `update_memory`, `list_memories`, `delete_memory`) in addition to memory store CRUD.
910

1011
## 2.2.0 (2026-05-29)
1112

sdk/ai/azure-ai-projects/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/ai/azure-ai-projects",
5-
"Tag": "python/ai/azure-ai-projects_11a3786ca2"
5+
"Tag": "python/ai/azure-ai-projects_7a920efd23"
66
}

sdk/ai/azure-ai-projects/samples/memories/sample_memory_crud.py

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
"""
88
DESCRIPTION:
99
This sample demonstrates how to perform CRUD operations on a memory store
10-
using the synchronous AIProjectClient.
10+
and on the individual memory items inside it, using the synchronous
11+
AIProjectClient.
12+
13+
Memory store operations: create, get, update, list, delete.
14+
Memory item operations: create_memory, get_memory, update_memory,
15+
list_memories, delete_memory.
1116
1217
See also /samples/agents/tools/sample_agent_memory_search.py that shows
1318
how to use the Memory Search Tool in a prompt agent.
@@ -33,54 +38,91 @@
3338
from azure.core.exceptions import ResourceNotFoundError
3439
from azure.identity import DefaultAzureCredential
3540
from azure.ai.projects import AIProjectClient
36-
from azure.ai.projects.models import MemoryStoreDefaultDefinition
41+
from azure.ai.projects.models import MemoryItemKind, MemoryStoreDefaultDefinition
3742

3843
load_dotenv()
3944

4045
endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
46+
memory_store_name = "my_memory_store"
47+
scope = "user_123"
4148

4249
with (
4350
DefaultAzureCredential(exclude_interactive_browser_credential=False) as credential,
4451
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
4552
):
4653

4754
# Delete memory store, if it already exists
48-
memory_store_name = "my_memory_store"
4955
try:
5056
project_client.beta.memory_stores.delete(memory_store_name)
5157
print(f"Memory store `{memory_store_name}` deleted")
5258
except ResourceNotFoundError:
5359
pass
5460

55-
# Create Memory Store
56-
definition = MemoryStoreDefaultDefinition(
57-
chat_model=os.environ["MEMORY_STORE_CHAT_MODEL_DEPLOYMENT_NAME"],
58-
embedding_model=os.environ["MEMORY_STORE_EMBEDDING_MODEL_DEPLOYMENT_NAME"],
59-
)
61+
# Create memory store
6062
memory_store = project_client.beta.memory_stores.create(
6163
name=memory_store_name,
6264
description="Example memory store for conversations",
63-
definition=definition,
65+
definition=MemoryStoreDefaultDefinition(
66+
chat_model=os.environ["MEMORY_STORE_CHAT_MODEL_DEPLOYMENT_NAME"],
67+
embedding_model=os.environ["MEMORY_STORE_EMBEDDING_MODEL_DEPLOYMENT_NAME"],
68+
),
6469
)
6570
print(f"Created memory store: {memory_store.name} ({memory_store.id}): {memory_store.description}")
6671

67-
# Get Memory Store
72+
# Get memory store
6873
get_store = project_client.beta.memory_stores.get(memory_store.name)
6974
print(f"Retrieved: {get_store.name} ({get_store.id}): {get_store.description}")
7075

71-
# Update Memory Store
76+
# Update memory store
7277
updated_store = project_client.beta.memory_stores.update(
7378
name=memory_store.name,
7479
description="Updated description",
7580
)
7681
print(f"Updated: {updated_store.name} ({updated_store.id}): {updated_store.description}")
7782

78-
# List Memory Store
83+
# List memory stores
7984
memory_stores = list(project_client.beta.memory_stores.list(limit=10))
8085
print(f"Found {len(memory_stores)} memory stores")
8186
for store in memory_stores:
8287
print(f" - {store.name} ({store.id}): {store.description}")
8388

84-
# Delete Memory Store
89+
# Create a memory item
90+
created_item = project_client.beta.memory_stores.create_memory(
91+
memory_store.name,
92+
scope=scope,
93+
content="The user prefers responses in concise bullet points.",
94+
kind=MemoryItemKind.USER_PROFILE,
95+
)
96+
print(f"Created memory item: {created_item.memory_id} (kind={created_item.kind}) -> {created_item.content}")
97+
98+
# Get the memory item
99+
fetched_item = project_client.beta.memory_stores.get_memory(memory_store.name, created_item.memory_id)
100+
print(f"Retrieved memory item: {fetched_item.memory_id} -> {fetched_item.content}")
101+
102+
# Update the memory item
103+
updated_item = project_client.beta.memory_stores.update_memory(
104+
memory_store.name,
105+
fetched_item.memory_id,
106+
content="The user prefers concise bullet points and Python code samples.",
107+
)
108+
print(f"Updated memory item: {updated_item.memory_id} -> {updated_item.content}")
109+
110+
# Add a second memory item, then list items in the scope
111+
project_client.beta.memory_stores.create_memory(
112+
memory_store.name,
113+
scope=scope,
114+
content="The user is working on the azure-ai-projects Python SDK.",
115+
kind=MemoryItemKind.USER_PROFILE,
116+
)
117+
items = list(project_client.beta.memory_stores.list_memories(memory_store.name, scope=scope, limit=10))
118+
print(f"Found {len(items)} memory items in scope '{scope}':")
119+
for item in items:
120+
print(f" - {item.memory_id} ({item.kind}): {item.content}")
121+
122+
# Delete a memory item
123+
delete_item_result = project_client.beta.memory_stores.delete_memory(memory_store.name, created_item.memory_id)
124+
print(f"Deleted memory item {created_item.memory_id}: deleted={delete_item_result.deleted}")
125+
126+
# Delete the memory store
85127
delete_response = project_client.beta.memory_stores.delete(memory_store.name)
86-
print(f"Deleted: {delete_response.deleted}")
128+
print(f"Deleted memory store: {delete_response.deleted}")

sdk/ai/azure-ai-projects/samples/memories/sample_memory_crud_async.py

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
"""
88
DESCRIPTION:
99
This sample demonstrates how to perform CRUD operations on a memory store
10-
using the asynchronous AIProjectClient.
10+
and on the individual memory items inside it, using the asynchronous
11+
AIProjectClient.
12+
13+
Memory store operations: create, get, update, list, delete.
14+
Memory item operations: create_memory, get_memory, update_memory,
15+
list_memories, delete_memory.
1116
1217
See also /samples/agents/tools/sample_agent_memory_search_async.py that shows
1318
how to use the Memory Search Tool in a prompt agent.
@@ -34,62 +39,101 @@
3439
from azure.core.exceptions import ResourceNotFoundError
3540
from azure.identity.aio import DefaultAzureCredential
3641
from azure.ai.projects.aio import AIProjectClient
37-
from azure.ai.projects.models import MemoryStoreDefaultDefinition
38-
39-
load_dotenv()
42+
from azure.ai.projects.models import MemoryItemKind, MemoryStoreDefaultDefinition
4043

4144

4245
async def main() -> None:
46+
load_dotenv()
4347

4448
endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
49+
memory_store_name = "my_memory_store"
50+
scope = "user_123"
4551

4652
async with (
4753
DefaultAzureCredential() as credential,
4854
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
4955
):
5056

5157
# Delete memory store, if it already exists
52-
memory_store_name = "my_memory_store"
5358
try:
5459
await project_client.beta.memory_stores.delete(memory_store_name)
5560
print(f"Memory store `{memory_store_name}` deleted")
5661
except ResourceNotFoundError:
5762
pass
5863

59-
# Create Memory Store
60-
definition = MemoryStoreDefaultDefinition(
61-
chat_model=os.environ["MEMORY_STORE_CHAT_MODEL_DEPLOYMENT_NAME"],
62-
embedding_model=os.environ["MEMORY_STORE_EMBEDDING_MODEL_DEPLOYMENT_NAME"],
63-
)
64+
# Create memory store
6465
memory_store = await project_client.beta.memory_stores.create(
6566
name=memory_store_name,
6667
description="Example memory store for conversations",
67-
definition=definition,
68+
definition=MemoryStoreDefaultDefinition(
69+
chat_model=os.environ["MEMORY_STORE_CHAT_MODEL_DEPLOYMENT_NAME"],
70+
embedding_model=os.environ["MEMORY_STORE_EMBEDDING_MODEL_DEPLOYMENT_NAME"],
71+
),
6872
)
6973
print(f"Created memory store: {memory_store.name} ({memory_store.id}): {memory_store.description}")
7074

71-
# Get Memory Store
75+
# Get memory store
7276
get_store = await project_client.beta.memory_stores.get(memory_store.name)
7377
print(f"Retrieved: {get_store.name} ({get_store.id}): {get_store.description}")
7478

75-
# Update Memory Store
79+
# Update memory store
7680
updated_store = await project_client.beta.memory_stores.update(
7781
name=memory_store.name,
7882
description="Updated description",
7983
)
8084
print(f"Updated: {updated_store.name} ({updated_store.id}): {updated_store.description}")
8185

82-
# List Memory Store
83-
memory_stores = []
84-
async for store in project_client.beta.memory_stores.list(limit=10):
85-
memory_stores.append(store)
86+
# List memory stores
87+
memory_stores = [store async for store in project_client.beta.memory_stores.list(limit=10)]
8688
print(f"Found {len(memory_stores)} memory stores")
8789
for store in memory_stores:
8890
print(f" - {store.name} ({store.id}): {store.description}")
8991

90-
# Delete Memory Store
92+
# Create a memory item
93+
created_item = await project_client.beta.memory_stores.create_memory(
94+
memory_store.name,
95+
scope=scope,
96+
content="The user prefers responses in concise bullet points.",
97+
kind=MemoryItemKind.USER_PROFILE,
98+
)
99+
print(f"Created memory item: {created_item.memory_id} (kind={created_item.kind}) -> {created_item.content}")
100+
101+
# Get the memory item
102+
fetched_item = await project_client.beta.memory_stores.get_memory(memory_store.name, created_item.memory_id)
103+
print(f"Retrieved memory item: {fetched_item.memory_id} -> {fetched_item.content}")
104+
105+
# Update the memory item
106+
updated_item = await project_client.beta.memory_stores.update_memory(
107+
memory_store.name,
108+
fetched_item.memory_id,
109+
content="The user prefers concise bullet points and Python code samples.",
110+
)
111+
print(f"Updated memory item: {updated_item.memory_id} -> {updated_item.content}")
112+
113+
# Add a second memory item, then list items in the scope
114+
await project_client.beta.memory_stores.create_memory(
115+
memory_store.name,
116+
scope=scope,
117+
content="The user is working on the azure-ai-projects Python SDK.",
118+
kind=MemoryItemKind.USER_PROFILE,
119+
)
120+
items = [
121+
item
122+
async for item in project_client.beta.memory_stores.list_memories(memory_store.name, scope=scope, limit=10)
123+
]
124+
print(f"Found {len(items)} memory items in scope '{scope}':")
125+
for item in items:
126+
print(f" - {item.memory_id} ({item.kind}): {item.content}")
127+
128+
# Delete a memory item
129+
delete_item_result = await project_client.beta.memory_stores.delete_memory(
130+
memory_store.name, created_item.memory_id
131+
)
132+
print(f"Deleted memory item {created_item.memory_id}: deleted={delete_item_result.deleted}")
133+
134+
# Delete the memory store
91135
delete_response = await project_client.beta.memory_stores.delete(memory_store.name)
92-
print(f"Deleted: {delete_response.deleted}")
136+
print(f"Deleted memory store: {delete_response.deleted}")
93137

94138

95139
if __name__ == "__main__":

sdk/ai/azure-ai-projects/tests/samples/test_samples.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ def test_agent_tools_samples(self, sample_path: str, **kwargs) -> None:
6363
"sample_path",
6464
get_sample_paths(
6565
"memories",
66-
samples_to_skip=[
67-
"sample_memory_advanced.py",
68-
"sample_memory_basic.py",
69-
"sample_memory_crud.py", # Sample works fine. But AI thinks something is wrong.
70-
],
66+
samples_to_skip=[],
7167
),
7268
)
7369
@servicePreparer()

sdk/ai/azure-ai-projects/tests/samples/test_samples_async.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ async def test_agent_tools_samples_async(self, sample_path: str, **kwargs) -> No
5151
get_async_sample_paths(
5252
"memories",
5353
samples_to_skip=[
54-
"sample_memory_advanced_async.py",
55-
"sample_memory_basic_async.py",
56-
"sample_memory_crud_async.py", # Skipped until re-enabled and recorded on Foundry endpoint that supports the new versioning schema
5754
],
5855
),
5956
)

0 commit comments

Comments
 (0)