Skip to content

Commit 4559d55

Browse files
committed
fix: Fix running background asyncio in current loop issue
1 parent f04ae7b commit 4559d55

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

packages/toolbox-langchain/src/toolbox_langchain/async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def aload_toolset(
161161

162162
tools = []
163163
for core_tool in core_tools:
164-
tools.append(AsyncToolboxTool(core_tool_instance=core_tool))
164+
tools.append(AsyncToolboxTool(core_tool=core_tool))
165165
return tools
166166

167167
def load_tool(

packages/toolbox-langchain/src/toolbox_langchain/client.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,21 @@ async def aload_tool(
8484
)
8585
auth_token_getters = auth_tokens
8686

87-
core_tool = await self.__core_sync_client._ToolboxSyncClient__async_client.load_tool(
87+
coro = self.__core_sync_client._ToolboxSyncClient__async_client.load_tool(
8888
name=tool_name,
8989
auth_token_getters=auth_token_getters,
9090
bound_params=bound_params
9191
)
92+
93+
# If a loop has not been provided, attempt to run in current thread.
94+
if not self.__core_sync_client._ToolboxSyncClient__loop:
95+
return await coro
96+
97+
# Otherwise, run in the background thread.
98+
core_tool = await asyncio.wrap_future(
99+
asyncio.run_coroutine_threadsafe(coro, self.__core_sync_client._ToolboxSyncClient__loop)
100+
)
101+
92102
return ToolboxTool(core_tool=core_tool)
93103

94104
async def aload_toolset(
@@ -148,16 +158,25 @@ async def aload_toolset(
148158
)
149159
auth_token_getters = auth_tokens
150160

151-
core_tools = await self.__core_sync_client._ToolboxSyncClient__async_client.load_toolset(
161+
coro = self.__core_sync_client._ToolboxSyncClient__async_client.load_toolset(
152162
name=toolset_name,
153163
auth_token_getters=auth_token_getters,
154164
bound_params=bound_params,
155165
strict=strict
156166
)
157167

168+
# If a loop has not been provided, attempt to run in current thread.
169+
if not self.__core_sync_client._ToolboxSyncClient__loop:
170+
return await coro
171+
172+
# Otherwise, run in the background thread.
173+
core_tools = await asyncio.wrap_future(
174+
asyncio.run_coroutine_threadsafe(coro, self.__core_sync_client._ToolboxSyncClient__loop)
175+
)
176+
158177
tools = []
159178
for core_tool in core_tools:
160-
tools.append(ToolboxTool(core_tool_instance=core_tool))
179+
tools.append(ToolboxTool(core_tool=core_tool))
161180
return tools
162181

163182
def load_tool(
@@ -282,5 +301,5 @@ def load_toolset(
282301

283302
tools = []
284303
for core_tool in core_tools:
285-
tools.append(ToolboxTool(core_tool_instance=core_tool))
304+
tools.append(ToolboxTool(core_tool=core_tool))
286305
return tools

0 commit comments

Comments
 (0)