Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: No context vars for async agents replies #4640

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading