feat: Enhance tool adapters to support asynchronous execution

- Updated LangGraphToolAdapter and OpenAIAgentToolAdapter to handle asynchronous tool execution by checking if the output is awaitable.
- Introduced `inspect` import to facilitate the awaitability check.
- Refactored tool wrapper functions to ensure proper handling of both synchronous and asynchronous tool results.
This commit is contained in:
lorenzejay
2025-04-16 12:25:17 -07:00
parent 5a0c636629
commit 95af240974
2 changed files with 26 additions and 10 deletions

View File

@@ -1,3 +1,5 @@
import inspect
import asyncio
from typing import Any, List, Optional
from crewai.agents.agent_adapters.base_tool_adapter import BaseToolAdapter
@@ -28,18 +30,25 @@ class LangGraphToolAdapter(BaseToolAdapter):
converted_tools.append(tool)
continue
def tool_wrapper(*args, tool=tool, **kwargs):
if len(args) > 0 and isinstance(args[0], str):
return tool.run(args[0])
elif "input" in kwargs:
return tool.run(kwargs["input"])
else:
return tool.run(**kwargs)
sanitized_name = self.sanitize_tool_name(tool.name)
sanitized_tool_name = self.sanitize_tool_name(tool.name)
async def tool_wrapper(*args, tool=tool, **kwargs):
output = None
if len(args) > 0 and isinstance(args[0], str):
output = tool.run(args[0])
elif "input" in kwargs:
output = tool.run(kwargs["input"])
else:
output = tool.run(**kwargs)
if inspect.isawaitable(output):
result = await output
else:
result = output
return result
converted_tool = StructuredTool(
name=sanitized_tool_name,
name=sanitized_name,
description=tool.description,
func=tool_wrapper,
args_schema=tool.args_schema,

View File

@@ -1,3 +1,4 @@
import inspect
from typing import Any, List, Optional
from agents import FunctionTool, Tool
@@ -58,7 +59,13 @@ class OpenAIAgentToolAdapter(BaseToolAdapter):
args_dict = {param_name: str(arguments)}
# Run the tool with the processed arguments
result = tool._run(**args_dict)
output = tool._run(**args_dict)
# Await if the tool returned a coroutine
if inspect.isawaitable(output):
result = await output
else:
result = output
# Ensure the result is JSON serializable
if isinstance(result, (dict, list, str, int, float, bool, type(None))):