From 61b70b94782b46668aaf3b18e8f8b37e5d6421f3 Mon Sep 17 00:00:00 2001 From: Grigorii Kurushin Date: Tue, 10 Dec 2024 14:34:13 +0300 Subject: [PATCH] fix: No context vars for async agents replies --- autogen/agentchat/conversable_agent.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 3a21c0f7d0e..084e93c0642 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -1,4 +1,5 @@ import asyncio +import contextvars import copy import functools import inspect @@ -1486,6 +1487,7 @@ async def a_generate_oai_reply( ) -> Tuple[bool, Union[str, Dict, None]]: """Generate a reply using autogen.oai asynchronously.""" iostream = IOStream.get_default() + parent_context = contextvars.copy_context() def _generate_oai_reply( self, iostream: IOStream, *args: Any, **kwargs: Any @@ -1495,7 +1497,7 @@ def _generate_oai_reply( return await asyncio.get_event_loop().run_in_executor( None, - functools.partial( + lambda: parent_context.run( _generate_oai_reply, self=self, iostream=iostream, messages=messages, sender=sender, config=config ), )