π OpenHands: AI-Driven Development
Fix unsupported MCP tools param (#8610)
Engel Nystc26ef180f266
diff --git a/openhands/agenthub/codeact_agent/codeact_agent.py b/openhands/agenthub/codeact_agent/codeact_agent.pyindex b33586139c..6db7ca272c 100644--- a/openhands/agenthub/codeact_agent/codeact_agent.py+++ b/openhands/agenthub/codeact_agent/codeact_agent.py@@ -10,6 +10,7 @@ if TYPE_CHECKING:from openhands.events.action import Actionfrom openhands.llm.llm import ModelResponse+from openhands.llm.llm_utils import check_toolsimport openhands.agenthub.codeact_agent.function_calling as codeact_function_callingfrom openhands.agenthub.codeact_agent.tools.bash import create_cmd_run_toolfrom openhands.agenthub.codeact_agent.tools.browser import BrowserTool@@ -185,26 +186,7 @@ class CodeActAgent(Agent):params: dict = {'messages': self.llm.format_messages_for_llm(messages),}- params['tools'] = self.tools-- # Special handling for Gemini model which doesn't support default fields- if self.llm.config.model == 'gemini-2.5-pro-preview-03-25':- logger.info(- f'Removing the default fields from tools for {self.llm.config.model} '- "since it doesn't support them and the request would crash."- )- # prevent mutation of input tools- params['tools'] = copy.deepcopy(params['tools'])- # Strip off default fields that cause errors with gemini-preview- for tool in params['tools']:- if 'function' in tool and 'parameters' in tool['function']:- if 'properties' in tool['function']['parameters']:- for prop_name, prop in tool['function']['parameters'][- 'properties'- ].items():- if 'default' in prop:- del prop['default']- # log to litellm proxy if possible+ params['tools'] = check_tools(self.tools, self.llm.config)params['extra_body'] = {'metadata': state.to_llm_metadata(agent_name=self.name)}response = self.llm.completion(**params)logger.debug(f'Response from LLM: {response}')diff --git a/openhands/llm/llm.py b/openhands/llm/llm.pyindex 5c9ae0861d..5e404b33ba 100644--- a/openhands/llm/llm.py+++ b/openhands/llm/llm.py@@ -279,9 +279,6 @@ class LLM(RetryMixin, DebugMixin):# Record start time for latency measurementstart_time = time.time()# we don't support streaming here, thus we get a ModelResponse- logger.debug(- f'LLM: calling litellm completion with model: {self.config.model}, base_url: {self.config.base_url}, args: {args}, kwargs: {kwargs}'- )resp: ModelResponse = self._completion_unwrapped(*args, **kwargs)# Calculate and record latencydiff --git a/openhands/llm/llm_utils.py b/openhands/llm/llm_utils.pynew file mode 100644index 0000000000..c59c24d0bf--- /dev/null+++ b/openhands/llm/llm_utils.py@@ -0,0 +1,44 @@+import copy+from typing import TYPE_CHECKING++from openhands.core.config import LLMConfig+from openhands.core.logger import openhands_logger as logger++if TYPE_CHECKING:+ from litellm import ChatCompletionToolParam+++def check_tools(+ tools: list['ChatCompletionToolParam'], llm_config: LLMConfig+) -> list['ChatCompletionToolParam']:+ """Checks and modifies tools for compatibility with the current LLM."""+ # Special handling for Gemini models which don't support default fields and have limited format support+ if 'gemini' in llm_config.model.lower():+ logger.info(+ f'Removing default fields and unsupported formats from tools for Gemini model {llm_config.model} '+ "since Gemini models have limited format support (only 'enum' and 'date-time' for STRING types)."+ )+ # prevent mutation of input tools+ checked_tools = copy.deepcopy(tools)+ # Strip off default fields and unsupported formats that cause errors with gemini-preview+ for tool in checked_tools:+ if 'function' in tool and 'parameters' in tool['function']:+ if 'properties' in tool['function']['parameters']:+ for prop_name, prop in tool['function']['parameters'][+ 'properties'+ ].items():+ # Remove default fields+ if 'default' in prop:+ del prop['default']++ # Remove format fields for STRING type parameters if the format is unsupported+ # Gemini only supports 'enum' and 'date-time' formats for STRING type+ if prop.get('type') == 'string' and 'format' in prop:+ supported_formats = ['enum', 'date-time']+ if prop['format'] not in supported_formats:+ logger.info(+ f'Removing unsupported format "{prop["format"]}" for STRING parameter "{prop_name}"'+ )+ del prop['format']+ return checked_tools+ return tools