Skip to content

Commit 414496d

Browse files
authored
fix: Azure Redis sample missing session for history persistence (microsoft#4692)
1 parent 55011b7 commit 414496d

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

python/samples/02-agents/context_providers/redis/azure_redis_conversation.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"""Azure Managed Redis History Provider with Azure AD Authentication
44
55
This example demonstrates how to use Azure Managed Redis with Azure AD authentication
6-
to persist conversational details using RedisHistoryProvider.
6+
to persist conversation history using RedisHistoryProvider.
7+
8+
Key concepts:
9+
- RedisHistoryProvider = durable storage (where messages are persisted)
10+
- AgentSession = conversation identity (which conversation the messages belong to)
711
812
Requirements:
913
- Azure Managed Redis instance with Azure AD authentication enabled
@@ -61,11 +65,11 @@ async def main() -> None:
6165
print("Get your Object ID from the Azure Portal")
6266
return
6367

64-
# Create Azure CLI credential provider (uses 'az login' credentials)
68+
# 1. Create Azure CLI credential provider (uses 'az login' credentials)
6569
azure_credential = AsyncAzureCliCredential()
6670
credential_provider = AzureCredentialProvider(azure_credential, user_object_id)
6771

68-
# Create Azure Redis history provider
72+
# 2. Create Azure Redis history provider (the durable storage backend)
6973
history_provider = RedisHistoryProvider(
7074
source_id="redis_memory",
7175
credential_provider=credential_provider,
@@ -76,49 +80,54 @@ async def main() -> None:
7680
max_messages=100,
7781
)
7882

79-
# Create chat client
83+
# 3. Create chat client
8084
client = AzureOpenAIResponsesClient(
8185
project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
8286
deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"],
8387
credential=AzureCliCredential(),
8488
)
8589

86-
# Create agent with Azure Redis history provider
90+
# 4. Create agent with Azure Redis history provider
8791
agent = client.as_agent(
8892
name="AzureRedisAssistant",
8993
instructions="You are a helpful assistant.",
9094
context_providers=[history_provider],
9195
)
9296

93-
# Conversation
97+
# 5. Create a session to provide conversation identity.
98+
# The session ID is used as the Redis key — all runs sharing the same session
99+
# will read/write the same conversation history in Redis.
100+
session = agent.create_session()
101+
102+
# 6. Conversation — each run passes the same session for continuity
94103
query = "Remember that I enjoy gumbo"
95-
result = await agent.run(query)
104+
result = await agent.run(query, session=session)
96105
print("User: ", query)
97106
print("Agent: ", result)
98107

99108
# Ask the agent to recall the stored preference; it should retrieve from memory
100109
query = "What do I enjoy?"
101-
result = await agent.run(query)
110+
result = await agent.run(query, session=session)
102111
print("User: ", query)
103112
print("Agent: ", result)
104113

105114
query = "What did I say to you just now?"
106-
result = await agent.run(query)
115+
result = await agent.run(query, session=session)
107116
print("User: ", query)
108117
print("Agent: ", result)
109118

110119
query = "Remember that I have a meeting at 3pm tomorrow"
111-
result = await agent.run(query)
120+
result = await agent.run(query, session=session)
112121
print("User: ", query)
113122
print("Agent: ", result)
114123

115124
query = "Tulips are red"
116-
result = await agent.run(query)
125+
result = await agent.run(query, session=session)
117126
print("User: ", query)
118127
print("Agent: ", result)
119128

120129
query = "What was the first thing I said to you this conversation?"
121-
result = await agent.run(query)
130+
result = await agent.run(query, session=session)
122131
print("User: ", query)
123132
print("Agent: ", result)
124133

0 commit comments

Comments
 (0)