From 69e94b83ee0969fbd723bf4623e02429f5a15a87 Mon Sep 17 00:00:00 2001 From: Shoubhit Dash Date: Mon, 27 Oct 2025 23:54:46 +0530 Subject: [PATCH] fix event loop management in sync wrapper --- .../src/supermemory_openai/wrapper.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/openai-sdk-python/src/supermemory_openai/wrapper.py b/packages/openai-sdk-python/src/supermemory_openai/wrapper.py index 10367fc0..8cd0d654 100644 --- a/packages/openai-sdk-python/src/supermemory_openai/wrapper.py +++ b/packages/openai-sdk-python/src/supermemory_openai/wrapper.py @@ -207,14 +207,18 @@ class SupermemorySyncChatCompletions: def create(self, **kwargs) -> ChatCompletion: """Create chat completion with memory integration.""" - # Run async method in event loop + # Use asyncio.run for safer event loop management try: - loop = asyncio.get_event_loop() + # Check if we're already in an event loop + asyncio.get_running_loop() + # If we reach here, we're in an async context - not supported + raise RuntimeError( + "Cannot use sync client from within an async context. " + "Use AsyncOpenAI with with_supermemory instead." + ) except RuntimeError: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - - return loop.run_until_complete(self.wrapper._process_chat_completion(**kwargs)) + # No running loop, safe to use asyncio.run + return asyncio.run(self.wrapper._process_chat_completion(**kwargs)) def __getattr__(self, name: str) -> Any: """Forward all other attributes to the original completions."""