Skip to content

Commit 0741b52

Browse files
fix: Update tool formatting to handle callable functions from any module
- Modified _format_tools_for_litellm to pass function objects instead of names - Updated _generate_tool_definition to accept both function objects and names - This fixes test failures where functions weren't found in globals() Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 88ae3a2 commit 0741b52

File tree

1 file changed

+38
-30
lines changed
  • src/praisonai-agents/praisonaiagents/llm

1 file changed

+38
-30
lines changed

src/praisonai-agents/praisonaiagents/llm/llm.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def _format_tools_for_litellm(self, tools: Optional[List[Any]]) -> Optional[List
403403
else:
404404
logging.debug(f"Skipping malformed OpenAI tool in list: missing function or name")
405405
elif callable(tool):
406-
tool_def = self._generate_tool_definition(tool.__name__)
406+
tool_def = self._generate_tool_definition(tool)
407407
if tool_def:
408408
formatted_tools.append(tool_def)
409409
elif isinstance(tool, str):
@@ -2038,36 +2038,44 @@ async def aresponse(
20382038
display_error(f"Error in response_async: {str(error)}")
20392039
raise
20402040

2041-
def _generate_tool_definition(self, function_name: str) -> Optional[Dict]:
2042-
"""Generate a tool definition from a function name."""
2043-
logging.debug(f"Attempting to generate tool definition for: {function_name}")
2044-
2045-
# First try to get the tool definition if it exists
2046-
tool_def_name = f"{function_name}_definition"
2047-
tool_def = globals().get(tool_def_name)
2048-
logging.debug(f"Looking for {tool_def_name} in globals: {tool_def is not None}")
2049-
2050-
if not tool_def:
2051-
import __main__
2052-
tool_def = getattr(__main__, tool_def_name, None)
2053-
logging.debug(f"Looking for {tool_def_name} in __main__: {tool_def is not None}")
2054-
2055-
if tool_def:
2056-
logging.debug(f"Found tool definition: {tool_def}")
2057-
return tool_def
2041+
def _generate_tool_definition(self, function_or_name) -> Optional[Dict]:
2042+
"""Generate a tool definition from a function or function name."""
2043+
if callable(function_or_name):
2044+
# Function object passed directly
2045+
func = function_or_name
2046+
function_name = func.__name__
2047+
logging.debug(f"Generating tool definition for callable: {function_name}")
2048+
else:
2049+
# Function name string passed
2050+
function_name = function_or_name
2051+
logging.debug(f"Attempting to generate tool definition for: {function_name}")
2052+
2053+
# First try to get the tool definition if it exists
2054+
tool_def_name = f"{function_name}_definition"
2055+
tool_def = globals().get(tool_def_name)
2056+
logging.debug(f"Looking for {tool_def_name} in globals: {tool_def is not None}")
2057+
2058+
if not tool_def:
2059+
import __main__
2060+
tool_def = getattr(__main__, tool_def_name, None)
2061+
logging.debug(f"Looking for {tool_def_name} in __main__: {tool_def is not None}")
2062+
2063+
if tool_def:
2064+
logging.debug(f"Found tool definition: {tool_def}")
2065+
return tool_def
20582066

2059-
# Try to find the function
2060-
func = globals().get(function_name)
2061-
logging.debug(f"Looking for {function_name} in globals: {func is not None}")
2062-
2063-
if not func:
2064-
import __main__
2065-
func = getattr(__main__, function_name, None)
2066-
logging.debug(f"Looking for {function_name} in __main__: {func is not None}")
2067-
2068-
if not func or not callable(func):
2069-
logging.debug(f"Function {function_name} not found or not callable")
2070-
return None
2067+
# Try to find the function
2068+
func = globals().get(function_name)
2069+
logging.debug(f"Looking for {function_name} in globals: {func is not None}")
2070+
2071+
if not func:
2072+
import __main__
2073+
func = getattr(__main__, function_name, None)
2074+
logging.debug(f"Looking for {function_name} in __main__: {func is not None}")
2075+
2076+
if not func or not callable(func):
2077+
logging.debug(f"Function {function_name} not found or not callable")
2078+
return None
20712079

20722080
import inspect
20732081
# Handle Langchain and CrewAI tools

0 commit comments

Comments
 (0)