From ee491c0854eef287be3de041a531ee4b964eb029 Mon Sep 17 00:00:00 2001 From: Grigoriy Kurushin <72713036+violonistahiles@users.noreply.github.com> Date: Sun, 15 Dec 2024 02:20:10 +0300 Subject: [PATCH] fix: No context vars for async agents replies (#4640) Co-authored-by: Ryan Sweet Co-authored-by: Eric Zhu --- 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 3a21c0f7d0e4..084e93c06427 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 ), )