From 414496dda7efeb671abef8ab3415bcc941bbeef8 Mon Sep 17 00:00:00 2001 From: Giles Odigwe <79032838+giles17@users.noreply.github.com> Date: Mon, 16 Mar 2026 14:34:21 -0700 Subject: [PATCH] fix: Azure Redis sample missing session for history persistence (#4692) --- .../redis/azure_redis_conversation.py | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/python/samples/02-agents/context_providers/redis/azure_redis_conversation.py b/python/samples/02-agents/context_providers/redis/azure_redis_conversation.py index 6408fd4be4..5adbb53ef3 100644 --- a/python/samples/02-agents/context_providers/redis/azure_redis_conversation.py +++ b/python/samples/02-agents/context_providers/redis/azure_redis_conversation.py @@ -3,7 +3,11 @@ """Azure Managed Redis History Provider with Azure AD Authentication This example demonstrates how to use Azure Managed Redis with Azure AD authentication -to persist conversational details using RedisHistoryProvider. +to persist conversation history using RedisHistoryProvider. + +Key concepts: + - RedisHistoryProvider = durable storage (where messages are persisted) + - AgentSession = conversation identity (which conversation the messages belong to) Requirements: - Azure Managed Redis instance with Azure AD authentication enabled @@ -61,11 +65,11 @@ async def main() -> None: print("Get your Object ID from the Azure Portal") return - # Create Azure CLI credential provider (uses 'az login' credentials) + # 1. Create Azure CLI credential provider (uses 'az login' credentials) azure_credential = AsyncAzureCliCredential() credential_provider = AzureCredentialProvider(azure_credential, user_object_id) - # Create Azure Redis history provider + # 2. Create Azure Redis history provider (the durable storage backend) history_provider = RedisHistoryProvider( source_id="redis_memory", credential_provider=credential_provider, @@ -76,49 +80,54 @@ async def main() -> None: max_messages=100, ) - # Create chat client + # 3. Create chat client client = AzureOpenAIResponsesClient( project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"], credential=AzureCliCredential(), ) - # Create agent with Azure Redis history provider + # 4. Create agent with Azure Redis history provider agent = client.as_agent( name="AzureRedisAssistant", instructions="You are a helpful assistant.", context_providers=[history_provider], ) - # Conversation + # 5. Create a session to provide conversation identity. + # The session ID is used as the Redis key — all runs sharing the same session + # will read/write the same conversation history in Redis. + session = agent.create_session() + + # 6. Conversation — each run passes the same session for continuity query = "Remember that I enjoy gumbo" - result = await agent.run(query) + result = await agent.run(query, session=session) print("User: ", query) print("Agent: ", result) # Ask the agent to recall the stored preference; it should retrieve from memory query = "What do I enjoy?" - result = await agent.run(query) + result = await agent.run(query, session=session) print("User: ", query) print("Agent: ", result) query = "What did I say to you just now?" - result = await agent.run(query) + result = await agent.run(query, session=session) print("User: ", query) print("Agent: ", result) query = "Remember that I have a meeting at 3pm tomorrow" - result = await agent.run(query) + result = await agent.run(query, session=session) print("User: ", query) print("Agent: ", result) query = "Tulips are red" - result = await agent.run(query) + result = await agent.run(query, session=session) print("User: ", query) print("Agent: ", result) query = "What was the first thing I said to you this conversation?" - result = await agent.run(query) + result = await agent.run(query, session=session) print("User: ", query) print("Agent: ", result)