@@ -246,7 +246,10 @@ The `UserInfoMemory` class below contains the following behavior:
246246
247247``` python
248248
249- from agent_framework import ContextProvider, Context, InvokedContext, InvokingContext, ChatAgent, ChatClientProtocol
249+ from collections.abc import MutableSequence, Sequence
250+ from typing import Any
251+
252+ from agent_framework import ContextProvider, Context, ChatAgent, ChatClientProtocol, ChatMessage, ChatOptions
250253
251254
252255class UserInfoMemory (ContextProvider ):
@@ -255,7 +258,6 @@ class UserInfoMemory(ContextProvider):
255258
256259 If you pass in kwargs, they will be attempted to be used to create a UserInfo object.
257260 """
258-
259261 self ._chat_client = chat_client
260262 if user_info:
261263 self .user_info = user_info
@@ -272,22 +274,28 @@ class UserInfoMemory(ContextProvider):
272274 ** kwargs : Any,
273275 ) -> None :
274276 """ Extract user information from messages after each agent call."""
277+ # Ensure request_messages is a list
278+ messages_list = [request_messages] if isinstance (request_messages, ChatMessage) else list (request_messages)
279+
275280 # Check if we need to extract user info from user messages
276- user_messages = [msg for msg in request_messages if hasattr (msg, " role " ) and msg.role.value == " user" ]
281+ user_messages = [msg for msg in messages_list if msg.role.value == " user" ]
277282
278283 if (self .user_info.name is None or self .user_info.age is None ) and user_messages:
279284 try :
280285 # Use the chat client to extract structured information
281286 result = await self ._chat_client.get_response(
282- messages = request_messages ,
287+ messages = messages_list ,
283288 chat_options = ChatOptions(
284- instructions = " Extract the user's name and age from the message if present. If not present return nulls." ,
289+ instructions = (
290+ " Extract the user's name and age from the message if present. "
291+ " If not present return nulls."
292+ ),
285293 response_format = UserInfo,
286294 ),
287295 )
288296
289297 # Update user info with extracted data
290- if result.value:
298+ if result.value and isinstance (result.value, UserInfo) :
291299 if self .user_info.name is None and result.value.name:
292300 self .user_info.name = result.value.name
293301 if self .user_info.age is None and result.value.age:
@@ -336,7 +344,7 @@ from azure.identity.aio import AzureCliCredential
336344
337345async def main ():
338346 async with AzureCliCredential() as credential:
339- chat_client = AzureAIAgentClient(async_credential = credential)
347+ chat_client = AzureAIAgentClient(credential = credential)
340348
341349 # Create the memory provider
342350 memory_provider = UserInfoMemory(chat_client)
@@ -355,11 +363,12 @@ async def main():
355363 print (await agent.run(" I am 20 years old" , thread = thread))
356364
357365 # Access the memory component via the thread's context_providers attribute and inspect the memories
358- user_info_memory = thread.context_provider.providers[0 ]
359- if user_info_memory:
360- print ()
361- print (f " MEMORY - User Name: { user_info_memory.user_info.name} " )
362- print (f " MEMORY - User Age: { user_info_memory.user_info.age} " )
366+ if thread.context_provider:
367+ user_info_memory = thread.context_provider.providers[0 ]
368+ if isinstance (user_info_memory, UserInfoMemory):
369+ print ()
370+ print (f " MEMORY - User Name: { user_info_memory.user_info.name} " )
371+ print (f " MEMORY - User Age: { user_info_memory.user_info.age} " )
363372
364373
365374if __name__ == " __main__" :
0 commit comments