Skip to content

Commit

Permalink
fix: No context vars for async agents replies (#4640)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Sweet <[email protected]>
Co-authored-by: Eric Zhu <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2024
1 parent e90b9aa commit ee491c0
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import contextvars
import copy
import functools
import inspect
Expand Down Expand Up @@ -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
Expand All @@ -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
),
)
Expand Down

0 comments on commit ee491c0

Please sign in to comment.