Skip to content

Commit d114462

Browse files
Merge pull request #928 from MervinPraison/claude/issue-926-20250715_112808
refactor: inline Gemini internal tools detection logic
2 parents ae105a1 + 68428f2 commit d114462

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
ReflectionOutput,
1818
execute_sync_callback,
1919
)
20-
from .model_capabilities import is_gemini_internal_tool
2120
from rich.console import Console
2221
from rich.live import Live
2322

@@ -651,10 +650,14 @@ def _format_tools_for_litellm(self, tools: Optional[List[Any]]) -> Optional[List
651650
if tool_def:
652651
formatted_tools.append(tool_def)
653652
# Handle Gemini internal tools (e.g., {"googleSearch": {}}, {"urlContext": {}}, {"codeExecution": {}})
654-
elif is_gemini_internal_tool(tool):
653+
elif isinstance(tool, dict) and len(tool) == 1:
655654
tool_name = next(iter(tool.keys()))
656-
logging.debug(f"Using Gemini internal tool: {tool_name}")
657-
formatted_tools.append(tool)
655+
gemini_internal_tools = {'googleSearch', 'urlContext', 'codeExecution'}
656+
if tool_name in gemini_internal_tools:
657+
logging.debug(f"Using Gemini internal tool: {tool_name}")
658+
formatted_tools.append(tool)
659+
else:
660+
logging.debug(f"Skipping unknown tool: {tool_name}")
658661
else:
659662
logging.debug(f"Skipping tool of unsupported type: {type(tool)}")
660663

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from rich.console import Console
1919
from rich.live import Live
2020
import inspect
21-
from .model_capabilities import is_gemini_internal_tool
2221

2322
# Constants
2423
LOCAL_SERVER_API_KEY_PLACEHOLDER = "not-needed"
@@ -407,10 +406,14 @@ def format_tools(self, tools: Optional[List[Any]]) -> Optional[List[Dict]]:
407406
if tool_def:
408407
formatted_tools.append(tool_def)
409408
# Handle Gemini internal tools (e.g., {"googleSearch": {}}, {"urlContext": {}}, {"codeExecution": {}})
410-
elif is_gemini_internal_tool(tool):
409+
elif isinstance(tool, dict) and len(tool) == 1:
411410
tool_name = next(iter(tool.keys()))
412-
logging.debug(f"Using Gemini internal tool: {tool_name}")
413-
formatted_tools.append(tool)
411+
gemini_internal_tools = {'googleSearch', 'urlContext', 'codeExecution'}
412+
if tool_name in gemini_internal_tools:
413+
logging.debug(f"Using Gemini internal tool: {tool_name}")
414+
formatted_tools.append(tool)
415+
else:
416+
logging.debug(f"Skipping unknown tool: {tool_name}")
414417
else:
415418
logging.debug(f"Skipping tool of unsupported type: {type(tool)}")
416419

test_gemini_tools.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
"""Test script to verify the refactored Gemini tools logic."""
3+
4+
import logging
5+
import sys
6+
import os
7+
8+
# Setup logging to see debug messages
9+
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s: %(message)s')
10+
11+
# Add the src directory to the Python path
12+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents'))
13+
14+
try:
15+
from praisonaiagents.llm.llm import LLM
16+
from praisonaiagents.llm.openai_client import OpenAIClient
17+
18+
print("Testing LLM tool formatting...")
19+
llm = LLM(model="gpt-4o-mini")
20+
tools = [
21+
{'googleSearch': {}}, # Valid Gemini tool
22+
{'urlContext': {}}, # Valid Gemini tool
23+
{'codeExecution': {}}, # Valid Gemini tool
24+
{'unknown': {}} # Invalid tool - should be skipped
25+
]
26+
27+
formatted = llm._format_tools_for_litellm(tools)
28+
print(f"LLM formatted tools ({len(formatted)} tools):", formatted)
29+
30+
print("\nTesting OpenAI client tool formatting...")
31+
client = OpenAIClient(api_key="not-needed")
32+
formatted = client.format_tools(tools)
33+
print(f"OpenAI client formatted tools ({len(formatted)} tools):", formatted)
34+
35+
print("\nTest completed successfully!")
36+
37+
except Exception as e:
38+
import traceback
39+
print(f"Error: {e}")
40+
traceback.print_exc()

0 commit comments

Comments
 (0)