Skip to content

Commit 89122ef

Browse files
committed
feat(toolbox-langchain): Add context manager support for sync and async clients
1 parent 367a7b8 commit 89122ef

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,28 @@ def load_toolset(
190190
strict: bool = False,
191191
) -> list[AsyncToolboxTool]:
192192
raise NotImplementedError("Synchronous methods not supported by async client.")
193+
194+
async def close(self):
195+
"""Close the underlying synchronous client."""
196+
await self.__core_client.close()
197+
198+
async def __aenter__(self):
199+
"""
200+
Enter the runtime context related to this client instance.
201+
202+
Allows the client to be used as an asynchronous context manager
203+
(e.g., `async with AsyncToolboxClient(...) as client:`).
204+
205+
Returns:
206+
self: The client instance itself.
207+
"""
208+
return self
209+
210+
async def __aexit__(self, exc_type, exc_val, exc_tb):
211+
"""
212+
Exit the runtime context and close the internally managed session.
213+
214+
Allows the client to be used as an asynchronous context manager
215+
(e.g., `async with AsyncToolboxClient(...) as client:`).
216+
"""
217+
await self.close()

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,49 @@ def load_toolset(
291291
for core_sync_tool in core_sync_tools:
292292
tools.append(ToolboxTool(core_tool=core_sync_tool))
293293
return tools
294+
295+
def close(self):
296+
"""Close the underlying synchronous client."""
297+
self.__core_client.close()
298+
299+
async def __aenter__(self):
300+
"""
301+
Enter the runtime context related to this client instance.
302+
303+
Allows the client to be used as an asynchronous context manager
304+
(e.g., `async with ToolboxClient(...) as client:`).
305+
306+
Returns:
307+
self: The client instance itself.
308+
"""
309+
return self
310+
311+
async def __aexit__(self, exc_type, exc_val, exc_tb):
312+
"""
313+
Exit the runtime context and close the internally managed session.
314+
315+
Allows the client to be used as an asynchronous context manager
316+
(e.g., `async with ToolboxClient(...) as client:`).
317+
"""
318+
self.close()
319+
320+
def __enter__(self):
321+
"""
322+
Enter the runtime context related to this client instance.
323+
324+
Allows the client to be used as a context manager
325+
(e.g., `with ToolboxClient(...) as client:`).
326+
327+
Returns:
328+
self: The client instance itself.
329+
"""
330+
return self
331+
332+
def __exit__(self, exc_type, exc_val, exc_tb):
333+
"""
334+
Exit the runtime context and close the internally managed session.
335+
336+
Allows the client to be used as a context manager
337+
(e.g., `with ToolboxClient(...) as client:`).
338+
"""
339+
self.close()

0 commit comments

Comments
 (0)