Skip to content

Commit dd44357

Browse files
fix: tool register (#3175)
Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
1 parent 1066024 commit dd44357

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

camel/agents/chat_agent.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,35 @@ def _handle_response_format_with_non_strict_tools(
13941394
# and True to indicate we used prompt formatting
13951395
return modified_message, None, True
13961396

1397+
def _is_called_from_registered_toolkit(self) -> bool:
1398+
r"""Check if current step/astep call originates from a
1399+
RegisteredAgentToolkit.
1400+
1401+
This method uses stack inspection to detect if the current call
1402+
is originating from a toolkit that inherits from
1403+
RegisteredAgentToolkit. When detected, tools should be disabled to
1404+
prevent recursive calls.
1405+
1406+
Returns:
1407+
bool: True if called from a RegisteredAgentToolkit, False otherwise
1408+
"""
1409+
import inspect
1410+
1411+
from camel.toolkits.base import RegisteredAgentToolkit
1412+
1413+
try:
1414+
for frame_info in inspect.stack():
1415+
frame_locals = frame_info.frame.f_locals
1416+
if 'self' in frame_locals:
1417+
caller_self = frame_locals['self']
1418+
if isinstance(caller_self, RegisteredAgentToolkit):
1419+
return True
1420+
1421+
except Exception:
1422+
return False
1423+
1424+
return False
1425+
13971426
def _apply_prompt_based_parsing(
13981427
self,
13991428
response: ModelResponse,
@@ -1587,6 +1616,10 @@ def _step_impl(
15871616
except ImportError:
15881617
pass # Langfuse not available
15891618

1619+
# Check if this call is from a RegisteredAgentToolkit to prevent tool
1620+
# use
1621+
disable_tools = self._is_called_from_registered_toolkit()
1622+
15901623
# Handle response format compatibility with non-strict tools
15911624
original_response_format = response_format
15921625
input_message, response_format, used_prompt_formatting = (
@@ -1634,7 +1667,9 @@ def _step_impl(
16341667
num_tokens=num_tokens,
16351668
current_iteration=iteration_count,
16361669
response_format=response_format,
1637-
tool_schemas=self._get_full_tool_schemas(),
1670+
tool_schemas=[]
1671+
if disable_tools
1672+
else self._get_full_tool_schemas(),
16381673
prev_num_openai_messages=prev_num_openai_messages,
16391674
)
16401675
prev_num_openai_messages = len(openai_messages)
@@ -1799,6 +1834,10 @@ async def _astep_non_streaming_task(
17991834
except ImportError:
18001835
pass # Langfuse not available
18011836

1837+
# Check if this call is from a RegisteredAgentToolkit to prevent tool
1838+
# use
1839+
disable_tools = self._is_called_from_registered_toolkit()
1840+
18021841
# Handle response format compatibility with non-strict tools
18031842
original_response_format = response_format
18041843
input_message, response_format, used_prompt_formatting = (
@@ -1839,7 +1878,9 @@ async def _astep_non_streaming_task(
18391878
num_tokens=num_tokens,
18401879
current_iteration=iteration_count,
18411880
response_format=response_format,
1842-
tool_schemas=self._get_full_tool_schemas(),
1881+
tool_schemas=[]
1882+
if disable_tools
1883+
else self._get_full_tool_schemas(),
18431884
prev_num_openai_messages=prev_num_openai_messages,
18441885
)
18451886
prev_num_openai_messages = len(openai_messages)

0 commit comments

Comments
 (0)