diff --git a/agents/ten_packages/extension/bingsearch_tool_python/extension.py b/agents/ten_packages/extension/bingsearch_tool_python/extension.py index 59d20305..515f24e4 100644 --- a/agents/ten_packages/extension/bingsearch_tool_python/extension.py +++ b/agents/ten_packages/extension/bingsearch_tool_python/extension.py @@ -98,7 +98,7 @@ async def on_stop(self, ten_env: AsyncTenEnv) -> None: ten_env.log_debug("on_stop") # TODO: clean up resources - if self.session: + if self.session and not self.session.closed: await self.session.close() self.session = None # Ensure it can't be reused accidentally @@ -145,7 +145,13 @@ async def _do_search(self, args: dict) -> Any: return snippets + async def _initialize_session(self): + if self.session is None or self.session.closed: + logger.debug("Initializing new session") + self.session = aiohttp.ClientSession() + async def _bing_search_results(self, search_term: str, count: int) -> List[dict]: + await self._initialize_session() headers = {"Ocp-Apim-Subscription-Key": self.api_key} params = { "q": search_term,