π OpenHands: AI-Driven Development
Remove openhands.llm package (legacy V0 code) (#14154)
Tim O'Farrellaea611602f1a
diff --git a/openhands/events/event.py b/openhands/events/event.pyindex e9e426b8a3..b459293a8a 100644--- a/openhands/events/event.py+++ b/openhands/events/event.py@@ -2,8 +2,8 @@ from dataclasses import dataclassfrom datetime import datetimefrom enum import Enum+from openhands.events.metrics import Metricsfrom openhands.events.tool import ToolCallMetadata-from openhands.llm.metrics import Metricsclass EventSource(str, Enum):diff --git a/openhands/llm/metrics.py b/openhands/events/metrics.pysimilarity index 100%rename from openhands/llm/metrics.pyrename to openhands/events/metrics.pydiff --git a/openhands/events/serialization/event.py b/openhands/events/serialization/event.pyindex 1b144b9fd7..5c17068553 100644--- a/openhands/events/serialization/event.py+++ b/openhands/events/serialization/event.py@@ -6,11 +6,11 @@ from typing import Anyfrom pydantic import BaseModelfrom openhands.events import Event, EventSource+from openhands.events.metrics import Cost, Metrics, ResponseLatency, TokenUsagefrom openhands.events.serialization.action import action_from_dictfrom openhands.events.serialization.observation import observation_from_dictfrom openhands.events.serialization.utils import remove_fieldsfrom openhands.events.tool import ToolCallMetadata-from openhands.llm.metrics import Cost, Metrics, ResponseLatency, TokenUsage# TODO: move `content` into `extras`TOP_KEYS = [diff --git a/openhands/llm/__init__.py b/openhands/llm/__init__.pydeleted file mode 100644index ba80a4dc2a..0000000000--- a/openhands/llm/__init__.py+++ /dev/null@@ -1,5 +0,0 @@-from openhands.llm.async_llm import AsyncLLM-from openhands.llm.llm import LLM-from openhands.llm.streaming_llm import StreamingLLM--__all__ = ['LLM', 'AsyncLLM', 'StreamingLLM']diff --git a/openhands/llm/async_llm.py b/openhands/llm/async_llm.pydeleted file mode 100644index 53498f099f..0000000000--- a/openhands/llm/async_llm.py+++ /dev/null@@ -1,140 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-import asyncio-from functools import partial-from typing import Any, Callable--from litellm import acompletion as litellm_acompletion--from openhands.core.exceptions import UserCancelledError-from openhands.core.logger import openhands_logger as logger-from openhands.llm.llm import (- LLM,- LLM_RETRY_EXCEPTIONS,-)-from openhands.llm.model_features import get_features-from openhands.utils.shutdown_listener import should_continue---class AsyncLLM(LLM):- """Asynchronous LLM class."""-- def __init__(self, *args: Any, **kwargs: Any) -> None:- super().__init__(*args, **kwargs)-- self._async_completion = partial(- self._call_acompletion,- model=self.config.model,- api_key=self.config.api_key.get_secret_value()- if self.config.api_key- else None,- base_url=self.config.base_url,- api_version=self.config.api_version,- custom_llm_provider=self.config.custom_llm_provider,- max_tokens=self.config.max_output_tokens,- timeout=self.config.timeout,- temperature=self.config.temperature,- top_p=self.config.top_p,- drop_params=self.config.drop_params,- seed=self.config.seed,- )-- async_completion_unwrapped = self._async_completion-- @self.retry_decorator(- num_retries=self.config.num_retries,- retry_exceptions=LLM_RETRY_EXCEPTIONS,- retry_min_wait=self.config.retry_min_wait,- retry_max_wait=self.config.retry_max_wait,- retry_multiplier=self.config.retry_multiplier,- )- async def async_completion_wrapper(*args: Any, **kwargs: Any) -> Any:- """Wrapper for the litellm acompletion function that adds logging and cost tracking."""- messages: list[dict[str, Any]] | dict[str, Any] = []-- # some callers might send the model and messages directly- # litellm allows positional args, like completion(model, messages, **kwargs)- # see llm.py for more details- if len(args) > 1:- messages = args[1] if len(args) > 1 else args[0]- kwargs['messages'] = messages-- # remove the first args, they're sent in kwargs- args = args[2:]- elif 'messages' in kwargs:- messages = kwargs['messages']-- # Set reasoning effort for models that support it, only if explicitly provided- if (- get_features(self.config.model).supports_reasoning_effort- and self.config.reasoning_effort is not None- ):- kwargs['reasoning_effort'] = self.config.reasoning_effort-- # ensure we work with a list of messages- messages = messages if isinstance(messages, list) else [messages]-- # if we have no messages, something went very wrong- if not messages:- raise ValueError(- 'The messages list is empty. At least one message is required.'- )-- self.log_prompt(messages)-- async def check_stopped() -> None:- while should_continue():- if (- hasattr(self.config, 'on_cancel_requested_fn')- and self.config.on_cancel_requested_fn is not None- and await self.config.on_cancel_requested_fn()- ):- return- await asyncio.sleep(0.1)-- stop_check_task = asyncio.create_task(check_stopped())-- try:- # Directly call and await litellm_acompletion- resp = await async_completion_unwrapped(*args, **kwargs)-- message_back = resp['choices'][0]['message']['content']- self.log_response(message_back)-- # log costs and tokens used- self._post_completion(resp)-- # We do not support streaming in this method, thus return resp- return resp-- except UserCancelledError:- logger.debug('LLM request cancelled by user.')- raise- except Exception as e:- logger.error(f'Completion Error occurred:\n{e}')- raise-- finally:- await asyncio.sleep(0.1)- stop_check_task.cancel()- try:- await stop_check_task- except asyncio.CancelledError:- pass-- self._async_completion = async_completion_wrapper-- async def _call_acompletion(self, *args: Any, **kwargs: Any) -> Any:- """Wrapper for the litellm acompletion function."""- # Used in testing?- return await litellm_acompletion(*args, **kwargs)-- @property- def async_completion(self) -> Callable:- """Decorator for the async litellm acompletion function."""- return self._async_completiondiff --git a/openhands/llm/debug_mixin.py b/openhands/llm/debug_mixin.pydeleted file mode 100644index 16528ea05a..0000000000--- a/openhands/llm/debug_mixin.py+++ /dev/null@@ -1,80 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-from logging import DEBUG-from typing import Any--from litellm import ChatCompletionMessageToolCall-from litellm.types.utils import ModelResponse--from openhands.core.logger import llm_prompt_logger, llm_response_logger-from openhands.core.logger import openhands_logger as logger--MESSAGE_SEPARATOR = '\n\n----------\n\n'---class DebugMixin:- def log_prompt(self, messages: list[dict[str, Any]] | dict[str, Any]) -> None:- if not logger.isEnabledFor(DEBUG):- # Don't use memory building message string if not logging.- return- if not messages:- logger.debug('No completion messages!')- return-- messages = messages if isinstance(messages, list) else [messages]- debug_message = MESSAGE_SEPARATOR.join(- self._format_message_content(msg)- for msg in messages- if msg['content'] is not None- )-- if debug_message:- llm_prompt_logger.debug(debug_message)- else:- logger.debug('No completion messages!')-- def log_response(self, resp: ModelResponse) -> None:- if not logger.isEnabledFor(DEBUG):- # Don't use memory building message string if not logging.- return- message_back: str = resp['choices'][0]['message']['content'] or ''- tool_calls: list[ChatCompletionMessageToolCall] = resp['choices'][0][- 'message'- ].get('tool_calls', [])- if tool_calls:- for tool_call in tool_calls:- fn_name = tool_call.function.name- fn_args = tool_call.function.arguments- message_back += f'\nFunction call: {fn_name}({fn_args})'-- if message_back:- llm_response_logger.debug(message_back)-- def _format_message_content(self, message: dict[str, Any]) -> str:- content = message['content']- if isinstance(content, list):- return '\n'.join(- self._format_content_element(element) for element in content- )- return str(content)-- def _format_content_element(self, element: dict[str, Any] | Any) -> str:- if isinstance(element, dict):- if 'text' in element:- return str(element['text'])- if (- self.vision_is_active()- and 'image_url' in element- and 'url' in element['image_url']- ):- return str(element['image_url']['url'])- return str(element)-- # This method should be implemented in the class that uses DebugMixin- def vision_is_active(self) -> bool:- raise NotImplementedErrordiff --git a/openhands/llm/fn_call_converter.py b/openhands/llm/fn_call_converter.pydeleted file mode 100644index d5be059324..0000000000--- a/openhands/llm/fn_call_converter.py+++ /dev/null@@ -1,977 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-"""Convert function calling messages to non-function calling messages and vice versa.--This will inject prompts so that models that doesn't support function calling-can still be used with function calling agents.--We follow format from: https://docs.litellm.ai/docs/completion/function_call-"""--import copy-import json-import re-import sys-from typing import Iterable--from litellm import ChatCompletionToolParam--from openhands.core.exceptions import (- FunctionCallConversionError,- FunctionCallValidationError,-)-from openhands.llm.tool_names import (- BROWSER_TOOL_NAME,- EXECUTE_BASH_TOOL_NAME,- FINISH_TOOL_NAME,- LLM_BASED_EDIT_TOOL_NAME,- STR_REPLACE_EDITOR_TOOL_NAME,-)--# Inspired by: https://docs.together.ai/docs/llama-3-function-calling#function-calling-w-llama-31-70b-SYSTEM_PROMPT_SUFFIX_TEMPLATE = """-You have access to the following functions:--{description}--If you choose to call a function ONLY reply in the following format with NO suffix:--<function=example_function_name>-<parameter=example_parameter_1>value_1</parameter>-<parameter=example_parameter_2>-This is the value for the second parameter-that can span-multiple lines-</parameter>-</function>--<IMPORTANT>-Reminder:-- Function calls MUST follow the specified format, start with <function= and end with </function>-- Required parameters MUST be specified-- Only call one function at a time-- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after.-- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls-</IMPORTANT>-"""--STOP_WORDS = ['</function']---def refine_prompt(prompt: str) -> str:- if sys.platform == 'win32':- return prompt.replace('bash', 'powershell')- return prompt---# Example snippets for each tool-TOOL_EXAMPLES = {- 'execute_bash': {- 'check_dir': """-ASSISTANT: Sure! Let me first check the current directory:-<function=execute_bash>-<parameter=command>-pwd && ls-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-/workspace-openhands@runtime:~/workspace$-""",- 'run_server': """-ASSISTANT:-Let me run the Python file for you:-<function=execute_bash>-<parameter=command>-python3 app.py > server.log 2>&1 &-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-[1] 121-[1]+ Exit 1 python3 app.py > server.log 2>&1--ASSISTANT:-Looks like the server was running with PID 121 then crashed. Let me check the server log:-<function=execute_bash>-<parameter=command>-cat server.log-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-Traceback (most recent call last):- File "/workspace/app.py", line 2, in <module>- from flask import Flask-ModuleNotFoundError: No module named 'flask'--ASSISTANT:-Looks like the server crashed because the `flask` module is not installed. Let me install the `flask` module for you:-<function=execute_bash>-<parameter=command>-pip3 install flask-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-Defaulting to user installation because normal site-packages is not writeable-Collecting flask- Using cached flask-3.0.3-py3-none-any.whl (101 kB)-Collecting blinker>=1.6.2- Using cached blinker-1.7.0-py3-none-any.whl (13 kB)-Collecting Werkzeug>=3.0.0- Using cached werkzeug-3.0.2-py3-none-any.whl (226 kB)-Collecting click>=8.1.3- Using cached click-8.1.7-py3-none-any.whl (97 kB)-Collecting itsdangerous>=2.1.2- Using cached itsdangerous-2.2.0-py3-none-any.whl (16 kB)-Requirement already satisfied: Jinja2>=3.1.2 in /home/openhands/.local/lib/python3.10/site-packages (from flask) (3.1.3)-Requirement already satisfied: MarkupSafe>=2.0 in /home/openhands/.local/lib/python3.10/site-packages (from Jinja2>=3.1.2->flask) (2.1.5)-Installing collected packages: Werkzeug, itsdangerous, click, blinker, flask-Successfully installed Werkzeug-3.0.2 blinker-1.7.0 click-8.1.7 flask-3.0.3 itsdangerous-2.2.0--ASSISTANT:-The `flask` module is installed successfully. Let me run the Python file again:-<function=execute_bash>-<parameter=command>-python3 app.py > server.log 2>&1 &-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-[1] 124--ASSISTANT:-Let me check the server log again:-<function=execute_bash>-<parameter=command>-cat server.log-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-* Serving Flask app 'app'- * Debug mode: off-WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.- * Running on http://127.0.0.1:5000-Press CTRL+C to quit--ASSISTANT:-The server is running on port 5000 with PID 124. You can access the list of numbers by visiting http://127.0.0.1:5000. If you have any further questions, feel free to ask!-""",- 'kill_server': """-USER: Now kill the server, make it display the numbers in a table format.--ASSISTANT:-Sure! Let me stop the server first:-<function=execute_bash>-<parameter=command>-kill 124-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-[1]+ Terminated python3 app.py > server.log 2>&1-""",- 'run_server_again': """-ASSISTANT:-Running the updated file:-<function=execute_bash>-<parameter=command>-python3 app.py > server.log 2>&1 &-</parameter>-</function>--USER: EXECUTION RESULT of [execute_bash]:-[1] 126--ASSISTANT:-The server is running on port 5000 with PID 126. You can access the list of numbers in a table format by visiting http://127.0.0.1:5000.-""",- },- 'str_replace_editor': {- 'create_file': """-ASSISTANT:-There is no `app.py` file in the current directory. Let me create a Python file `app.py`:-<function=str_replace_editor>-<parameter=command>create</parameter>-<parameter=path>/workspace/app.py</parameter>-<parameter=file_text>-from flask import Flask-app = Flask(__name__)-[email protected]('/')-def index():- numbers = list(range(1, 11))- return str(numbers)--if __name__ == '__main__':- app.run(port=5000)-</parameter>-</function>--USER: EXECUTION RESULT of [str_replace_editor]:-File created successfully at: /workspace/app.py-""",- 'edit_file': """-ASSISTANT:-Now let me display the numbers in a table format:-<function=str_replace_editor>-<parameter=command>str_replace</parameter>-<parameter=path>/workspace/app.py</parameter>-<parameter=old_str>return str(numbers)</parameter>-<parameter=new_str>return '<table>' + ''.join([f'<tr><td>{i}</td></tr>' for i in numbers]) + '</table>'</parameter>-</function>--USER: EXECUTION RESULT of [str_replace_editor]:-The file /workspace/app.py has been edited. Here's the result of running `cat -n` on a snippet of /workspace/app.py:- 3- 4 @app.route('/')- 5 def index():- 6 numbers = list(range(1, 11))- 7 return '<table>' + ''.join([f'<tr><td>{i}</td></tr>' for i in numbers]) + '</table>'- 8- 9 if __name__ == '__main__':- 10 app.run(port=5000)-Review the changes and make sure they are as expected. Edit the file again if necessary.--""",- },- 'browser': {- 'view_page': """-ASSISTANT:-Let me check how the page looks in the browser:-<function=browser>-<parameter=code>-goto('http://127.0.0.1:5000')-noop(1000) # Wait for page to load-</parameter>-</function>--USER: EXECUTION RESULT of [browser]:-[Browser shows the numbers in a table format]-"""- },- 'edit_file': {- 'create_file': """-ASSISTANT: There is no `app.py` file in the current directory. Let me create a Python file `app.py`:-<function=edit_file>-<parameter=path>/workspace/app.py</parameter>-<parameter=start>1</parameter>-<parameter=end>-1</parameter>-<parameter=content>-from flask import Flask-app = Flask(__name__)-[email protected]('/')-def index():- numbers = list(range(1, 11))- return str(numbers)--if __name__ == '__main__':- app.run(port=5000)-</parameter>-</function>--USER: EXECUTION RESULT of [edit_file]:-File created successfully at: /workspace/app.py-""",- 'edit_file': """-ASSISTANT:-Now let me display the numbers in a table format:-<function=edit_file>-<parameter=path>/workspace/app.py</parameter>-<parameter=start>6</parameter>-<parameter=end>9</parameter>-<parameter=content>- numbers = list(range(1, 11))- return '<table>' + ''.join([f'<tr><td>{i}</td></tr>' for i in numbers]) + '</table>'- # ... existing code ...-if __name__ == '__main__':-</parameter>-</function>--USER: EXECUTION RESULT of [edit_file]:-The file /workspace/app.py has been edited. Here's the result of running `cat -n` on a snippet of /workspace/app.py:- 3- 4 @app.route('/')- 5 def index():- 6 numbers = list(range(1, 11))- 7 return '<table>' + ''.join([f'<tr><td>{i}</td></tr>' for i in numbers]) + '</table>'- 8- 9 if __name__ == '__main__':- 10 app.run(port=5000)-Review the changes and make sure they are as expected. Edit the file again if necessary.-""",- },- 'finish': {- 'example': """-ASSISTANT:-The server is running on port 5000 with PID 126. You can access the list of numbers in a table format by visiting http://127.0.0.1:5000. Let me know if you have any further requests!-<function=finish>-<parameter=message>The task has been completed. The web server is running and displaying numbers 1-10 in a table format at http://127.0.0.1:5000.</parameter>-</function>-"""- },-}---def get_example_for_tools(tools: list[dict]) -> str:- """Generate an in-context learning example based on available tools."""- available_tools = set()- for tool in tools:- if tool['type'] == 'function':- name = tool['function']['name']- if name == EXECUTE_BASH_TOOL_NAME:- available_tools.add('execute_bash')- elif name == STR_REPLACE_EDITOR_TOOL_NAME:- available_tools.add('str_replace_editor')- elif name == BROWSER_TOOL_NAME:- available_tools.add('browser')- elif name == FINISH_TOOL_NAME:- available_tools.add('finish')- elif name == LLM_BASED_EDIT_TOOL_NAME:- available_tools.add('edit_file')-- if not available_tools:- return ''-- example = """Here's a running example of how to perform a task with the provided tools.----------------------- START OF EXAMPLE -----------------------USER: Create a list of numbers from 1 to 10, and display them in a web page at port 5000.--"""-- # Build example based on available tools- if 'execute_bash' in available_tools:- example += TOOL_EXAMPLES['execute_bash']['check_dir']-- if 'str_replace_editor' in available_tools:- example += TOOL_EXAMPLES['str_replace_editor']['create_file']- elif 'edit_file' in available_tools:- example += TOOL_EXAMPLES['edit_file']['create_file']-- if 'execute_bash' in available_tools:- example += TOOL_EXAMPLES['execute_bash']['run_server']-- if 'browser' in available_tools:- example += TOOL_EXAMPLES['browser']['view_page']-- if 'execute_bash' in available_tools:- example += TOOL_EXAMPLES['execute_bash']['kill_server']-- if 'str_replace_editor' in available_tools:- example += TOOL_EXAMPLES['str_replace_editor']['edit_file']- elif 'edit_file' in available_tools:- example += TOOL_EXAMPLES['edit_file']['edit_file']-- if 'execute_bash' in available_tools:- example += TOOL_EXAMPLES['execute_bash']['run_server_again']-- if 'finish' in available_tools:- example += TOOL_EXAMPLES['finish']['example']-- example += """---------------------- END OF EXAMPLE -----------------------Do NOT assume the environment is the same as in the example above.----------------------- NEW TASK DESCRIPTION ----------------------"""- example = example.lstrip()-- return refine_prompt(example)---IN_CONTEXT_LEARNING_EXAMPLE_PREFIX = get_example_for_tools--IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX = """---------------------- END OF NEW TASK DESCRIPTION -----------------------PLEASE follow the format strictly! PLEASE EMIT ONE AND ONLY ONE FUNCTION CALL PER MESSAGE.-"""--# Regex patterns for function call parsing-FN_REGEX_PATTERN = r'<function=([^>]+)>\n(.*?)</function>'-FN_PARAM_REGEX_PATTERN = r'<parameter=([^>]+)>(.*?)</parameter>'--# Add new regex pattern for tool execution results-TOOL_RESULT_REGEX_PATTERN = r'EXECUTION RESULT of \[(.*?)\]:\n(.*)'---def convert_tool_call_to_string(tool_call: dict) -> str:- """Convert tool call to content in string format."""- if 'function' not in tool_call:- raise FunctionCallConversionError("Tool call must contain 'function' key.")- if 'id' not in tool_call:- raise FunctionCallConversionError("Tool call must contain 'id' key.")- if 'type' not in tool_call:- raise FunctionCallConversionError("Tool call must contain 'type' key.")- if tool_call['type'] != 'function':- raise FunctionCallConversionError("Tool call type must be 'function'.")-- ret = f'<function={tool_call["function"]["name"]}>\n'- try:- args = json.loads(tool_call['function']['arguments'])- except json.JSONDecodeError as e:- raise FunctionCallConversionError(- f'Failed to parse arguments as JSON. Arguments: {tool_call["function"]["arguments"]}'- ) from e- for param_name, param_value in args.items():- # Don't add extra newlines - keep parameter value as-is- ret += f'<parameter={param_name}>'- if isinstance(param_value, list) or isinstance(param_value, dict):- ret += json.dumps(param_value)- else:- ret += f'{param_value}'- ret += '</parameter>\n'- ret += '</function>'- return ret---def convert_tools_to_description(tools: list[dict]) -> str:- ret = ''- for i, tool in enumerate(tools):- assert tool['type'] == 'function'- fn = tool['function']- if i > 0:- ret += '\n'- ret += f'---- BEGIN FUNCTION #{i + 1}: {fn["name"]} ----\n'- ret += f'Description: {fn["description"]}\n'-- if 'parameters' in fn:- ret += 'Parameters:\n'- properties = fn['parameters'].get('properties', {})- required_params = set(fn['parameters'].get('required', []))-- for j, (param_name, param_info) in enumerate(properties.items()):- # Indicate required/optional in parentheses with type- is_required = param_name in required_params- param_status = 'required' if is_required else 'optional'- param_type = param_info.get('type', 'string')-- # Get parameter description- desc = param_info.get('description', 'No description provided')-- # Handle enum values if present- if 'enum' in param_info:- enum_values = ', '.join(f'`{v}`' for v in param_info['enum'])- desc += f'\nAllowed values: [{enum_values}]'-- ret += (- f' ({j + 1}) {param_name} ({param_type}, {param_status}): {desc}\n'- )- else:- ret += 'No parameters are required for this function.\n'-- ret += f'---- END FUNCTION #{i + 1} ----\n'- return ret---def convert_fncall_messages_to_non_fncall_messages(- messages: list[dict],- tools: list[ChatCompletionToolParam],- add_in_context_learning_example: bool = True,-) -> list[dict]:- """Convert function calling messages to non-function calling messages."""- messages = copy.deepcopy(messages)-- formatted_tools = convert_tools_to_description(tools)- system_prompt_suffix = SYSTEM_PROMPT_SUFFIX_TEMPLATE.format(- description=formatted_tools- )-- converted_messages = []- first_user_message_encountered = False- for message in messages:- role = message['role']- content = message['content']-- # 1. SYSTEM MESSAGES- # append system prompt suffix to content- if role == 'system':- if isinstance(content, str):- content += system_prompt_suffix- elif isinstance(content, list):- if content and content[-1]['type'] == 'text':- content[-1]['text'] += system_prompt_suffix- else:- content.append({'type': 'text', 'text': system_prompt_suffix})- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )- converted_messages.append({'role': 'system', 'content': content})-- # 2. USER MESSAGES (no change)- elif role == 'user':- # Add in-context learning example for the first user message- if not first_user_message_encountered and add_in_context_learning_example:- first_user_message_encountered = True-- # Generate example based on available tools- example = IN_CONTEXT_LEARNING_EXAMPLE_PREFIX(tools)-- # Add example if we have any tools- if example:- # add in-context learning example- if isinstance(content, str):- content = example + content + IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX- elif isinstance(content, list):- if content and content[0]['type'] == 'text':- content[0]['text'] = (- example- + content[0]['text']- + IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX- )- else:- content = (- [- {- 'type': 'text',- 'text': example,- }- ]- + content- + [- {- 'type': 'text',- 'text': IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX,- }- ]- )- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )- converted_messages.append(- {- 'role': 'user',- 'content': content,- }- )-- # 3. ASSISTANT MESSAGES- # - 3.1 no change if no function call- # - 3.2 change if function call- elif role == 'assistant':- if 'tool_calls' in message and message['tool_calls'] is not None:- if len(message['tool_calls']) != 1:- raise FunctionCallConversionError(- f'Expected exactly one tool call in the message. More than one tool call is not supported. But got {len(message["tool_calls"])} tool calls. Content: {content}'- )- try:- tool_content = convert_tool_call_to_string(message['tool_calls'][0])- except FunctionCallConversionError as e:- raise FunctionCallConversionError(- f'Failed to convert tool call to string.\nCurrent tool call: {message["tool_calls"][0]}.\nRaw messages: {json.dumps(messages, indent=2)}'- ) from e- if isinstance(content, str):- content += '\n\n' + tool_content- content = content.lstrip()- elif isinstance(content, list):- if content and content[-1]['type'] == 'text':- content[-1]['text'] += '\n\n' + tool_content- content[-1]['text'] = content[-1]['text'].lstrip()- else:- content.append({'type': 'text', 'text': tool_content})- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )- converted_messages.append({'role': 'assistant', 'content': content})-- # 4. TOOL MESSAGES (tool outputs)- elif role == 'tool':- # Convert tool result as user message- tool_name = message.get('name', 'function')- prefix = f'EXECUTION RESULT of [{tool_name}]:\n'- # and omit "tool_call_id" AND "name"- if isinstance(content, str):- content = prefix + content- elif isinstance(content, list):- if content and (- first_text_content := next(- (c for c in content if c['type'] == 'text'), None- )- ):- first_text_content['text'] = prefix + first_text_content['text']- else:- content = [{'type': 'text', 'text': prefix}] + content- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )- if 'cache_control' in message:- content[-1]['cache_control'] = {'type': 'ephemeral'}- converted_messages.append({'role': 'user', 'content': content})- else:- raise FunctionCallConversionError(- f'Unexpected role {role}. Expected system, user, assistant or tool.'- )- return converted_messages---def _extract_and_validate_params(- matching_tool: dict, param_matches: Iterable[re.Match], fn_name: str-) -> dict:- params = {}- # Parse and validate parameters- required_params = set()- if 'parameters' in matching_tool and 'required' in matching_tool['parameters']:- required_params = set(matching_tool['parameters'].get('required', []))-- allowed_params = set()- if 'parameters' in matching_tool and 'properties' in matching_tool['parameters']:- allowed_params = set(matching_tool['parameters']['properties'].keys())-- param_name_to_type = {}- if 'parameters' in matching_tool and 'properties' in matching_tool['parameters']:- param_name_to_type = {- name: val.get('type', 'string')- for name, val in matching_tool['parameters']['properties'].items()- }-- # Collect parameters- found_params = set()- for param_match in param_matches:- param_name = param_match.group(1)- param_value = param_match.group(2)-- # Validate parameter is allowed- if allowed_params and param_name not in allowed_params:- raise FunctionCallValidationError(- f"Parameter '{param_name}' is not allowed for function '{fn_name}'. "- f'Allowed parameters: {allowed_params}'- )-- # Validate and convert parameter type- # supported: string, integer, array- if param_name in param_name_to_type:- if param_name_to_type[param_name] == 'integer':- try:- param_value = int(param_value)- except ValueError:- raise FunctionCallValidationError(- f"Parameter '{param_name}' is expected to be an integer."- )- elif param_name_to_type[param_name] == 'array':- try:- param_value = json.loads(param_value)- except json.JSONDecodeError:- raise FunctionCallValidationError(- f"Parameter '{param_name}' is expected to be an array."- )- else:- # string- pass-- # Enum check- if 'enum' in matching_tool['parameters']['properties'][param_name]:- if (- param_value- not in matching_tool['parameters']['properties'][param_name]['enum']- ):- raise FunctionCallValidationError(- f"Parameter '{param_name}' is expected to be one of {matching_tool['parameters']['properties'][param_name]['enum']}."- )-- params[param_name] = param_value- found_params.add(param_name)-- # Check all required parameters are present- missing_params = required_params - found_params- if missing_params:- raise FunctionCallValidationError(- f"Missing required parameters for function '{fn_name}': {missing_params}"- )- return params---def _fix_stopword(content: str) -> str:- """Fix the issue when some LLM would NOT return the stopword."""- if '<function=' in content and content.count('<function=') == 1:- if content.endswith('</'):- content = content.rstrip() + 'function>'- else:- content = content + '\n</function>'- return content---def _normalize_parameter_tags(fn_body: str) -> str:- """Normalize malformed parameter tags to the canonical format.-- Some models occasionally emit malformed parameter tags like:- <parameter=command=str_replace</parameter>- instead of the correct:- <parameter=command>str_replace</parameter>-- This function rewrites the malformed form into the correct one to allow- downstream parsing to succeed.- """- # Replace '<parameter=name=value</parameter>' with '<parameter=name>value</parameter>'- return re.sub(- r'<parameter=([a-zA-Z0-9_]+)=([^<]*)</parameter>',- r'<parameter=\1>\2</parameter>',- fn_body,- )---def convert_non_fncall_messages_to_fncall_messages(- messages: list[dict],- tools: list[ChatCompletionToolParam],-) -> list[dict]:- """Convert non-function calling messages back to function calling messages."""- messages = copy.deepcopy(messages)- formatted_tools = convert_tools_to_description(tools)- system_prompt_suffix = SYSTEM_PROMPT_SUFFIX_TEMPLATE.format(- description=formatted_tools- )-- converted_messages = []- tool_call_counter = 1 # Counter for tool calls-- first_user_message_encountered = False- for message in messages:- role, content = message['role'], message['content']- content = content or '' # handle cases where content is None- # For system messages, remove the added suffix- if role == 'system':- if isinstance(content, str):- # Remove the suffix if present- content = content.split(system_prompt_suffix)[0]- elif isinstance(content, list):- if content and content[-1]['type'] == 'text':- # Remove the suffix from the last text item- content[-1]['text'] = content[-1]['text'].split(- system_prompt_suffix- )[0]- converted_messages.append({'role': 'system', 'content': content})- # Skip user messages (no conversion needed)- elif role == 'user':- # Check & replace in-context learning example- if not first_user_message_encountered:- first_user_message_encountered = True- if isinstance(content, str):- # Remove any existing example- if content.startswith(IN_CONTEXT_LEARNING_EXAMPLE_PREFIX(tools)):- content = content.replace(- IN_CONTEXT_LEARNING_EXAMPLE_PREFIX(tools), '', 1- )- if content.endswith(IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX):- content = content.replace(- IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX, '', 1- )- elif isinstance(content, list):- for item in content:- if item['type'] == 'text':- # Remove any existing example- example = IN_CONTEXT_LEARNING_EXAMPLE_PREFIX(tools)- if item['text'].startswith(example):- item['text'] = item['text'].replace(example, '', 1)- if item['text'].endswith(- IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX- ):- item['text'] = item['text'].replace(- IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX, '', 1- )- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )-- # Check for tool execution result pattern- if isinstance(content, str):- tool_result_match = re.search(- TOOL_RESULT_REGEX_PATTERN, content, re.DOTALL- )- elif isinstance(content, list):- tool_result_match = next(- (- _match- for item in content- if item.get('type') == 'text'- and (- _match := re.search(- TOOL_RESULT_REGEX_PATTERN, item['text'], re.DOTALL- )- )- ),- None,- )- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )-- if tool_result_match:- if isinstance(content, list):- text_content_items = [- item for item in content if item.get('type') == 'text'- ]- if not text_content_items:- raise FunctionCallConversionError(- f'Could not find text content in message with tool result. Content: {content}'- )- elif not isinstance(content, str):- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )-- tool_name = tool_result_match.group(1)- tool_result = tool_result_match.group(2).strip()-- # Convert to tool message format- converted_messages.append(- {- 'role': 'tool',- 'name': tool_name,- 'content': [{'type': 'text', 'text': tool_result}]- if isinstance(content, list)- else tool_result,- 'tool_call_id': f'toolu_{tool_call_counter - 1:02d}', # Use last generated ID- }- )- else:- converted_messages.append({'role': 'user', 'content': content})-- # Handle assistant messages- elif role == 'assistant':- if isinstance(content, str):- content = _fix_stopword(content)- fn_match = re.search(FN_REGEX_PATTERN, content, re.DOTALL)- elif isinstance(content, list):- if content and content[-1]['type'] == 'text':- content[-1]['text'] = _fix_stopword(content[-1]['text'])- fn_match = re.search(- FN_REGEX_PATTERN, content[-1]['text'], re.DOTALL- )- else:- fn_match = None- fn_match_exists = any(- item.get('type') == 'text'- and re.search(FN_REGEX_PATTERN, item['text'], re.DOTALL)- for item in content- )- if fn_match_exists and not fn_match:- raise FunctionCallConversionError(- f'Expecting function call in the LAST index of content list. But got content={content}'- )- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )-- if fn_match:- fn_name = fn_match.group(1)- fn_body = _normalize_parameter_tags(fn_match.group(2))- matching_tool = next(- (- tool['function']- for tool in tools- if tool['type'] == 'function'- and tool['function']['name'] == fn_name- ),- None,- )- # Validate function exists in tools- if not matching_tool:- raise FunctionCallValidationError(- f"Function '{fn_name}' not found in available tools: {[tool['function']['name'] for tool in tools if tool['type'] == 'function']}"- )-- # Parse parameters- param_matches = re.finditer(FN_PARAM_REGEX_PATTERN, fn_body, re.DOTALL)- params = _extract_and_validate_params(- matching_tool, param_matches, fn_name- )-- # Create tool call with unique ID- tool_call_id = f'toolu_{tool_call_counter:02d}'- tool_call = {- 'index': 1, # always 1 because we only support **one tool call per message**- 'id': tool_call_id,- 'type': 'function',- 'function': {'name': fn_name, 'arguments': json.dumps(params)},- }- tool_call_counter += 1 # Increment counter-- # Remove the function call part from content- if isinstance(content, list):- assert content and content[-1]['type'] == 'text'- content[-1]['text'] = (- content[-1]['text'].split('<function=')[0].strip()- )- elif isinstance(content, str):- content = content.split('<function=')[0].strip()- else:- raise FunctionCallConversionError(- f'Unexpected content type {type(content)}. Expected str or list. Content: {content}'- )-- converted_messages.append(- {'role': 'assistant', 'content': content, 'tool_calls': [tool_call]}- )- else:- # No function call, keep message as is- converted_messages.append(message)-- else:- raise FunctionCallConversionError(- f'Unexpected role {role}. Expected system, user, or assistant in non-function calling messages.'- )- return converted_messages---def convert_from_multiple_tool_calls_to_single_tool_call_messages(- messages: list[dict],- ignore_final_tool_result: bool = False,-) -> list[dict]:- """Break one message with multiple tool calls into multiple messages."""- converted_messages = []-- pending_tool_calls: dict[str, dict] = {}- for message in messages:- role, content = message['role'], message['content']- if role == 'assistant':- if message.get('tool_calls') and len(message['tool_calls']) > 1:- # handle multiple tool calls by breaking them into multiple messages- for i, tool_call in enumerate(message['tool_calls']):- pending_tool_calls[tool_call['id']] = {- 'role': 'assistant',- 'content': content if i == 0 else '',- 'tool_calls': [tool_call],- }- else:- converted_messages.append(message)- elif role == 'tool':- if message['tool_call_id'] in pending_tool_calls:- # remove the tool call from the pending list- _tool_call_message = pending_tool_calls.pop(message['tool_call_id'])- converted_messages.append(_tool_call_message)- # add the tool result- converted_messages.append(message)- else:- assert len(pending_tool_calls) == 0, (- f'Found pending tool calls but not found in pending list: {pending_tool_calls=}'- )- converted_messages.append(message)- else:- assert len(pending_tool_calls) == 0, (- f'Found pending tool calls but not expect to handle it with role {role}: {pending_tool_calls=}, {message=}'- )- converted_messages.append(message)-- if not ignore_final_tool_result and len(pending_tool_calls) > 0:- raise FunctionCallConversionError(- f'Found pending tool calls but no tool result: {pending_tool_calls=}'- )- return converted_messagesdiff --git a/openhands/llm/llm.py b/openhands/llm/llm.pydeleted file mode 100644index b782cc6f6d..0000000000--- a/openhands/llm/llm.py+++ /dev/null@@ -1,872 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-# V1 replacement for this module lives in the Software Agent SDK.-import copy-import json-import os-import time-import warnings-from functools import partial-from typing import Any, Callable, cast--import httpx--from openhands.core.config import LLMConfig-from openhands.llm.metrics import Metrics-from openhands.llm.model_features import get_features--with warnings.catch_warnings():- warnings.simplefilter('ignore')- import litellm--from litellm import Message as LiteLLMMessage-from litellm import ModelInfo, PromptTokensDetails-from litellm import completion as litellm_completion-from litellm import completion_cost as litellm_completion_cost-from litellm.exceptions import (- APIConnectionError,- BadGatewayError,- RateLimitError,- ServiceUnavailableError,-)-from litellm.types.utils import CostPerToken, ModelResponse, Usage-from litellm.utils import create_pretrained_tokenizer--from openhands.core.exceptions import LLMNoResponseError-from openhands.core.logger import openhands_logger as logger-from openhands.core.message import Message-from openhands.llm.debug_mixin import DebugMixin-from openhands.llm.fn_call_converter import (- STOP_WORDS,- convert_fncall_messages_to_non_fncall_messages,- convert_non_fncall_messages_to_fncall_messages,-)-from openhands.llm.retry_mixin import RetryMixin--__all__ = ['LLM']--# tuple of exceptions to retry on-LLM_RETRY_EXCEPTIONS: tuple[type[Exception], ...] = (- APIConnectionError,- RateLimitError,- ServiceUnavailableError,- BadGatewayError,- litellm.Timeout,- litellm.InternalServerError,- LLMNoResponseError,-)---class LLM(RetryMixin, DebugMixin):- """The LLM class represents a Language Model instance.-- Attributes:- config: an LLMConfig object specifying the configuration of the LLM.- """-- def __init__(- self,- config: LLMConfig,- service_id: str,- metrics: Metrics | None = None,- retry_listener: Callable[[int, int], None] | None = None,- ) -> None:- """Initializes the LLM. If LLMConfig is passed, its values will be the fallback.-- Passing simple parameters always overrides config.-- Args:- config: The LLM configuration.- metrics: The metrics to use.- """- self._tried_model_info = False- self.cost_metric_supported: bool = True- self.config: LLMConfig = copy.deepcopy(config)- self.service_id = service_id- self.metrics: Metrics = (- metrics if metrics is not None else Metrics(model_name=config.model)- )-- self.model_info: ModelInfo | None = None- self._function_calling_active: bool = False- self.retry_listener = retry_listener- if self.config.log_completions:- if self.config.log_completions_folder is None:- raise RuntimeError(- 'log_completions_folder is required when log_completions is enabled'- )- os.makedirs(self.config.log_completions_folder, exist_ok=True)-- # call init_model_info to initialize config.max_output_tokens- # which is used in partial function- with warnings.catch_warnings():- warnings.simplefilter('ignore')- self.init_model_info()- if self.vision_is_active():- logger.debug('LLM: model has vision enabled')- if self.is_caching_prompt_active():- logger.debug('LLM: caching prompt enabled')- if self.is_function_calling_active():- logger.debug('LLM: model supports function calling')-- # if using a custom tokenizer, make sure it's loaded and accessible in the format expected by litellm- if self.config.custom_tokenizer is not None:- self.tokenizer = create_pretrained_tokenizer(self.config.custom_tokenizer)- else:- self.tokenizer = None-- # set up the completion function- kwargs: dict[str, Any] = {- 'temperature': self.config.temperature,- 'max_completion_tokens': self.config.max_output_tokens,- }- if self.config.top_k is not None:- # openai doesn't expose top_k- # litellm will handle it a bit differently than the openai-compatible params- kwargs['top_k'] = self.config.top_k- if self.config.top_p is not None:- # openai doesn't expose top_p, but litellm does- kwargs['top_p'] = self.config.top_p-- # Handle OpenHands provider - rewrite to litellm_proxy- if self.config.model.startswith('openhands/'):- model_name = self.config.model.removeprefix('openhands/')- self.config.model = f'litellm_proxy/{model_name}'- self.config.base_url = _get_openhands_llm_base_url()- logger.debug(- f'Rewrote openhands/{model_name} to {self.config.model} with base URL {self.config.base_url}'- )-- features = get_features(self.config.model)- if features.supports_reasoning_effort:- # For Gemini models, only map 'low' to optimized thinking budget- # Let other reasoning_effort values pass through to API as-is- if 'gemini-2.5-pro' in self.config.model:- logger.debug(- f'Gemini model {self.config.model} with reasoning_effort {self.config.reasoning_effort}'- )- if self.config.reasoning_effort in {None, 'low', 'none'}:- kwargs['thinking'] = {'budget_tokens': 128}- kwargs['allowed_openai_params'] = ['thinking']- kwargs.pop('reasoning_effort', None)- else:- kwargs['reasoning_effort'] = self.config.reasoning_effort- logger.debug(- f'Gemini model {self.config.model} with reasoning_effort {self.config.reasoning_effort} mapped to thinking {kwargs.get("thinking")}'- )- elif any(- k in self.config.model- for k in (- 'claude-sonnet-4-5',- 'claude-haiku-4-5-20251001',- 'claude-opus-4-6',- )- ):- # don't send reasoning_effort to specific Claude Sonnet/Haiku 4.5 variants or Claude Opus 4.6- kwargs.pop('reasoning_effort', None)- else:- if self.config.reasoning_effort is not None:- kwargs['reasoning_effort'] = self.config.reasoning_effort- kwargs.pop(- 'temperature'- ) # temperature is not supported for reasoning models- kwargs.pop('top_p') # reasoning model like o3 doesn't support top_p- # Azure issue: https://github.com/OpenHands/OpenHands/issues/6777- if self.config.model.startswith('azure'):- kwargs['max_tokens'] = self.config.max_output_tokens- kwargs.pop('max_completion_tokens')-- # Add safety settings for models that support them- if 'mistral' in self.config.model.lower() and self.config.safety_settings:- kwargs['safety_settings'] = self.config.safety_settings- elif 'gemini' in self.config.model.lower() and self.config.safety_settings:- kwargs['safety_settings'] = self.config.safety_settings-- # support AWS Bedrock provider- kwargs['aws_region_name'] = self.config.aws_region_name- if self.config.aws_access_key_id:- kwargs['aws_access_key_id'] = (- self.config.aws_access_key_id.get_secret_value()- )- if self.config.aws_secret_access_key:- kwargs['aws_secret_access_key'] = (- self.config.aws_secret_access_key.get_secret_value()- )-- # Explicitly disable Anthropic extended thinking for Opus 4.1 to avoid- # requiring 'thinking' content blocks. See issue #10510.- if 'claude-opus-4-1' in self.config.model.lower():- kwargs['thinking'] = {'type': 'disabled'}-- # Anthropic constraint: Opus 4.1, Opus 4.5, Opus 4.6, and Sonnet 4.x models cannot accept both temperature and top_p- # Prefer temperature (drop top_p) if both are specified.- _model_lower = self.config.model.lower()- # Apply to Opus 4.1, Opus 4.5, Opus 4.6, and Sonnet 4.x models to avoid API errors- if (- ('claude-opus-4-1' in _model_lower)- or ('claude-opus-4-5' in _model_lower)- or ('claude-opus-4-6' in _model_lower)- or ('claude-sonnet-4' in _model_lower)- ) and ('temperature' in kwargs and 'top_p' in kwargs):- kwargs.pop('top_p', None)-- # Add completion_kwargs if present- if self.config.completion_kwargs is not None:- kwargs.update(self.config.completion_kwargs)-- self._completion = partial(- litellm_completion,- model=self.config.model,- api_key=self.config.api_key.get_secret_value()- if self.config.api_key- else None,- base_url=self.config.base_url,- api_version=self.config.api_version,- custom_llm_provider=self.config.custom_llm_provider,- timeout=self.config.timeout,- drop_params=self.config.drop_params,- seed=self.config.seed,- **kwargs,- )-- self._completion_unwrapped = self._completion-- @self.retry_decorator(- num_retries=self.config.num_retries,- retry_exceptions=LLM_RETRY_EXCEPTIONS,- retry_min_wait=self.config.retry_min_wait,- retry_max_wait=self.config.retry_max_wait,- retry_multiplier=self.config.retry_multiplier,- retry_listener=self.retry_listener,- )- def wrapper(*args: Any, **kwargs: Any) -> Any:- """Wrapper for the litellm completion function. Logs the input and output of the completion function."""- messages_kwarg: (- dict[str, Any] | Message | list[dict[str, Any]] | list[Message]- ) = []- mock_function_calling = not self.is_function_calling_active()-- # some callers might send the model and messages directly- # litellm allows positional args, like completion(model, messages, **kwargs)- if len(args) > 1:- # ignore the first argument if it's provided (it would be the model)- # design wise: we don't allow overriding the configured values- # implementation wise: the partial function set the model as a kwarg already- # as well as other kwargs- messages_kwarg = args[1] if len(args) > 1 else args[0]- kwargs['messages'] = messages_kwarg-- # remove the first args, they're sent in kwargs- args = args[2:]- elif 'messages' in kwargs:- messages_kwarg = kwargs['messages']-- # ensure we work with a list of messages- messages_list = (- messages_kwarg if isinstance(messages_kwarg, list) else [messages_kwarg]- )- # Format Message objects to dict format if needed- messages: list[dict] = []- if messages_list and isinstance(messages_list[0], Message):- messages = self.format_messages_for_llm(- cast(list[Message], messages_list)- )- else:- messages = cast(list[dict[str, Any]], messages_list)-- kwargs['messages'] = messages-- # handle conversion of to non-function calling messages if needed- original_fncall_messages = copy.deepcopy(messages)- mock_fncall_tools = None- # if the agent or caller has defined tools, and we mock via prompting, convert the messages- if mock_function_calling and 'tools' in kwargs:- add_in_context_learning_example = True- if (- 'openhands-lm' in self.config.model- or 'devstral' in self.config.model- ):- add_in_context_learning_example = False-- messages = convert_fncall_messages_to_non_fncall_messages(- messages,- kwargs['tools'],- add_in_context_learning_example=add_in_context_learning_example,- )- kwargs['messages'] = messages-- # add stop words if the model supports it and stop words are not disabled- if (- get_features(self.config.model).supports_stop_words- and not self.config.disable_stop_word- ):- kwargs['stop'] = STOP_WORDS-- mock_fncall_tools = kwargs.pop('tools')- if 'openhands-lm' in self.config.model:- # If we don't have this, we might run into issue when serving openhands-lm- # using SGLang- # BadRequestError: litellm.BadRequestError: OpenAIException - Error code: 400 - {'object': 'error', 'message': '400', 'type': 'Failed to parse fc related info to json format!', 'param': None, 'code': 400}- kwargs['tool_choice'] = 'none'- else:- # tool_choice should not be specified when mocking function calling- kwargs.pop('tool_choice', None)-- # if we have no messages, something went very wrong- if not messages:- raise ValueError(- 'The messages list is empty. At least one message is required.'- )-- # log the entire LLM prompt- self.log_prompt(messages)-- # set litellm modify_params to the configured value- # True by default to allow litellm to do transformations like adding a default message, when a message is empty- # NOTE: this setting is global; unlike drop_params, it cannot be overridden in the litellm completion partial- litellm.modify_params = self.config.modify_params-- # if we're not using litellm proxy, remove the extra_body- if 'litellm_proxy' not in self.config.model:- kwargs.pop('extra_body', None)-- # Record start time for latency measurement- start_time = time.time()- # we don't support streaming here, thus we get a ModelResponse-- # Suppress httpx deprecation warnings during LiteLLM calls- # This prevents the "Use 'content=<...>' to upload raw bytes/text content" warning- # that appears when LiteLLM makes HTTP requests to LLM providers- with warnings.catch_warnings():- warnings.filterwarnings(- 'ignore', category=DeprecationWarning, module='httpx.*'- )- warnings.filterwarnings(- 'ignore',- message=r'.*content=.*upload.*',- category=DeprecationWarning,- )- resp: ModelResponse = self._completion_unwrapped(*args, **kwargs)-- # Calculate and record latency- latency = time.time() - start_time- response_id = resp.get('id', 'unknown')- self.metrics.add_response_latency(latency, response_id)-- non_fncall_response = copy.deepcopy(resp)-- # if we mocked function calling, and we have tools, convert the response back to function calling format- if mock_function_calling and mock_fncall_tools is not None:- if len(resp.choices) < 1:- raise LLMNoResponseError(- 'Response choices is less than 1 - This is only seen in Gemini models so far. Response: '- + str(resp)- )-- non_fncall_response_message = resp.choices[0].message- # messages is already a list with proper typing from line 223- fn_call_messages_with_response = (- convert_non_fncall_messages_to_fncall_messages(- messages + [non_fncall_response_message], mock_fncall_tools- )- )- fn_call_response_message = fn_call_messages_with_response[-1]- if not isinstance(fn_call_response_message, LiteLLMMessage):- fn_call_response_message = LiteLLMMessage(- **fn_call_response_message- )- resp.choices[0].message = fn_call_response_message-- # Check if resp has 'choices' key with at least one item- if not resp.get('choices') or len(resp['choices']) < 1:- raise LLMNoResponseError(- 'Response choices is less than 1 - This is only seen in Gemini models so far. Response: '- + str(resp)- )-- # log the LLM response- self.log_response(resp)-- # post-process the response first to calculate cost- cost = self._post_completion(resp)-- # log for evals or other scripts that need the raw completion- if self.config.log_completions:- assert self.config.log_completions_folder is not None- log_file = os.path.join(- self.config.log_completions_folder,- f'{self.config.model.replace("/", "__")}-{time.time()}.json',- )-- # set up the dict to be logged- _d = {- 'messages': messages,- 'response': resp,- 'args': args,- 'kwargs': {- k: v- for k, v in kwargs.items()- if k not in ('messages', 'client')- },- 'timestamp': time.time(),- 'cost': cost,- }-- # if non-native function calling, save messages/response separately- if mock_function_calling:- # Overwrite response as non-fncall to be consistent with messages- _d['response'] = non_fncall_response-- # Save fncall_messages/response separately- _d['fncall_messages'] = original_fncall_messages- _d['fncall_response'] = resp- with open(log_file, 'w') as f:- f.write(json.dumps(_d))-- return resp-- self._completion = wrapper-- @property- def completion(self) -> Callable:- """Decorator for the litellm completion function.-- Check the complete documentation at https://litellm.vercel.app/docs/completion- """- return self._completion-- def init_model_info(self) -> None:- if self._tried_model_info:- return- self._tried_model_info = True- try:- if self.config.model.startswith('openrouter'):- self.model_info = litellm.get_model_info(self.config.model)- except Exception as e:- logger.debug(f'Error getting model info: {e}')-- if self.config.model.startswith('litellm_proxy/'):- # IF we are using LiteLLM proxy, get model info from LiteLLM proxy- # GET {base_url}/v1/model/info with litellm_model_id as path param- base_url = self.config.base_url.strip() if self.config.base_url else ''- if not base_url.startswith(('http://', 'https://')):- base_url = 'http://' + base_url-- response = httpx.get(- f'{base_url}/v1/model/info',- headers={- 'Authorization': f'Bearer {self.config.api_key.get_secret_value() if self.config.api_key else None}'- },- )-- try:- resp_json = response.json()- if 'data' not in resp_json:- logger.info(- f'No data field in model info response from LiteLLM proxy: {resp_json}'- )- all_model_info = resp_json.get('data', [])- except Exception as e:- logger.info(f'Error parsing JSON response from LiteLLM proxy: {e}')- all_model_info = []- current_model_info = next(- (- info- for info in all_model_info- if info['model_name']- == self.config.model.removeprefix('litellm_proxy/')- ),- None,- )- if current_model_info:- self.model_info = current_model_info['model_info']- logger.debug(f'Got model info from litellm proxy: {self.model_info}')-- # Last two attempts to get model info from NAME- if not self.model_info:- try:- self.model_info = litellm.get_model_info(- self.config.model.split(':')[0]- )- # noinspection PyBroadException- except Exception:- pass- if not self.model_info:- try:- self.model_info = litellm.get_model_info(- self.config.model.split('/')[-1]- )- # noinspection PyBroadException- except Exception:- pass-- logger.debug(- f'Model info: {json.dumps({"model": self.config.model, "base_url": self.config.base_url}, indent=2)}'- )-- if self.config.model.startswith('huggingface'):- # HF doesn't support the OpenAI default value for top_p (1)- logger.debug(- f'Setting top_p to 0.9 for Hugging Face model: {self.config.model}'- )- self.config.top_p = 0.9 if self.config.top_p == 1 else self.config.top_p-- # Set max_input_tokens from model info if not explicitly set- if (- self.config.max_input_tokens is None- and self.model_info is not None- and 'max_input_tokens' in self.model_info- and isinstance(self.model_info['max_input_tokens'], int)- ):- self.config.max_input_tokens = self.model_info['max_input_tokens']-- # Set max_output_tokens from model info if not explicitly set- if self.config.max_output_tokens is None:- # Special case for Claude Sonnet models- sonnet_models = [- 'claude-3-7-sonnet',- 'claude-3.7-sonnet',- 'claude-sonnet-4',- 'claude-sonnet-4-5-20250929',- 'claude-haiku-4-5-20251001',- ]- if any(model in self.config.model for model in sonnet_models):- self.config.max_output_tokens = 64000 # litellm set max to 128k, but that requires a header to be set- # Try to get from model info- elif self.model_info is not None:- # max_output_tokens has precedence over max_tokens- if 'max_output_tokens' in self.model_info and isinstance(- self.model_info['max_output_tokens'], int- ):- self.config.max_output_tokens = self.model_info['max_output_tokens']- elif 'max_tokens' in self.model_info and isinstance(- self.model_info['max_tokens'], int- ):- self.config.max_output_tokens = self.model_info['max_tokens']-- # Initialize function calling using centralized model features- features = get_features(self.config.model)- if self.config.native_tool_calling is None:- self._function_calling_active = features.supports_function_calling- else:- self._function_calling_active = self.config.native_tool_calling-- def vision_is_active(self) -> bool:- with warnings.catch_warnings():- warnings.simplefilter('ignore')- return not self.config.disable_vision and self._supports_vision()-- def _supports_vision(self) -> bool:- """Acquire from litellm if model is vision capable.-- Returns:- bool: True if model is vision capable. Return False if model not supported by litellm.- """-- # Allow manual override via environment variable- if os.getenv('OPENHANDS_FORCE_VISION', '').lower() in (- '1',- 'true',- 'yes',- 'on',- ):- return True-- # litellm.supports_vision currently returns False for 'openai/gpt-...' or 'anthropic/claude-...' (with prefixes)- # but model_info will have the correct value for some reason.- # we can go with it, but we will need to keep an eye if model_info is correct for Vertex or other providers- # remove when litellm is updated to fix https://github.com/BerriAI/litellm/issues/5608- # Check both the full model name and the name after proxy prefix for vision support- return (- litellm.supports_vision(self.config.model)- or litellm.supports_vision(self.config.model.split('/')[-1])- or (- self.model_info is not None- and self.model_info.get('supports_vision', False)- )- )-- def is_caching_prompt_active(self) -> bool:- """Check if prompt caching is supported and enabled for current model.-- Returns:- boolean: True if prompt caching is supported and enabled for the given model.- """- if not self.config.caching_prompt:- return False- # We don't need to look-up model_info, because only Anthropic models need explicit caching breakpoints- return get_features(self.config.model).supports_prompt_cache-- def is_function_calling_active(self) -> bool:- """Returns whether function calling is supported and enabled for this LLM instance.-- The result is cached during initialization for performance.- """- return self._function_calling_active-- def _post_completion(self, response: ModelResponse) -> float:- """Post-process the completion response.-- Logs the cost and usage stats of the completion call.- """- try:- cur_cost = self._completion_cost(response)- except Exception:- cur_cost = 0-- stats = ''- if self.cost_metric_supported:- # keep track of the cost- stats = 'Cost: %.2f USD | Accumulated Cost: %.2f USD\n' % (- cur_cost,- self.metrics.accumulated_cost,- )-- # Add latency to stats if available- if self.metrics.response_latencies:- latest_latency = self.metrics.response_latencies[-1]- stats += 'Response Latency: %.3f seconds\n' % latest_latency.latency-- usage: Usage | None = response.get('usage')- response_id = response.get('id', 'unknown')-- if usage:- # keep track of the input and output tokens- prompt_tokens = usage.get('prompt_tokens', 0)- completion_tokens = usage.get('completion_tokens', 0)-- if prompt_tokens:- stats += 'Input tokens: ' + str(prompt_tokens)-- if completion_tokens:- stats += (- (' | ' if prompt_tokens else '')- + 'Output tokens: '- + str(completion_tokens)- + '\n'- )-- # read the prompt cache hit, if any- prompt_tokens_details: PromptTokensDetails = usage.get(- 'prompt_tokens_details'- )- cache_hit_tokens = (- prompt_tokens_details.cached_tokens- if prompt_tokens_details and prompt_tokens_details.cached_tokens- else 0- )- if cache_hit_tokens:- stats += 'Input tokens (cache hit): ' + str(cache_hit_tokens) + '\n'-- # For Anthropic, the cache writes have a different cost than regular input tokens- # but litellm doesn't separate them in the usage stats- # we can read it from the provider-specific extra field- model_extra = usage.get('model_extra', {})- cache_write_tokens = model_extra.get('cache_creation_input_tokens', 0)- if cache_write_tokens:- stats += 'Input tokens (cache write): ' + str(cache_write_tokens) + '\n'-- # Get context window from model info- context_window = 0- if self.model_info and 'max_input_tokens' in self.model_info:- context_window = self.model_info['max_input_tokens']- logger.debug(f'Using context window: {context_window}')-- # Record in metrics- # We'll treat cache_hit_tokens as "cache read" and cache_write_tokens as "cache write"- self.metrics.add_token_usage(- prompt_tokens=prompt_tokens,- completion_tokens=completion_tokens,- cache_read_tokens=cache_hit_tokens,- cache_write_tokens=cache_write_tokens,- context_window=context_window,- response_id=response_id,- )-- # log the stats- if stats:- logger.debug(stats)-- return cur_cost-- def get_token_count(self, messages: list[dict] | list[Message]) -> int:- """Get the number of tokens in a list of messages. Use dicts for better token counting.-- Args:- messages (list): A list of messages, either as a list of dicts or as a list of Message objects.-- Returns:- int: The number of tokens.- """- # attempt to convert Message objects to dicts, litellm expects dicts- if (- isinstance(messages, list)- and len(messages) > 0- and isinstance(messages[0], Message)- ):- logger.info(- 'Message objects now include serialized tool calls in token counting'- )- # Assert the expected type for format_messages_for_llm- assert isinstance(messages, list) and all(- isinstance(m, Message) for m in messages- ), 'Expected list of Message objects'-- # We've already asserted that messages is a list of Message objects- # Use explicit typing to satisfy mypy- messages_typed: list[Message] = messages # type: ignore- messages = self.format_messages_for_llm(messages_typed)-- # try to get the token count with the default litellm tokenizers- # or the custom tokenizer if set for this LLM configuration- try:- return int(- litellm.token_counter(- model=self.config.model,- messages=messages,- custom_tokenizer=self.tokenizer,- )- )- except Exception as e:- # limit logspam in case token count is not supported- logger.error(- f'Error getting token count for\n model {self.config.model}\n{e}'- + (- f'\ncustom_tokenizer: {self.config.custom_tokenizer}'- if self.config.custom_tokenizer is not None- else ''- )- )- return 0-- def _is_local(self) -> bool:- """Determines if the system is using a locally running LLM.-- Returns:- boolean: True if executing a local model.- """- if self.config.base_url is not None:- for substring in ['localhost', '127.0.0.1', '0.0.0.0']:- if substring in self.config.base_url:- return True- elif self.config.model is not None:- if self.config.model.startswith('ollama'):- return True- return False-- def _completion_cost(self, response: Any) -> float:- """Calculate completion cost and update metrics with running total.-- Calculate the cost of a completion response based on the model. Local models are treated as free.- Add the current cost into total cost in metrics.-- Args:- response: A response from a model invocation.-- Returns:- number: The cost of the response.- """- if not self.cost_metric_supported:- return 0.0-- extra_kwargs = {}- if (- self.config.input_cost_per_token is not None- and self.config.output_cost_per_token is not None- ):- cost_per_token = CostPerToken(- input_cost_per_token=self.config.input_cost_per_token,- output_cost_per_token=self.config.output_cost_per_token,- )- logger.debug(f'Using custom cost per token: {cost_per_token}')- extra_kwargs['custom_cost_per_token'] = cost_per_token-- # try directly get response_cost from response- _hidden_params = getattr(response, '_hidden_params', {})- cost = _hidden_params.get('additional_headers', {}).get(- 'llm_provider-x-litellm-response-cost', None- )- if cost is not None:- cost = float(cost)- logger.debug(f'Got response_cost from response: {cost}')-- try:- if cost is None:- try:- cost = litellm_completion_cost(- completion_response=response, **extra_kwargs- )- except Exception as e:- logger.debug(f'Error getting cost from litellm: {e}')-- if cost is None:- _model_name = '/'.join(self.config.model.split('/')[1:])- cost = litellm_completion_cost(- completion_response=response, model=_model_name, **extra_kwargs- )- logger.debug(- f'Using fallback model name {_model_name} to get cost: {cost}'- )- self.metrics.add_cost(float(cost))- return float(cost)- except Exception:- self.cost_metric_supported = False- logger.debug('Cost calculation not supported for this model.')- return 0.0-- def __str__(self) -> str:- if self.config.api_version:- return f'LLM(model={self.config.model}, api_version={self.config.api_version}, base_url={self.config.base_url})'- elif self.config.base_url:- return f'LLM(model={self.config.model}, base_url={self.config.base_url})'- return f'LLM(model={self.config.model})'-- def __repr__(self) -> str:- return str(self)-- def format_messages_for_llm(self, messages: Message | list[Message]) -> list[dict]:- if isinstance(messages, Message):- messages = [messages]-- # set flags to know how to serialize the messages- for message in messages:- message.cache_enabled = self.is_caching_prompt_active()- message.vision_enabled = self.vision_is_active()- message.function_calling_enabled = self.is_function_calling_active()- if 'deepseek' in self.config.model:- message.force_string_serializer = True- if 'kimi-k2-instruct' in self.config.model and 'groq' in self.config.model:- message.force_string_serializer = True- if any(- k in self.config.model- for k in (- 'openrouter/anthropic/claude-sonnet-4',- 'openrouter/anthropic/claude-opus-4-6',- 'openrouter/anthropic/claude-sonnet-4-5-20250929',- 'openrouter/anthropic/claude-haiku-4-5-20251001',- )- ):- message.force_string_serializer = True-- # let pydantic handle the serialization- return [message.model_dump() for message in messages]---def _get_openhands_llm_base_url():- # Get the API url if specified- lite_llm_api_url = os.getenv('LITE_LLM_API_URL')- if lite_llm_api_url:- return lite_llm_api_url-- # Fallback to using web_host.- web_host = os.getenv('WEB_HOST')- if web_host and ('.staging.' in web_host or web_host.startswith('staging')):- return 'https://llm-proxy.staging.all-hands.dev/'-- # Use the default- return 'https://llm-proxy.app.all-hands.dev/'diff --git a/openhands/llm/llm_registry.py b/openhands/llm/llm_registry.pydeleted file mode 100644index 457ba013b7..0000000000--- a/openhands/llm/llm_registry.py+++ /dev/null@@ -1,153 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-import copy-from typing import Any, Callable-from uuid import uuid4--from pydantic import BaseModel, ConfigDict--from openhands.core.config.agent_config import AgentConfig-from openhands.core.config.llm_config import LLMConfig-from openhands.core.config.openhands_config import OpenHandsConfig-from openhands.core.logger import openhands_logger as logger-from openhands.llm.llm import LLM---class RegistryEvent(BaseModel):- llm: LLM- service_id: str-- model_config = ConfigDict(- arbitrary_types_allowed=True,- )---class LLMRegistry:- def __init__(- self,- config: OpenHandsConfig,- agent_cls: str | None = None,- retry_listener: Callable[[int, int], None] | None = None,- ):- self.registry_id = str(uuid4())- self.config = copy.deepcopy(config)- self.retry_listner = retry_listener- self.agent_to_llm_config = self.config.get_agent_to_llm_config_map()- self.service_to_llm: dict[str, LLM] = {}- self.subscriber: Callable[[Any], None] | None = None-- selected_agent_cls = self.config.default_agent- if agent_cls:- selected_agent_cls = agent_cls-- agent_name = selected_agent_cls if selected_agent_cls is not None else 'agent'- llm_config = self.config.get_llm_config_from_agent(agent_name)- self.active_agent_llm: LLM = self.get_llm('agent', llm_config)-- def _create_new_llm(- self, service_id: str, config: LLMConfig, with_listener: bool = True- ) -> LLM:- if with_listener:- llm = LLM(- service_id=service_id, config=config, retry_listener=self.retry_listner- )- else:- llm = LLM(service_id=service_id, config=config)- self.service_to_llm[service_id] = llm- self.notify(RegistryEvent(llm=llm, service_id=service_id))- return llm-- def request_extraneous_completion(- self, service_id: str, llm_config: LLMConfig, messages: list[dict[str, str]]- ) -> str:- logger.info(f'extraneous completion: {service_id}')- if service_id not in self.service_to_llm:- self._create_new_llm(- config=llm_config, service_id=service_id, with_listener=False- )-- llm = self.service_to_llm[service_id]- response = llm.completion(messages=messages)- return response.choices[0].message.content.strip()-- def get_llm_from_agent_config(self, service_id: str, agent_config: AgentConfig):- llm_config = self.config.get_llm_config_from_agent_config(agent_config)- if service_id in self.service_to_llm:- if self.service_to_llm[service_id].config != llm_config:- # TODO: update llm config internally- # Done when agent delegates has different config, we should reuse the existing LLM- pass- return self.service_to_llm[service_id]-- return self._create_new_llm(config=llm_config, service_id=service_id)-- def get_llm(- self,- service_id: str,- config: LLMConfig | None = None,- ):- logger.info(- f'[LLM registry {self.registry_id}]: Registering service for {service_id}'- )-- # Attempting to switch configs for existing LLM- if (- service_id in self.service_to_llm- and self.service_to_llm[service_id].config != config- ):- raise ValueError(- f'Requesting same service ID {service_id} with different config, use a new service ID'- )-- if service_id in self.service_to_llm:- return self.service_to_llm[service_id]-- if not config:- raise ValueError('Requesting new LLM without specifying LLM config')-- return self._create_new_llm(config=config, service_id=service_id)-- def get_active_llm(self) -> LLM:- return self.active_agent_llm-- def get_router(self, agent_config: AgentConfig) -> 'LLM':- """- Get a router instance that inherits from LLM.- """- # Import here to avoid circular imports- from openhands.llm.router import RouterLLM-- router_name = agent_config.model_routing.router_name-- if router_name == 'noop_router':- # Return the main LLM directly (no routing)- return self.get_llm_from_agent_config('agent', agent_config)-- return RouterLLM.from_config(- agent_config=agent_config,- llm_registry=self,- retry_listener=self.retry_listner,- )-- def subscribe(self, callback: Callable[[RegistryEvent], None]) -> None:- self.subscriber = callback-- # Subscriptions happen after default llm is initialized- # Notify service of this llm- self.notify(- RegistryEvent(- llm=self.active_agent_llm, service_id=self.active_agent_llm.service_id- )- )-- def notify(self, event: RegistryEvent):- if self.subscriber:- try:- self.subscriber(event)- except Exception as e:- logger.warning(f'Failed to emit event: {e}')diff --git a/openhands/llm/llm_utils.py b/openhands/llm/llm_utils.pydeleted file mode 100644index 88ad2ec693..0000000000--- a/openhands/llm/llm_utils.py+++ /dev/null@@ -1,51 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-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 toolsdiff --git a/openhands/llm/model_features.py b/openhands/llm/model_features.pydeleted file mode 100644index cd36e90fa1..0000000000--- a/openhands/llm/model_features.py+++ /dev/null@@ -1,172 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-from __future__ import annotations--from dataclasses import dataclass-from fnmatch import fnmatch---def normalize_model_name(model: str) -> str:- """Normalize a model string to a canonical, comparable name.-- Strategy:- - Trim whitespace- - Lowercase- - If there is a '/', keep only the basename after the last '/'- (handles prefixes like openrouter/, litellm_proxy/, anthropic/, etc.)- and treat ':' inside that basename as an Ollama-style variant tag to be removed- - There is no provider:model form; providers, when present, use 'provider/model'- - Drop a trailing "-gguf" suffix if present- """- raw = (model or '').strip().lower()- if '/' in raw:- name = raw.split('/')[-1]- if ':' in name:- # Drop Ollama-style variant tag in basename- name = name.split(':', 1)[0]- else:- # No '/', keep the whole raw name (we do not support provider:model)- name = raw- if name.endswith('-gguf'):- name = name[: -len('-gguf')]- return name---def model_matches(model: str, patterns: list[str]) -> bool:- """Return True if the model matches any of the glob patterns.-- If a pattern contains a '/', it is treated as provider-qualified and matched- against the full, lowercased model string (including provider prefix).- Otherwise, it is matched against the normalized basename.- """- raw = (model or '').strip().lower()- name = normalize_model_name(model)- for pat in patterns:- pat_l = pat.lower()- if '/' in pat_l:- if fnmatch(raw, pat_l):- return True- else:- if fnmatch(name, pat_l):- return True- return False---@dataclass(frozen=True)-class ModelFeatures:- supports_function_calling: bool- supports_reasoning_effort: bool- supports_prompt_cache: bool- supports_stop_words: bool---# Pattern tables capturing current behavior. Keep patterns lowercase.-FUNCTION_CALLING_PATTERNS: list[str] = [- # Anthropic families- 'claude-3-7-sonnet*',- 'claude-3.7-sonnet*',- 'claude-sonnet-3-7-latest',- 'claude-3-5-sonnet*',- 'claude-3.5-sonnet*', # Accept dot-notation for Sonnet 3.5 as well- 'claude-3.5-haiku*',- 'claude-3-5-haiku*',- 'claude-sonnet-4*',- 'claude-opus-4*',- # OpenAI families- 'gpt-4o*',- 'gpt-4.1',- 'gpt-5*',- # o-series (keep exact o1 support per existing list)- 'o1-2024-12-17',- 'o3*',- 'o4-mini*',- # Google Gemini- 'gemini-2.5-pro*',- 'gemini-3*',- # Groq models (via groq/ provider prefix)- 'groq/*',- # Others- 'kimi-k2-0711-preview',- 'kimi-k2-instruct',- 'kimi-k2.5',- 'qwen3-coder*',- 'qwen3-coder-480b-a35b-instruct',- 'deepseek-chat',- 'grok-code-fast-1',- # GLM series - verified via official docs and litellm config- 'glm-4*',- 'glm-5*',-]--REASONING_EFFORT_PATTERNS: list[str] = [- # Mirror main behavior exactly (no unintended expansion), plus DeepSeek support- 'o1-2024-12-17',- 'o1',- 'o3',- 'o3-2025-04-16',- 'o3-mini-2025-01-31',- 'o3-mini',- 'o4-mini',- 'o4-mini-2025-04-16',- 'gemini-2.5-flash',- 'gemini-2.5-pro',- 'gemini-3.1-pro*',- 'gpt-5*',- # DeepSeek reasoning family- 'deepseek-r1-0528*',- 'claude-sonnet-4-5*',- 'claude-sonnet-4-6*',- 'claude-haiku-4-5*',- # Kimi series - verified via litellm config- 'kimi-k2.5',- # GLM series - verified via litellm config- 'glm-4*',- 'glm-5*',-]--PROMPT_CACHE_PATTERNS: list[str] = [- 'claude-3-7-sonnet*',- 'claude-3.7-sonnet*',- 'claude-sonnet-3-7-latest',- 'claude-3-5-sonnet*',- 'claude-3-5-haiku*',- 'claude-3.5-haiku*',- 'claude-3-haiku-20240307',- 'claude-3-opus-20240229',- 'claude-sonnet-4*',- 'claude-opus-4*',- 'gemini-3.1-pro*',- # Kimi series - verified via litellm config- 'kimi-k2.5',- # GLM series - verified via litellm config- 'glm-4*',- 'glm-5*',-]--SUPPORTS_STOP_WORDS_FALSE_PATTERNS: list[str] = [- # o1 family doesn't support stop words- 'o1*',- # grok-4 specific model name (basename)- 'grok-4-0709',- 'grok-code-fast-1',- # DeepSeek R1 family- 'deepseek-r1-0528*',- # Azure GPT-5 family- 'azure/gpt-5*',-]---def get_features(model: str) -> ModelFeatures:- return ModelFeatures(- supports_function_calling=model_matches(model, FUNCTION_CALLING_PATTERNS),- supports_reasoning_effort=model_matches(model, REASONING_EFFORT_PATTERNS),- supports_prompt_cache=model_matches(model, PROMPT_CACHE_PATTERNS),- supports_stop_words=not model_matches(- model, SUPPORTS_STOP_WORDS_FALSE_PATTERNS- ),- )diff --git a/openhands/llm/retry_mixin.py b/openhands/llm/retry_mixin.pydeleted file mode 100644index 04148cd34d..0000000000--- a/openhands/llm/retry_mixin.py+++ /dev/null@@ -1,108 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-from typing import Any, Callable--from tenacity import (- retry,- retry_if_exception_type,- stop_after_attempt,- wait_exponential,-)--from openhands.core.exceptions import LLMNoResponseError-from openhands.core.logger import openhands_logger as logger-from openhands.utils.tenacity_stop import stop_if_should_exit---class RetryMixin:- """Mixin class for retry logic."""-- def retry_decorator(self, num_retries: int, **kwargs: Any) -> Callable:- """Create a LLM retry decorator with customizable parameters. This is used for 429 errors, and a few other exceptions in LLM classes.-- Args:- num_retries: Number of retry attempts.- **kwargs: Keyword arguments to override default retry behavior.- Keys: retry_exceptions, retry_min_wait, retry_max_wait, retry_multiplier-- Returns:- A retry decorator with the parameters customizable in configuration.- """- retry_exceptions: tuple = kwargs.get('retry_exceptions', ())- retry_min_wait = kwargs.get('retry_min_wait')- retry_max_wait = kwargs.get('retry_max_wait')- retry_multiplier = kwargs.get('retry_multiplier')- retry_listener = kwargs.get('retry_listener')-- def before_sleep(retry_state: Any) -> None:- self.log_retry_attempt(retry_state)- if retry_listener:- retry_listener(retry_state.attempt_number, num_retries)-- # Check if the exception is LLMNoResponseError- exception = retry_state.outcome.exception()- if isinstance(exception, LLMNoResponseError):- if hasattr(retry_state, 'kwargs'):- # Only change temperature if it's zero or not set- current_temp = retry_state.kwargs.get('temperature', 0)- if current_temp == 0:- retry_state.kwargs['temperature'] = 1.0- logger.warning(- 'LLMNoResponseError detected with temperature=0, setting temperature to 1.0 for next attempt.'- )- else:- logger.warning(- f'LLMNoResponseError detected with temperature={current_temp}, keeping original temperature'- )-- retry_decorator: Callable = retry(- before_sleep=before_sleep,- stop=stop_after_attempt(num_retries) | stop_if_should_exit(),- reraise=True,- retry=(- retry_if_exception_type(retry_exceptions)- ), # retry only for these types- # TODO: Type errors here probably mean the defaults are being ignored.- wait=wait_exponential(- multiplier=retry_multiplier, # type: ignore[arg-type]- min=retry_min_wait, # type: ignore[arg-type]- max=retry_max_wait, # type: ignore[arg-type]- ),- )- return retry_decorator-- def log_retry_attempt(self, retry_state: Any) -> None:- """Log retry attempts."""- exception = retry_state.outcome.exception()-- # Add retry attempt and max retries to the exception for later use- if hasattr(retry_state, 'retry_object') and hasattr(- retry_state.retry_object, 'stop'- ):- # Get the max retries from the stop_after_attempt- stop_condition = retry_state.retry_object.stop-- # Handle both single stop conditions and stop_any (combined conditions)- stop_funcs = []- if hasattr(stop_condition, 'stops'):- # This is a stop_any object with multiple stop conditions- stop_funcs = stop_condition.stops- else:- # This is a single stop condition- stop_funcs = [stop_condition]-- for stop_func in stop_funcs:- if hasattr(stop_func, 'max_attempts'):- # Add retry information to the exception- exception.retry_attempt = retry_state.attempt_number- exception.max_retries = stop_func.max_attempts- break-- logger.error(- f'{exception}. Attempt #{retry_state.attempt_number} | You can customize retry values in the configuration.',- )diff --git a/openhands/llm/router/README.md b/openhands/llm/router/README.mddeleted file mode 100644index 1ed6223612..0000000000--- a/openhands/llm/router/README.md+++ /dev/null@@ -1,39 +0,0 @@-# Model Routing Module--**β οΈ Experimental Feature**: This module is experimental and under active development.--## Overview--Model routing enables OpenHands to switch between different LLM models during a conversation. An example use case is routing between a primary (expensive, multimodal) model and a secondary (cheaper, text-only) model.--## Available Routers--- **`noop_router`** (default): No routing, always uses primary LLM-- **`multimodal_router`**: A router that switches based on:- - Routes to primary model for images or when secondary model's context limit is exceeded- - Uses secondary model for text-only requests within its context limit--## Configuration--Add to your `config.toml`:--```toml-# Main LLM (primary model)-[llm]-model = "claude-sonnet-4"-api_key = "your-api-key"--# Secondary model for routing-[llm.secondary_model]-model = "kimi-k2"-api_key = "your-api-key"-for_routing = true--# Enable routing-[model_routing]-router_name = "multimodal_router"-```--## Extending--Create custom routers by inheriting from `BaseRouter` and implementing `set_active_llm()`. Register in `ROUTER_REGISTRY`.diff --git a/openhands/llm/router/__init__.py b/openhands/llm/router/__init__.pydeleted file mode 100644index 877bbbb477..0000000000--- a/openhands/llm/router/__init__.py+++ /dev/null@@ -1,8 +0,0 @@-from openhands.llm.router.base import ROUTER_LLM_REGISTRY, RouterLLM-from openhands.llm.router.rule_based.impl import MultimodalRouter--__all__ = [- 'RouterLLM',- 'ROUTER_LLM_REGISTRY',- 'MultimodalRouter',-]diff --git a/openhands/llm/router/base.py b/openhands/llm/router/base.pydeleted file mode 100644index a842cf34c2..0000000000--- a/openhands/llm/router/base.py+++ /dev/null@@ -1,171 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-import copy-from abc import abstractmethod-from typing import TYPE_CHECKING, Any, Callable--from openhands.core.config import AgentConfig-from openhands.core.logger import openhands_logger as logger-from openhands.core.message import Message-from openhands.llm.llm import LLM-from openhands.llm.metrics import Metrics--if TYPE_CHECKING:- from openhands.llm.llm_registry import LLMRegistry--ROUTER_LLM_REGISTRY: dict[str, type['RouterLLM']] = {}---class RouterLLM(LLM):- """- Base class for multiple LLM acting as a unified LLM.-- This class provides a foundation for implementing model routing by inheriting from LLM,- allowing routers to work with multiple underlying LLM models while presenting a unified- LLM interface to consumers.-- Key features:- - Works with multiple LLMs configured via llms_for_routing- - Delegates all other operations/properties to the selected LLM- - Provides routing interface through _select_llm() method- """-- def __init__(- self,- agent_config: AgentConfig,- llm_registry: 'LLMRegistry',- service_id: str = 'router_llm',- metrics: Metrics | None = None,- retry_listener: Callable[[int, int], None] | None = None,- ):- """- Initialize RouterLLM with multiple LLM support.- """- self.llm_registry = llm_registry- self.model_routing_config = agent_config.model_routing-- # Get the primary agent LLM- self.primary_llm = llm_registry.get_llm_from_agent_config('agent', agent_config)-- # Instantiate all the LLM instances for routing- llms_for_routing_config = self.model_routing_config.llms_for_routing- self.llms_for_routing = {- config_name: self.llm_registry.get_llm(- f'llm_for_routing.{config_name}', config=llm_config- )- for config_name, llm_config in llms_for_routing_config.items()- }-- # All available LLMs for routing (set this BEFORE calling super().__init__)- self.available_llms = {'primary': self.primary_llm, **self.llms_for_routing}-- # Create router config based on primary LLM- router_config = copy.deepcopy(self.primary_llm.config)-- # Update model name to indicate this is a router- llm_names = [self.primary_llm.config.model]- if self.model_routing_config.llms_for_routing:- llm_names.extend(- config.model- for config in self.model_routing_config.llms_for_routing.values()- )- router_config.model = f'router({",".join(llm_names)})'-- # Initialize parent LLM class- super().__init__(- config=router_config,- service_id=service_id,- metrics=metrics,- retry_listener=retry_listener,- )-- # Current LLM state- self._current_llm = self.primary_llm # Default to primary LLM- self._last_routing_decision = 'primary'-- logger.info(- f'RouterLLM initialized with {len(self.available_llms)} LLMs: {list(self.available_llms.keys())}'- )-- @abstractmethod- def _select_llm(self, messages: list[Message]) -> str:- """- Select which LLM to use based on messages and events.- """- pass-- def _get_llm_by_key(self, llm_key: str) -> LLM:- """- Get LLM instance by key.- """- if llm_key not in self.available_llms:- raise ValueError(- f'Unknown LLM key: {llm_key}. Available: {list(self.available_llms.keys())}'- )- return self.available_llms[llm_key]-- @property- def completion(self) -> Callable:- """- Override completion to route to appropriate LLM.-- This method intercepts completion calls and routes them to the appropriate- underlying LLM based on the routing logic implemented in _select_llm().- """-- def router_completion(*args: Any, **kwargs: Any) -> Any:- # Extract messages for routing decision- messages = kwargs.get('messages', [])- if args and not messages:- messages = args[0] if args else []-- # Select appropriate LLM- selected_llm_key = self._select_llm(messages)- selected_llm = self._get_llm_by_key(selected_llm_key)-- # Update current state- self._current_llm = selected_llm- self._last_routing_decision = selected_llm_key-- logger.debug(- f'RouterLLM routing to {selected_llm_key} ({selected_llm.config.model})'- )-- # Delegate to selected LLM- return selected_llm.completion(*args, **kwargs)-- return router_completion-- def __str__(self) -> str:- """String representation of the router."""- return f'{self.__class__.__name__}(llms={list(self.available_llms.keys())})'-- def __repr__(self) -> str:- """Detailed string representation of the router."""- return (- f'{self.__class__.__name__}('- f'primary={self.primary_llm.config.model}, '- f'routing_llms={[llm.config.model for llm in self.llms_for_routing.values()]}, '- f'current={self._last_routing_decision})'- )-- def __getattr__(self, name):- """Delegate other attributes/methods to the active LLM."""- return getattr(self._current_llm, name)-- @classmethod- def from_config(- cls, llm_registry: 'LLMRegistry', agent_config: AgentConfig, **kwargs- ) -> 'RouterLLM':- """Factory method to create a RouterLLM instance from configuration."""- router_cls = ROUTER_LLM_REGISTRY.get(agent_config.model_routing.router_name)- if not router_cls:- raise ValueError(- f'Router LLM {agent_config.model_routing.router_name} not found.'- )- return router_cls(agent_config, llm_registry, **kwargs)diff --git a/openhands/llm/router/rule_based/impl.py b/openhands/llm/router/rule_based/impl.pydeleted file mode 100644index 8bba9b7e07..0000000000--- a/openhands/llm/router/rule_based/impl.py+++ /dev/null@@ -1,81 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-from openhands.core.config import AgentConfig-from openhands.core.logger import openhands_logger as logger-from openhands.core.message import Message-from openhands.llm.llm import LLM-from openhands.llm.llm_registry import LLMRegistry-from openhands.llm.router.base import ROUTER_LLM_REGISTRY, RouterLLM---class MultimodalRouter(RouterLLM):- SECONDARY_MODEL_CONFIG_NAME = 'secondary_model'- ROUTER_NAME = 'multimodal_router'-- def __init__(- self,- agent_config: AgentConfig,- llm_registry: LLMRegistry,- **kwargs,- ):- super().__init__(agent_config, llm_registry, **kwargs)-- self._validate_model_routing_config(self.llms_for_routing)-- # States- self.max_token_exceeded = False-- def _select_llm(self, messages: list[Message]) -> str:- """Select LLM based on multimodal content and token limits."""- route_to_primary = False-- # Check for multimodal content in messages- for message in messages:- if message.contains_image:- logger.info(- 'Multimodal content detected in messages. Routing to the primary model.'- )- route_to_primary = True-- if not route_to_primary and self.max_token_exceeded:- route_to_primary = True-- # Check if `messages` exceeds context window of the secondary model- # Assuming the secondary model has a lower context window limit compared to the primary model- secondary_llm = self.available_llms.get(self.SECONDARY_MODEL_CONFIG_NAME)- if secondary_llm and (- secondary_llm.config.max_input_tokens- and secondary_llm.get_token_count(messages)- > secondary_llm.config.max_input_tokens- ):- logger.warning(- f"Messages having {secondary_llm.get_token_count(messages)} tokens, exceed secondary model's max input tokens ({secondary_llm.config.max_input_tokens} tokens). "- 'Routing to the primary model.'- )- self.max_token_exceeded = True- route_to_primary = True-- if route_to_primary:- logger.info('Routing to the primary model...')- return 'primary'- else:- logger.info('Routing to the secondary model...')- return self.SECONDARY_MODEL_CONFIG_NAME-- def vision_is_active(self):- return self.primary_llm.vision_is_active()-- def _validate_model_routing_config(self, llms_for_routing: dict[str, LLM]):- if self.SECONDARY_MODEL_CONFIG_NAME not in llms_for_routing:- raise ValueError(- f'Secondary LLM config {self.SECONDARY_MODEL_CONFIG_NAME} not found.'- )---# Register the router-ROUTER_LLM_REGISTRY[MultimodalRouter.ROUTER_NAME] = MultimodalRouterdiff --git a/openhands/llm/streaming_llm.py b/openhands/llm/streaming_llm.pydeleted file mode 100644index ab2961552c..0000000000--- a/openhands/llm/streaming_llm.py+++ /dev/null@@ -1,123 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-import asyncio-from functools import partial-from typing import Any, Callable--from openhands.core.exceptions import UserCancelledError-from openhands.core.logger import openhands_logger as logger-from openhands.llm.async_llm import LLM_RETRY_EXCEPTIONS, AsyncLLM-from openhands.llm.model_features import get_features---class StreamingLLM(AsyncLLM):- """Streaming LLM class."""-- def __init__(self, *args: Any, **kwargs: Any) -> None:- super().__init__(*args, **kwargs)-- self._async_streaming_completion = partial(- self._call_acompletion,- model=self.config.model,- api_key=self.config.api_key.get_secret_value()- if self.config.api_key- else None,- base_url=self.config.base_url,- api_version=self.config.api_version,- custom_llm_provider=self.config.custom_llm_provider,- max_tokens=self.config.max_output_tokens,- timeout=self.config.timeout,- temperature=self.config.temperature,- top_p=self.config.top_p,- drop_params=self.config.drop_params,- stream=True, # Ensure streaming is enabled- )-- async_streaming_completion_unwrapped = self._async_streaming_completion-- @self.retry_decorator(- num_retries=self.config.num_retries,- retry_exceptions=LLM_RETRY_EXCEPTIONS,- retry_min_wait=self.config.retry_min_wait,- retry_max_wait=self.config.retry_max_wait,- retry_multiplier=self.config.retry_multiplier,- )- async def async_streaming_completion_wrapper(*args: Any, **kwargs: Any) -> Any:- messages: list[dict[str, Any]] | dict[str, Any] = []-- # some callers might send the model and messages directly- # litellm allows positional args, like completion(model, messages, **kwargs)- # see llm.py for more details- if len(args) > 1:- messages = args[1] if len(args) > 1 else args[0]- kwargs['messages'] = messages-- # remove the first args, they're sent in kwargs- args = args[2:]- elif 'messages' in kwargs:- messages = kwargs['messages']-- # ensure we work with a list of messages- messages = messages if isinstance(messages, list) else [messages]-- # if we have no messages, something went very wrong- if not messages:- raise ValueError(- 'The messages list is empty. At least one message is required.'- )-- # Set reasoning effort for models that support it, only if explicitly provided- if (- get_features(self.config.model).supports_reasoning_effort- and self.config.reasoning_effort is not None- ):- kwargs['reasoning_effort'] = self.config.reasoning_effort-- self.log_prompt(messages)-- try:- # Directly call and await litellm_acompletion- resp = await async_streaming_completion_unwrapped(*args, **kwargs)-- # For streaming we iterate over the chunks- async for chunk in resp:- # Check for cancellation before yielding the chunk- if (- hasattr(self.config, 'on_cancel_requested_fn')- and self.config.on_cancel_requested_fn is not None- and await self.config.on_cancel_requested_fn()- ):- raise UserCancelledError(- 'LLM request cancelled due to CANCELLED state'- )- # with streaming, it is "delta", not "message"!- message_back = chunk['choices'][0]['delta'].get('content', '')- if message_back:- self.log_response(message_back)- self._post_completion(chunk)-- yield chunk-- except UserCancelledError:- logger.debug('LLM request cancelled by user.')- raise- except Exception as e:- logger.error(f'Completion Error occurred:\n{e}')- raise-- finally:- # sleep for 0.1 seconds to allow the stream to be flushed- if kwargs.get('stream', False):- await asyncio.sleep(0.1)-- self._async_streaming_completion = async_streaming_completion_wrapper-- @property- def async_streaming_completion(self) -> Callable:- """Decorator for the async litellm acompletion function with streaming."""- return self._async_streaming_completiondiff --git a/openhands/llm/tool_names.py b/openhands/llm/tool_names.pydeleted file mode 100644index 7c45fd63fe..0000000000--- a/openhands/llm/tool_names.py+++ /dev/null@@ -1,15 +0,0 @@-# IMPORTANT: LEGACY V0 CODE - Deprecated since version 1.0.0, scheduled for removal April 1, 2026-# This file is part of the legacy (V0) implementation of OpenHands and will be removed soon as we complete the migration to V1.-# OpenHands V1 uses the Software Agent SDK for the agentic core and runs a new application server. Please refer to:-# - V1 agentic core (SDK): https://github.com/OpenHands/software-agent-sdk-# - V1 application server (in this repo): openhands/app_server/-# Unless you are working on deprecation, please avoid extending this legacy file and consult the V1 codepaths above.-# Tag: Legacy-V0-"""Constants for tool names used in function calling."""--EXECUTE_BASH_TOOL_NAME = 'execute_bash'-STR_REPLACE_EDITOR_TOOL_NAME = 'str_replace_editor'-BROWSER_TOOL_NAME = 'browser'-FINISH_TOOL_NAME = 'finish'-LLM_BASED_EDIT_TOOL_NAME = 'edit_file'-TASK_TRACKER_TOOL_NAME = 'task_tracker'diff --git a/openhands/utils/conversation_summary.py b/openhands/utils/conversation_summary.pydeleted file mode 100644index 9cacd41741..0000000000--- a/openhands/utils/conversation_summary.py+++ /dev/null@@ -1,156 +0,0 @@-"""Utility functions for generating conversation summaries."""--from typing import Optional--from pydantic import SecretStr--from openhands.core.config import LLMConfig-from openhands.core.logger import openhands_logger as logger-from openhands.events.action.message import MessageAction-from openhands.events.event import EventSource-from openhands.events.event_store import EventStore-from openhands.llm.llm_registry import LLMRegistry-from openhands.storage.data_models.settings import Settings-from openhands.storage.files import FileStore-from openhands.utils.environment import get_effective_llm_base_url---async def generate_conversation_title(- message: str,- llm_config: LLMConfig,- llm_registry: LLMRegistry,- max_length: int = 50,-) -> Optional[str]:- """Generate a concise title for a conversation based on the first user message.-- Args:- message: The first user message in the conversation.- llm_config: The LLM configuration to use for generating the title.- max_length: The maximum length of the generated title.-- Returns:- A concise title for the conversation, or None if generation fails.- """- if not message or message.strip() == '':- return None-- # Truncate very long messages to avoid excessive token usage- if len(message) > 1000:- truncated_message = message[:1000] + '...(truncated)'- else:- truncated_message = message-- try:- # Create a simple prompt for the LLM to generate a title- messages = [- {- 'role': 'system',- 'content': 'You are a helpful assistant that generates concise, descriptive titles for conversations with OpenHands. OpenHands is a helpful AI agent that can interact with a computer to solve tasks using bash terminal, file editor, and browser. Given a user message (which may be truncated), generate a concise, descriptive title for the conversation. Return only the title, with no additional text, quotes, or explanations.',- },- {- 'role': 'user',- 'content': f'Generate a title (maximum {max_length} characters) for a conversation that starts with this message:\n\n{truncated_message}',- },- ]-- title = llm_registry.request_extraneous_completion(- 'conversation_title_creator', llm_config, messages- )-- # Ensure the title isn't too long- if len(title) > max_length:- title = title[: max_length - 3] + '...'-- return title- except Exception as e:- logger.error(f'Error generating conversation title: {e}')- return None---def get_default_conversation_title(conversation_id: str) -> str:- """Generate a default title for a conversation based on its ID.-- Args:- conversation_id: The ID of the conversation-- Returns:- A default title string- """- return f'Conversation {conversation_id[:5]}'---async def auto_generate_title(- conversation_id: str,- user_id: str | None,- file_store: FileStore,- settings: Settings,- llm_registry: LLMRegistry,-) -> str:- """Auto-generate a title for a conversation based on the first user message.- Uses LLM-based title generation if available, otherwise falls back to a simple truncation.-- Args:- conversation_id: The ID of the conversation- user_id: The ID of the user-- Returns:- A generated title string- """- try:- # Create an event store for the conversation- event_store = EventStore(conversation_id, file_store, user_id)-- # Find the first user message- first_user_message = None- for event in event_store.search_events():- if (- event.source == EventSource.USER- and isinstance(event, MessageAction)- and event.content- and event.content.strip()- ):- first_user_message = event.content- break-- if first_user_message:- # Get LLM config from user settings- try:- if settings:- agent_settings = settings.agent_settings- settings_base_url = agent_settings.llm.base_url- effective_base_url = get_effective_llm_base_url(- agent_settings.llm.model,- settings_base_url,- )- raw_api_key = settings.agent_settings.llm.api_key- api_key = (- SecretStr(raw_api_key)- if isinstance(raw_api_key, str)- else raw_api_key- )- llm_config = LLMConfig(- model=agent_settings.llm.model,- api_key=api_key,- base_url=effective_base_url,- )-- # Try to generate title using LLM- llm_title = await generate_conversation_title(- first_user_message, llm_config, llm_registry- )- if llm_title:- logger.info(f'Generated title using LLM: {llm_title}')- return llm_title- except Exception as e:- logger.error(f'Error using LLM for title generation: {e}')-- # Fall back to simple truncation if LLM generation fails or is unavailable- first_user_message = first_user_message.strip()- title = first_user_message[:30]- if len(first_user_message) > 30:- title += '...'- logger.info(f'Generated title using truncation: {title}')- return title- except Exception as e:- logger.error(f'Error generating title: {str(e)}')- return ''diff --git a/tests/unit/events/test_event_serialization.py b/tests/unit/events/test_event_serialization.pyindex 1f0b5bfe0f..3fdaab61bb 100644--- a/tests/unit/events/test_event_serialization.py+++ b/tests/unit/events/test_event_serialization.py@@ -1,8 +1,8 @@from openhands.events.action import CmdRunAction, MessageActionfrom openhands.events.action.action import ActionSecurityRisk+from openhands.events.metrics import Cost, Metrics, ResponseLatency, TokenUsagefrom openhands.events.observation import CmdOutputMetadata, CmdOutputObservationfrom openhands.events.serialization import event_from_dict, event_to_dict-from openhands.llm.metrics import Cost, Metrics, ResponseLatency, TokenUsagedef test_command_output_success_serialization():diff --git a/tests/unit/llm/__init__.py b/tests/unit/llm/__init__.pydeleted file mode 100644index e69de29bb2..0000000000diff --git a/tests/unit/llm/test_acompletion.py b/tests/unit/llm/test_acompletion.pydeleted file mode 100644index 434bd676f7..0000000000--- a/tests/unit/llm/test_acompletion.py+++ /dev/null@@ -1,196 +0,0 @@-import asyncio-from contextlib import contextmanager-from unittest.mock import AsyncMock, MagicMock, patch--import pytest--from openhands.core.config import load_openhands_config-from openhands.core.exceptions import UserCancelledError-from openhands.llm.async_llm import AsyncLLM-from openhands.llm.llm import LLM-from openhands.llm.streaming_llm import StreamingLLM--config = load_openhands_config()---def test_llm():- return _get_llm(LLM)---def _get_llm(type_: type[LLM]):- with _patch_http():- return type_(config=config.get_llm_config(), service_id='test_service')---def mock_response():- return [- {'choices': [{'delta': {'content': 'This is a'}}]},- {'choices': [{'delta': {'content': ' test'}}]},- {'choices': [{'delta': {'content': ' message.'}}]},- {'choices': [{'delta': {'content': ' It is'}}]},- {'choices': [{'delta': {'content': ' a bit'}}]},- {'choices': [{'delta': {'content': ' longer'}}]},- {'choices': [{'delta': {'content': ' than'}}]},- {'choices': [{'delta': {'content': ' the'}}]},- {'choices': [{'delta': {'content': ' previous'}}]},- {'choices': [{'delta': {'content': ' one,'}}]},- {'choices': [{'delta': {'content': ' but'}}]},- {'choices': [{'delta': {'content': ' hopefully'}}]},- {'choices': [{'delta': {'content': ' still'}}]},- {'choices': [{'delta': {'content': ' short'}}]},- {'choices': [{'delta': {'content': ' enough.'}}]},- ]---@contextmanager-def _patch_http():- with patch('openhands.llm.llm.httpx.get', MagicMock()) as mock_http:- mock_http.json.return_value = {- 'data': [- {'model_name': 'some_model'},- {'model_name': 'another_model'},- ]- }- yield---async def test_acompletion_non_streaming():- with patch.object(AsyncLLM, '_call_acompletion') as mock_call_acompletion:- mock_response = {- 'choices': [{'message': {'content': 'This is a test message.'}}]- }- mock_call_acompletion.return_value = mock_response- test_llm = _get_llm(AsyncLLM)- response = await test_llm.async_completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- drop_params=True,- )- # Assertions for non-streaming completion- assert response['choices'][0]['message']['content'] != ''---async def test_acompletion_streaming(mock_response):- with patch.object(StreamingLLM, '_call_acompletion') as mock_call_acompletion:- mock_call_acompletion.return_value.__aiter__.return_value = iter(mock_response)- test_llm = _get_llm(StreamingLLM)- async for chunk in test_llm.async_streaming_completion(- messages=[{'role': 'user', 'content': 'Hello!'}], stream=True- ):- print(f'Chunk: {chunk["choices"][0]["delta"]["content"]}')- # Assertions for streaming completion- assert chunk['choices'][0]['delta']['content'] in [- r['choices'][0]['delta']['content'] for r in mock_response- ]---async def test_completion(test_llm):- with patch.object(LLM, 'completion') as mock_completion:- mock_completion.return_value = {- 'choices': [{'message': {'content': 'This is a test message.'}}]- }- response = test_llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}])- assert response['choices'][0]['message']['content'] == 'This is a test message.'--[email protected]('cancel_delay', [0.1, 0.3, 0.5, 0.7, 0.9])-async def test_async_completion_with_user_cancellation(cancel_delay):- cancel_event = asyncio.Event()-- async def mock_on_cancel_requested():- is_set = cancel_event.is_set()- print(f'Cancel requested: {is_set}')- return is_set-- async def mock_acompletion(*args, **kwargs):- print('Starting mock_acompletion')- for i in range(20): # Increased iterations for longer running task- print(f'mock_acompletion iteration {i}')- await asyncio.sleep(0.1)- if await mock_on_cancel_requested():- print('Cancellation detected in mock_acompletion')- raise UserCancelledError('LLM request cancelled by user')- print('Completing mock_acompletion without cancellation')- return {'choices': [{'message': {'content': 'This is a test message.'}}]}-- with patch.object(- AsyncLLM, '_call_acompletion', new_callable=AsyncMock- ) as mock_call_acompletion:- mock_call_acompletion.side_effect = mock_acompletion- test_llm = _get_llm(AsyncLLM)-- async def cancel_after_delay():- print(f'Starting cancel_after_delay with delay {cancel_delay}')- await asyncio.sleep(cancel_delay)- print('Setting cancel event')- cancel_event.set()-- with pytest.raises(UserCancelledError):- await asyncio.gather(- test_llm.async_completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- ),- cancel_after_delay(),- )-- # Ensure the mock was called- mock_call_acompletion.assert_called_once()--[email protected]('cancel_after_chunks', [1, 3, 5, 7, 9])-async def test_async_streaming_completion_with_user_cancellation(cancel_after_chunks):- cancel_requested = False-- test_messages = [- 'This is ',- 'a test ',- 'message ',- 'with ',- 'multiple ',- 'chunks ',- 'to ',- 'simulate ',- 'a ',- 'longer ',- 'streaming ',- 'response.',- ]-- async def mock_acompletion(*args, **kwargs):- for i, content in enumerate(test_messages):- yield {'choices': [{'delta': {'content': content}}]}- if i + 1 == cancel_after_chunks:- nonlocal cancel_requested- cancel_requested = True- if cancel_requested:- raise UserCancelledError('LLM request cancelled by user')- await asyncio.sleep(0.05) # Simulate some delay between chunks-- with patch.object(- AsyncLLM, '_call_acompletion', new_callable=AsyncMock- ) as mock_call_acompletion:- mock_call_acompletion.return_value = mock_acompletion()- test_llm = _get_llm(StreamingLLM)-- received_chunks = []- with pytest.raises(UserCancelledError):- async for chunk in test_llm.async_streaming_completion(- messages=[{'role': 'user', 'content': 'Hello!'}], stream=True- ):- received_chunks.append(chunk['choices'][0]['delta']['content'])- print(f'Chunk: {chunk["choices"][0]["delta"]["content"]}')-- # Assert that we received the expected number of chunks before cancellation- assert len(received_chunks) == cancel_after_chunks- assert received_chunks == test_messages[:cancel_after_chunks]-- # Ensure the mock was called- mock_call_acompletion.assert_called_once()diff --git a/tests/unit/llm/test_api_connection_error_retry.py b/tests/unit/llm/test_api_connection_error_retry.pydeleted file mode 100644index b88c170079..0000000000--- a/tests/unit/llm/test_api_connection_error_retry.py+++ /dev/null@@ -1,110 +0,0 @@-from unittest.mock import patch--import pytest-from litellm.exceptions import APIConnectionError, BadGatewayError--from openhands.core.config import LLMConfig-from openhands.llm.llm import LLM---def default_config():- return LLMConfig(- model='gpt-4o',- api_key='test_key',- num_retries=2,- retry_min_wait=1,- retry_max_wait=2,- )---@patch('openhands.llm.llm.litellm_completion')-def test_completion_retries_api_connection_error(- mock_litellm_completion, default_config-):- """Test that APIConnectionError is properly retried."""- # Mock the litellm_completion to first raise an APIConnectionError, then return a successful response- mock_litellm_completion.side_effect = [- APIConnectionError(- message='API connection error',- llm_provider='test_provider',- model='test_model',- ),- {'choices': [{'message': {'content': 'Retry successful'}}]},- ]-- # Create an LLM instance and call completion- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- # Verify that the retry was successful- assert response['choices'][0]['message']['content'] == 'Retry successful'- assert mock_litellm_completion.call_count == 2 # Initial call + 1 retry---@patch('openhands.llm.llm.litellm_completion')-def test_completion_max_retries_api_connection_error(- mock_litellm_completion, default_config-):- """Test that APIConnectionError respects max retries."""- # Mock the litellm_completion to raise APIConnectionError multiple times- mock_litellm_completion.side_effect = [- APIConnectionError(- message='API connection error 1',- llm_provider='test_provider',- model='test_model',- ),- APIConnectionError(- message='API connection error 2',- llm_provider='test_provider',- model='test_model',- ),- APIConnectionError(- message='API connection error 3',- llm_provider='test_provider',- model='test_model',- ),- ]-- # Create an LLM instance and call completion- llm = LLM(config=default_config, service_id='test-service')-- # The completion should raise an APIConnectionError after exhausting all retries- with pytest.raises(APIConnectionError) as excinfo:- llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- # Verify that the correct number of retries were attempted- # The actual behavior is that it tries the initial call + num_retries (not +1)- assert mock_litellm_completion.call_count == default_config.num_retries-- # The exception doesn't contain retry information in the current implementation- # Just verify that we got an APIConnectionError- assert 'API connection error' in str(excinfo.value)---@patch('openhands.llm.llm.litellm_completion')-def test_completion_retries_bad_gateway_error(mock_litellm_completion, default_config):- """Test that BadGatewayError is properly retried."""- mock_litellm_completion.side_effect = [- BadGatewayError(- message='Bad gateway',- llm_provider='test_provider',- model='test_model',- ),- {'choices': [{'message': {'content': 'Retry successful'}}]},- ]-- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- assert response['choices'][0]['message']['content'] == 'Retry successful'- assert mock_litellm_completion.call_count == 2diff --git a/tests/unit/llm/test_llm.py b/tests/unit/llm/test_llm.pydeleted file mode 100644index e27bc354d9..0000000000--- a/tests/unit/llm/test_llm.py+++ /dev/null@@ -1,1549 +0,0 @@-import copy-import tempfile-from pathlib import Path-from unittest.mock import MagicMock, patch--import pytest-from litellm import PromptTokensDetails-from litellm.exceptions import (- RateLimitError,-)--from openhands.core.config import LLMConfig-from openhands.core.exceptions import LLMNoResponseError, OperationCancelled-from openhands.core.message import Message, TextContent-from openhands.llm.async_llm import AsyncLLM-from openhands.llm.llm import LLM-from openhands.llm.metrics import Metrics, TokenUsage-from openhands.llm.streaming_llm import StreamingLLM--[email protected](autouse=True)-def mock_logger(monkeypatch):- # suppress logging of completion data to file- mock_logger = MagicMock()- monkeypatch.setattr('openhands.llm.debug_mixin.llm_prompt_logger', mock_logger)- monkeypatch.setattr('openhands.llm.debug_mixin.llm_response_logger', mock_logger)- monkeypatch.setattr('openhands.llm.llm.logger', mock_logger)- return mock_logger---def default_config():- return LLMConfig(- model='gpt-4o',- api_key='test_key',- num_retries=2,- retry_min_wait=1,- retry_max_wait=2,- )---def test_llm_init_with_default_config(default_config):- llm = LLM(default_config, service_id='test-service')- assert llm.config.model == 'gpt-4o'- assert llm.config.api_key.get_secret_value() == 'test_key'- assert isinstance(llm.metrics, Metrics)- assert llm.metrics.model_name == 'gpt-4o'---def test_token_usage_add():- """Test that TokenUsage instances can be added together."""- # Create two TokenUsage instances- usage1 = TokenUsage(- model='model1',- prompt_tokens=10,- completion_tokens=5,- cache_read_tokens=3,- cache_write_tokens=2,- response_id='response-1',- )-- usage2 = TokenUsage(- model='model2',- prompt_tokens=8,- completion_tokens=6,- cache_read_tokens=2,- cache_write_tokens=4,- response_id='response-2',- )-- # Add them together- combined = usage1 + usage2-- # Verify the result- assert combined.model == 'model1' # Should keep the model from the first instance- assert combined.prompt_tokens == 18 # 10 + 8- assert combined.completion_tokens == 11 # 5 + 6- assert combined.cache_read_tokens == 5 # 3 + 2- assert combined.cache_write_tokens == 6 # 2 + 4- assert (- combined.response_id == 'response-1'- ) # Should keep the response_id from the first instance---def test_metrics_merge_accumulated_token_usage():- """Test that accumulated token usage is properly merged between two Metrics instances."""- # Create two Metrics instances- metrics1 = Metrics(model_name='model1')- metrics2 = Metrics(model_name='model2')-- # Add token usage to each- metrics1.add_token_usage(10, 5, 3, 2, 1000, 'response-1')- metrics2.add_token_usage(8, 6, 2, 4, 1000, 'response-2')-- # Verify initial accumulated token usage- metrics1_data = metrics1.get()- accumulated1 = metrics1_data['accumulated_token_usage']- assert accumulated1['prompt_tokens'] == 10- assert accumulated1['completion_tokens'] == 5- assert accumulated1['cache_read_tokens'] == 3- assert accumulated1['cache_write_tokens'] == 2-- metrics2_data = metrics2.get()- accumulated2 = metrics2_data['accumulated_token_usage']- assert accumulated2['prompt_tokens'] == 8- assert accumulated2['completion_tokens'] == 6- assert accumulated2['cache_read_tokens'] == 2- assert accumulated2['cache_write_tokens'] == 4-- # Merge metrics2 into metrics1- metrics1.merge(metrics2)-- # Verify merged accumulated token usage- merged_data = metrics1.get()- merged_accumulated = merged_data['accumulated_token_usage']- assert merged_accumulated['prompt_tokens'] == 18 # 10 + 8- assert merged_accumulated['completion_tokens'] == 11 # 5 + 6- assert merged_accumulated['cache_read_tokens'] == 5 # 3 + 2- assert merged_accumulated['cache_write_tokens'] == 6 # 2 + 4-- # Verify individual token usage records are maintained- token_usages = merged_data['token_usages']- assert len(token_usages) == 2- assert token_usages[0]['response_id'] == 'response-1'- assert token_usages[1]['response_id'] == 'response-2'---@patch('openhands.llm.llm.litellm.get_model_info')-def test_llm_init_with_model_info(mock_get_model_info, default_config):- mock_get_model_info.return_value = {- 'max_input_tokens': 8000,- 'max_output_tokens': 2000,- }- llm = LLM(default_config, service_id='test-service')- llm.init_model_info()- assert llm.config.max_input_tokens == 8000- assert llm.config.max_output_tokens == 2000---@patch('openhands.llm.llm.litellm.get_model_info')-def test_llm_init_without_model_info(mock_get_model_info, default_config):- mock_get_model_info.side_effect = Exception('Model info not available')- llm = LLM(default_config, service_id='test-service')- llm.init_model_info()- assert llm.config.max_input_tokens is None- assert llm.config.max_output_tokens is None---def test_llm_init_with_custom_config():- custom_config = LLMConfig(- model='custom-model',- api_key='custom_key',- max_input_tokens=5000,- max_output_tokens=1500,- temperature=0.8,- top_p=0.9,- top_k=None,- )- llm = LLM(custom_config, service_id='test-service')- assert llm.config.model == 'custom-model'- assert llm.config.api_key.get_secret_value() == 'custom_key'- assert llm.config.max_input_tokens == 5000- assert llm.config.max_output_tokens == 1500- assert llm.config.temperature == 0.8- assert llm.config.top_p == 0.9- assert llm.config.top_k is None---@patch('openhands.llm.llm.litellm_completion')-def test_llm_top_k_in_completion_when_set(mock_litellm_completion):- # Create a config with top_k set- config_with_top_k = LLMConfig(top_k=50)- llm = LLM(config_with_top_k, service_id='test-service')-- # Define a side effect function to check top_k- def side_effect(*args, **kwargs):- assert 'top_k' in kwargs- assert kwargs['top_k'] == 50- return {'choices': [{'message': {'content': 'Mocked response'}}]}-- mock_litellm_completion.side_effect = side_effect-- # Call completion- llm.completion(messages=[{'role': 'system', 'content': 'Test message'}])---@patch('openhands.llm.llm.litellm_completion')-def test_llm_top_k_not_in_completion_when_none(mock_litellm_completion):- # Create a config with top_k set to None- config_without_top_k = LLMConfig(top_k=None)- llm = LLM(config_without_top_k, service_id='test-service')-- # Define a side effect function to check top_k- def side_effect(*args, **kwargs):- assert 'top_k' not in kwargs- return {'choices': [{'message': {'content': 'Mocked response'}}]}-- mock_litellm_completion.side_effect = side_effect-- # Call completion- llm.completion(messages=[{'role': 'system', 'content': 'Test message'}])---@patch('openhands.llm.llm.litellm_completion')-def test_completion_kwargs_passed_to_litellm(mock_litellm_completion):- # Create a config with custom completion_kwargs- config_with_completion_kwargs = LLMConfig(- completion_kwargs={'custom_param': 'custom_value', 'another_param': 42}- )- llm = LLM(config_with_completion_kwargs, service_id='test-service')-- # Define a side effect function to check completion_kwargs are passed- def side_effect(*args, **kwargs):- assert 'custom_param' in kwargs- assert kwargs['custom_param'] == 'custom_value'- assert 'another_param' in kwargs- assert kwargs['another_param'] == 42- return {'choices': [{'message': {'content': 'Mocked response'}}]}-- mock_litellm_completion.side_effect = side_effect-- # Call completion- llm.completion(messages=[{'role': 'system', 'content': 'Test message'}])---def test_llm_init_with_metrics():- config = LLMConfig(model='gpt-4o', api_key='test_key')- metrics = Metrics()- llm = LLM(config, metrics=metrics, service_id='test-service')- assert llm.metrics is metrics- assert (- llm.metrics.model_name == 'default'- ) # because we didn't specify model_name in Metrics init---@patch('openhands.llm.llm.litellm_completion')-@patch('time.time')-def test_response_latency_tracking(mock_time, mock_litellm_completion):- # Mock time.time() to return controlled values- mock_time.side_effect = [1000.0, 1002.5] # Start time, end time (2.5s difference)-- # Mock the completion response with a specific ID- mock_response = {- 'id': 'test-response-123',- 'choices': [{'message': {'content': 'Test response'}}],- }- mock_litellm_completion.return_value = mock_response-- # Create LLM instance and make a completion call- config = LLMConfig(model='gpt-4o', api_key='test_key')- llm = LLM(config, service_id='test-service')- response = llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}])-- # Verify the response latency was tracked correctly- assert len(llm.metrics.response_latencies) == 1- latency_record = llm.metrics.response_latencies[0]- assert latency_record.model == 'gpt-4o'- assert (- latency_record.latency == 2.5- ) # Should be the difference between our mocked times- assert latency_record.response_id == 'test-response-123'-- # Verify the completion response was returned correctly- assert response['id'] == 'test-response-123'- assert response['choices'][0]['message']['content'] == 'Test response'-- # To make sure the metrics fail gracefully, set the start/end time to go backwards.- mock_time.side_effect = [1000.0, 999.0]- llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}])-- # There should now be 2 latencies, the last of which has the value clipped to 0- assert len(llm.metrics.response_latencies) == 2- latency_record = llm.metrics.response_latencies[-1]- assert latency_record.latency == 0.0 # Should be lifted to 0 instead of being -1!---@patch('openhands.llm.llm.litellm.get_model_info')-def test_llm_init_with_openrouter_model(mock_get_model_info, default_config):- default_config.model = 'openrouter/gpt-4o-mini'- mock_get_model_info.return_value = {- 'max_input_tokens': 7000,- 'max_output_tokens': 1500,- }- llm = LLM(default_config, service_id='test-service')- llm.init_model_info()- assert llm.config.max_input_tokens == 7000- assert llm.config.max_output_tokens == 1500- mock_get_model_info.assert_called_once_with('openrouter/gpt-4o-mini')---@patch('openhands.llm.llm.litellm_completion')-def test_stop_parameter_handling(mock_litellm_completion, default_config):- """Test that stop parameter is only added for supported models."""- from litellm.types.utils import ModelResponse-- mock_response = ModelResponse(- id='test-id',- choices=[{'message': {'content': 'Test response'}}],- model='test-model',- )- mock_litellm_completion.return_value = mock_response-- # Test with a model that supports stop parameter- default_config.model = (- 'custom-model' # Use a model not in FUNCTION_CALLING_SUPPORTED_MODELS- )- llm = LLM(default_config, service_id='test-service')- llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- tools=[- {'type': 'function', 'function': {'name': 'test', 'description': 'test'}}- ],- )- # Verify stop parameter was included- assert 'stop' in mock_litellm_completion.call_args[1]-- # Test with Grok-4 model that doesn't support stop parameter- default_config.model = 'xai/grok-4-0709'- llm = LLM(default_config, service_id='test-service')- llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- tools=[- {'type': 'function', 'function': {'name': 'test', 'description': 'test'}}- ],- )- # Verify stop parameter was not included- assert 'stop' not in mock_litellm_completion.call_args[1]---# Tests involving completion and retries---@patch('openhands.llm.llm.litellm_completion')-def test_completion_with_mocked_logger(- mock_litellm_completion, default_config, mock_logger-):- mock_litellm_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}]- }-- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- assert response['choices'][0]['message']['content'] == 'Test response'- assert mock_litellm_completion.call_count == 1-- mock_logger.debug.assert_called()--- 'exception_class,extra_args,expected_retries',- [- (RateLimitError, {'llm_provider': 'test_provider', 'model': 'test_model'}, 2),- ],-)-@patch('openhands.llm.llm.litellm_completion')-def test_completion_retries(- mock_litellm_completion,- default_config,- exception_class,- extra_args,- expected_retries,-):- mock_litellm_completion.side_effect = [- exception_class('Test error message', **extra_args),- {'choices': [{'message': {'content': 'Retry successful'}}]},- ]-- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- assert response['choices'][0]['message']['content'] == 'Retry successful'- assert mock_litellm_completion.call_count == expected_retries---@patch('openhands.llm.llm.litellm_completion')-def test_completion_rate_limit_wait_time(mock_litellm_completion, default_config):- with patch('time.sleep') as mock_sleep:- mock_litellm_completion.side_effect = [- RateLimitError(- 'Rate limit exceeded', llm_provider='test_provider', model='test_model'- ),- {'choices': [{'message': {'content': 'Retry successful'}}]},- ]-- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- assert response['choices'][0]['message']['content'] == 'Retry successful'- assert mock_litellm_completion.call_count == 2-- mock_sleep.assert_called_once()- wait_time = mock_sleep.call_args[0][0]- assert (- default_config.retry_min_wait <= wait_time <= default_config.retry_max_wait- ), (- f'Expected wait time between {default_config.retry_min_wait} and {default_config.retry_max_wait} seconds, but got {wait_time}'- )---@patch('openhands.llm.llm.litellm_completion')-def test_completion_operation_cancelled(mock_litellm_completion, default_config):- mock_litellm_completion.side_effect = OperationCancelled('Operation cancelled')-- llm = LLM(config=default_config, service_id='test-service')- with pytest.raises(OperationCancelled):- llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- assert mock_litellm_completion.call_count == 1---@patch('openhands.llm.llm.litellm_completion')-def test_completion_keyboard_interrupt(mock_litellm_completion, default_config):- def side_effect(*args, **kwargs):- raise KeyboardInterrupt('Simulated KeyboardInterrupt')-- mock_litellm_completion.side_effect = side_effect-- llm = LLM(config=default_config, service_id='test-service')- with pytest.raises(OperationCancelled):- try:- llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )- except KeyboardInterrupt:- raise OperationCancelled('Operation cancelled due to KeyboardInterrupt')-- assert mock_litellm_completion.call_count == 1---@patch('openhands.llm.llm.litellm_completion')-def test_completion_keyboard_interrupt_handler(mock_litellm_completion, default_config):- global _should_exit-- def side_effect(*args, **kwargs):- global _should_exit- _should_exit = True- return {'choices': [{'message': {'content': 'Simulated interrupt response'}}]}-- mock_litellm_completion.side_effect = side_effect-- llm = LLM(config=default_config, service_id='test-service')- result = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- )-- assert mock_litellm_completion.call_count == 1- assert result['choices'][0]['message']['content'] == 'Simulated interrupt response'- assert _should_exit-- _should_exit = False---@patch('openhands.llm.llm.litellm_completion')-def test_completion_retry_with_llm_no_response_error_zero_temp(- mock_litellm_completion, default_config-):- """Test that the retry decorator properly handles LLMNoResponseError by:- 1. First call to llm_completion uses temperature=0 and throws LLMNoResponseError- 2. Second call should have temperature=0.2 and return a successful response- """-- # Define a side effect function that checks the temperature parameter- # and returns different responses based on it- def side_effect(*args, **kwargs):- temperature = kwargs.get('temperature', 0)-- # First call with temperature=0 should raise LLMNoResponseError- if temperature == 0:- raise LLMNoResponseError('LLM did not return a response')-- else:- return {- 'choices': [- {'message': {'content': f'Response with temperature={temperature}'}}- ]- }-- mock_litellm_completion.side_effect = side_effect-- # Create LLM instance and make a completion call- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- temperature=0, # Initial temperature is 0- )-- # Verify the response after retry- assert (- response['choices'][0]['message']['content'] == 'Response with temperature=1.0'- )-- # Verify that litellm_completion was called twice- assert mock_litellm_completion.call_count == 2-- # Check the temperature in the first call (should be 0)- first_call_kwargs = mock_litellm_completion.call_args_list[0][1]- assert first_call_kwargs.get('temperature') == 0-- # Check the temperature in the second call (should be 1.0)- second_call_kwargs = mock_litellm_completion.call_args_list[1][1]- assert second_call_kwargs.get('temperature') == 1.0---@patch('openhands.llm.llm.litellm_completion')-def test_completion_retry_with_llm_no_response_error_nonzero_temp(- mock_litellm_completion, default_config-):- """Test that the retry decorator works for LLMNoResponseError when initial temperature is non-zero,- and keeps the original temperature on retry.-- This test verifies that when LLMNoResponseError is raised with a non-zero temperature:- 1. The retry mechanism is triggered- 2. The temperature remains unchanged (not set to 0.2)- 3. After all retries are exhausted, the error is raised- """- mock_litellm_completion.side_effect = LLMNoResponseError(- 'LLM did not return a response'- )-- llm = LLM(config=default_config, service_id='test-service')- with pytest.raises(LLMNoResponseError):- llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- temperature=0.7, # Non-zero temperature- )-- # Verify that litellm_completion was called the expected number of times- assert mock_litellm_completion.call_count == default_config.num_retries-- # Check that all calls used the original temperature- for call in mock_litellm_completion.call_args_list:- assert call[1].get('temperature') == 0.7---@patch('openhands.llm.llm.litellm.get_model_info')-@patch('openhands.llm.llm.httpx.get')-def test_gemini_25_pro_function_calling(mock_httpx_get, mock_get_model_info):- """Test that Gemini 2.5 Pro models have function calling enabled by default.- This includes testing various model name formats with different prefixes.- """- # Mock the model info response- mock_get_model_info.return_value = {- 'max_input_tokens': 8000,- 'max_output_tokens': 2000,- }-- # Mock the httpx response for litellm proxy- mock_response = MagicMock()- mock_response.json.return_value = {- 'data': [- {- 'model_name': 'gemini-2.5-pro-preview-03-25',- 'model_info': {- 'max_input_tokens': 8000,- 'max_output_tokens': 2000,- },- }- ]- }- mock_httpx_get.return_value = mock_response-- # Test cases with model names and expected function calling support- test_cases = [- # Base model names- ('gemini-2.5-pro-preview-03-25', True),- ('gemini-2.5-pro-exp-03-25', True),- # With gemini/ prefix- ('gemini/gemini-2.5-pro-preview-03-25', True),- ('gemini/gemini-2.5-pro-exp-03-25', True),- # With litellm_proxy/ prefix- ('litellm_proxy/gemini-2.5-pro-preview-03-25', True),- ('litellm_proxy/gemini-2.5-pro-exp-03-25', True),- # With openrouter/gemini/ prefix- ('openrouter/gemini/gemini-2.5-pro-preview-03-25', True),- ('openrouter/gemini/gemini-2.5-pro-exp-03-25', True),- # With litellm_proxy/gemini/ prefix- ('litellm_proxy/gemini/gemini-2.5-pro-preview-03-25', True),- ('litellm_proxy/gemini/gemini-2.5-pro-exp-03-25', True),- # Control case - a model that shouldn't have function calling- ('gemini-1.0-pro', False),- ]-- for model_name, expected_support in test_cases:- config = LLMConfig(model=model_name, api_key='test_key')- llm = LLM(config, service_id='test-service')-- assert llm.is_function_calling_active() == expected_support, (- f'Expected function calling support to be {expected_support} for model {model_name}'- )---@patch('openhands.llm.llm.litellm_completion')-def test_completion_retry_with_llm_no_response_error_nonzero_temp_successful_retry(- mock_litellm_completion, default_config-):- """Test that the retry decorator works for LLMNoResponseError with non-zero temperature- and successfully retries while preserving the original temperature.-- This test verifies that:- 1. First call to llm_completion with temperature=0.7 throws LLMNoResponseError- 2. Second call with the same temperature=0.7 returns a successful response- """-- # Define a side effect function that raises LLMNoResponseError on first call- # and returns a successful response on second call- def side_effect(*args, **kwargs):- temperature = kwargs.get('temperature', 0)-- if mock_litellm_completion.call_count == 1:- # First call should raise LLMNoResponseError- raise LLMNoResponseError('LLM did not return a response')- else:- # Second call should return a successful response- return {- 'choices': [- {- 'message': {- 'content': f'Successful response with temperature={temperature}'- }- }- ]- }-- mock_litellm_completion.side_effect = side_effect-- # Create LLM instance and make a completion call with non-zero temperature- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- temperature=0.7, # Non-zero temperature- )-- # Verify the response after retry- assert (- response['choices'][0]['message']['content']- == 'Successful response with temperature=0.7'- )-- # Verify that litellm_completion was called twice- assert mock_litellm_completion.call_count == 2-- # Check the temperature in the first call (should be 0.7)- first_call_kwargs = mock_litellm_completion.call_args_list[0][1]- assert first_call_kwargs.get('temperature') == 0.7-- # Check the temperature in the second call (should still be 0.7)- second_call_kwargs = mock_litellm_completion.call_args_list[1][1]- assert second_call_kwargs.get('temperature') == 0.7---@patch('openhands.llm.llm.litellm_completion')-def test_completion_retry_with_llm_no_response_error_successful_retry(- mock_litellm_completion, default_config-):- """Test that the retry decorator works for LLMNoResponseError with zero temperature- and successfully retries with temperature=0.2.-- This test verifies that:- 1. First call to llm_completion with temperature=0 throws LLMNoResponseError- 2. Second call with temperature=0.2 returns a successful response- """-- # Define a side effect function that raises LLMNoResponseError on first call- # and returns a successful response on second call- def side_effect(*args, **kwargs):- temperature = kwargs.get('temperature', 0)-- if mock_litellm_completion.call_count == 1:- # First call should raise LLMNoResponseError- raise LLMNoResponseError('LLM did not return a response')- else:- # Second call should return a successful response- return {- 'choices': [- {- 'message': {- 'content': f'Successful response with temperature={temperature}'- }- }- ]- }-- mock_litellm_completion.side_effect = side_effect-- # Create LLM instance and make a completion call with explicit temperature=0- llm = LLM(config=default_config, service_id='test-service')- response = llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- temperature=0, # Explicitly set temperature to 0- )-- # Verify the response after retry- assert (- response['choices'][0]['message']['content']- == 'Successful response with temperature=1.0'- )-- # Verify that litellm_completion was called twice- assert mock_litellm_completion.call_count == 2-- # Check the temperature in the first call (should be 0)- first_call_kwargs = mock_litellm_completion.call_args_list[0][1]- assert first_call_kwargs.get('temperature') == 0-- # Check the temperature in the second call (should be 1.0)- second_call_kwargs = mock_litellm_completion.call_args_list[1][1]- assert second_call_kwargs.get('temperature') == 1.0---@patch('openhands.llm.llm.litellm_completion')-def test_completion_with_litellm_mock(mock_litellm_completion, default_config):- mock_response = {- 'choices': [{'message': {'content': 'This is a mocked response.'}}]- }- mock_litellm_completion.return_value = mock_response-- test_llm = LLM(config=default_config, service_id='test-service')- response = test_llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- drop_params=True,- )-- # Assertions- assert response['choices'][0]['message']['content'] == 'This is a mocked response.'- mock_litellm_completion.assert_called_once()-- # Check if the correct arguments were passed to litellm_completion- call_args = mock_litellm_completion.call_args[1] # Get keyword arguments- assert call_args['model'] == default_config.model- assert call_args['messages'] == [{'role': 'user', 'content': 'Hello!'}]- assert not call_args['stream']---@patch('openhands.llm.llm.litellm_completion')-def test_llm_gemini_thinking_parameter(mock_litellm_completion, default_config):- """Test that the 'thinking' parameter is correctly passed to litellm_completion- when a Gemini model is used with 'low' reasoning_effort.- """- # Configure for Gemini model with low reasoning effort- gemini_config = copy.deepcopy(default_config)- gemini_config.model = 'gemini-2.5-pro'- gemini_config.reasoning_effort = 'low'-- # Mock the response from litellm- mock_litellm_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}]- }-- # Initialize LLM and call completion- llm = LLM(config=gemini_config, service_id='test-service')- llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}])-- # Verify that litellm_completion was called with the 'thinking' parameter- mock_litellm_completion.assert_called_once()- call_args, call_kwargs = mock_litellm_completion.call_args- assert 'thinking' in call_kwargs- assert call_kwargs['thinking'] == {'budget_tokens': 128}- assert 'reasoning_effort' not in call_kwargs-- # Ensure the first positional argument (model) was ignored- assert (- len(call_args) == 0- ) # No positional args should be passed to litellm_completion here---@patch('openhands.llm.llm.litellm.token_counter')-def test_get_token_count_with_dict_messages(mock_token_counter, default_config):- mock_token_counter.return_value = 42- llm = LLM(default_config, service_id='test-service')- messages = [{'role': 'user', 'content': 'Hello!'}]-- token_count = llm.get_token_count(messages)-- assert token_count == 42- mock_token_counter.assert_called_once_with(- model=default_config.model, messages=messages, custom_tokenizer=None- )---@patch('openhands.llm.llm.litellm.token_counter')-def test_get_token_count_with_message_objects(- mock_token_counter, default_config, mock_logger-):- llm = LLM(default_config, service_id='test-service')-- # Create a Message object and its equivalent dict- message_obj = Message(role='user', content=[TextContent(text='Hello!')])- message_dict = {'role': 'user', 'content': 'Hello!'}-- # Mock token counter to return different values for each call- mock_token_counter.side_effect = [42, 42] # Same value for both cases-- # Get token counts for both formats- token_count_obj = llm.get_token_count([message_obj])- token_count_dict = llm.get_token_count([message_dict])-- # Verify both formats get the same token count- assert token_count_obj == token_count_dict- assert mock_token_counter.call_count == 2---@patch('openhands.llm.llm.litellm.token_counter')-@patch('openhands.llm.llm.create_pretrained_tokenizer')-def test_get_token_count_with_custom_tokenizer(- mock_create_tokenizer, mock_token_counter, default_config-):- mock_tokenizer = MagicMock()- mock_create_tokenizer.return_value = mock_tokenizer- mock_token_counter.return_value = 42-- config = copy.deepcopy(default_config)- config.custom_tokenizer = 'custom/tokenizer'- llm = LLM(config, service_id='test-service')- messages = [{'role': 'user', 'content': 'Hello!'}]-- token_count = llm.get_token_count(messages)-- assert token_count == 42- mock_create_tokenizer.assert_called_once_with('custom/tokenizer')- mock_token_counter.assert_called_once_with(- model=config.model, messages=messages, custom_tokenizer=mock_tokenizer- )---@patch('openhands.llm.llm.litellm.token_counter')-def test_get_token_count_error_handling(- mock_token_counter, default_config, mock_logger-):- mock_token_counter.side_effect = Exception('Token counting failed')- llm = LLM(default_config, service_id='test-service')- messages = [{'role': 'user', 'content': 'Hello!'}]-- token_count = llm.get_token_count(messages)-- assert token_count == 0- mock_token_counter.assert_called_once()- mock_logger.error.assert_called_once_with(- 'Error getting token count for\n model gpt-4o\nToken counting failed'- )---@patch('openhands.llm.llm.litellm_completion')-def test_llm_token_usage(mock_litellm_completion, default_config):- # This mock response includes usage details with prompt_tokens,- # completion_tokens, prompt_tokens_details.cached_tokens, and model_extra.cache_creation_input_tokens- mock_response_1 = {- 'id': 'test-response-usage',- 'choices': [{'message': {'content': 'Usage test response'}}],- 'usage': {- 'prompt_tokens': 12,- 'completion_tokens': 3,- 'prompt_tokens_details': PromptTokensDetails(cached_tokens=2),- 'model_extra': {'cache_creation_input_tokens': 5},- },- }-- # Create a second usage scenario to test accumulation and a different response_id- mock_response_2 = {- 'id': 'test-response-usage-2',- 'choices': [{'message': {'content': 'Second usage test response'}}],- 'usage': {- 'prompt_tokens': 7,- 'completion_tokens': 2,- 'prompt_tokens_details': PromptTokensDetails(cached_tokens=1),- 'model_extra': {'cache_creation_input_tokens': 3},- },- }-- # We'll make mock_litellm_completion return these responses in sequence- mock_litellm_completion.side_effect = [mock_response_1, mock_response_2]-- llm = LLM(config=default_config, service_id='test-service')-- # First call- llm.completion(messages=[{'role': 'user', 'content': 'Hello usage!'}])-- # Verify we have exactly one usage record after first call- token_usage_list = llm.metrics.get()['token_usages']- assert len(token_usage_list) == 1- usage_entry_1 = token_usage_list[0]- assert usage_entry_1['prompt_tokens'] == 12- assert usage_entry_1['completion_tokens'] == 3- assert usage_entry_1['cache_read_tokens'] == 2- assert usage_entry_1['cache_write_tokens'] == 5- assert usage_entry_1['response_id'] == 'test-response-usage'-- # Second call- llm.completion(messages=[{'role': 'user', 'content': 'Hello again!'}])-- # Now we expect two usage records total- token_usage_list = llm.metrics.get()['token_usages']- assert len(token_usage_list) == 2- usage_entry_2 = token_usage_list[-1]- assert usage_entry_2['prompt_tokens'] == 7- assert usage_entry_2['completion_tokens'] == 2- assert usage_entry_2['cache_read_tokens'] == 1- assert usage_entry_2['cache_write_tokens'] == 3- assert usage_entry_2['response_id'] == 'test-response-usage-2'---@patch('openhands.llm.llm.litellm_completion')-def test_accumulated_token_usage(mock_litellm_completion, default_config):- """Test that token usage is properly accumulated across multiple LLM calls."""- # Mock responses with token usage information- mock_response_1 = {- 'id': 'test-response-1',- 'choices': [{'message': {'content': 'First response'}}],- 'usage': {- 'prompt_tokens': 10,- 'completion_tokens': 5,- 'prompt_tokens_details': PromptTokensDetails(cached_tokens=3),- 'model_extra': {'cache_creation_input_tokens': 4},- },- }-- mock_response_2 = {- 'id': 'test-response-2',- 'choices': [{'message': {'content': 'Second response'}}],- 'usage': {- 'prompt_tokens': 8,- 'completion_tokens': 6,- 'prompt_tokens_details': PromptTokensDetails(cached_tokens=2),- 'model_extra': {'cache_creation_input_tokens': 3},- },- }-- # Set up the mock to return these responses in sequence- mock_litellm_completion.side_effect = [mock_response_1, mock_response_2]-- # Create LLM instance- llm = LLM(config=default_config, service_id='test-service')-- # First call- llm.completion(messages=[{'role': 'user', 'content': 'First message'}])-- # Check accumulated token usage after first call- metrics_data = llm.metrics.get()- accumulated_usage = metrics_data['accumulated_token_usage']-- assert accumulated_usage['prompt_tokens'] == 10- assert accumulated_usage['completion_tokens'] == 5- assert accumulated_usage['cache_read_tokens'] == 3- assert accumulated_usage['cache_write_tokens'] == 4-- # Second call- llm.completion(messages=[{'role': 'user', 'content': 'Second message'}])-- # Check accumulated token usage after second call- metrics_data = llm.metrics.get()- accumulated_usage = metrics_data['accumulated_token_usage']-- # Values should be the sum of both calls- assert accumulated_usage['prompt_tokens'] == 18 # 10 + 8- assert accumulated_usage['completion_tokens'] == 11 # 5 + 6- assert accumulated_usage['cache_read_tokens'] == 5 # 3 + 2- assert accumulated_usage['cache_write_tokens'] == 7 # 4 + 3-- # Verify individual token usage records are still maintained- token_usages = metrics_data['token_usages']- assert len(token_usages) == 2-- # First record- assert token_usages[0]['prompt_tokens'] == 10- assert token_usages[0]['completion_tokens'] == 5- assert token_usages[0]['cache_read_tokens'] == 3- assert token_usages[0]['cache_write_tokens'] == 4- assert token_usages[0]['response_id'] == 'test-response-1'-- # Second record- assert token_usages[1]['prompt_tokens'] == 8- assert token_usages[1]['completion_tokens'] == 6- assert token_usages[1]['cache_read_tokens'] == 2- assert token_usages[1]['cache_write_tokens'] == 3- assert token_usages[1]['response_id'] == 'test-response-2'---@patch('openhands.llm.llm.litellm_completion')-def test_completion_with_log_completions(mock_litellm_completion, default_config):- with tempfile.TemporaryDirectory() as temp_dir:- default_config.log_completions = True- default_config.log_completions_folder = temp_dir- mock_response = {- 'choices': [{'message': {'content': 'This is a mocked response.'}}]- }- mock_litellm_completion.return_value = mock_response-- test_llm = LLM(config=default_config, service_id='test-service')- response = test_llm.completion(- messages=[{'role': 'user', 'content': 'Hello!'}],- stream=False,- drop_params=True,- )- assert (- response['choices'][0]['message']['content'] == 'This is a mocked response.'- )- files = list(Path(temp_dir).iterdir())- # Expect a log to be generated- assert len(files) == 1---@patch('httpx.get')-def test_llm_base_url_auto_protocol_patch(mock_get):- """Test that LLM base_url without protocol is automatically fixed with 'http://'."""- config = LLMConfig(- model='litellm_proxy/test-model',- api_key='fake-key',- base_url=' api.example.com ',- )-- mock_get.return_value.status_code = 200- mock_get.return_value.json.return_value = {'model': 'fake'}-- llm = LLM(config=config, service_id='test-service')- llm.init_model_info()-- called_url = mock_get.call_args[0][0]- assert called_url.startswith('http://') or called_url.startswith('https://')---# Tests for max_output_tokens configuration and usage---def test_unknown_model_token_limits():- """Test that models without known token limits get None for both max_output_tokens and max_input_tokens."""- # Create LLM instance with a non-existent model to avoid litellm having model info for it- config = LLMConfig(model='non-existent-model', api_key='test_key')- llm = LLM(config, service_id='test-service')-- # Verify max_output_tokens and max_input_tokens are initialized to None (default value)- assert llm.config.max_output_tokens is None- assert llm.config.max_input_tokens is None---def test_max_tokens_from_model_info():- """Test that max_output_tokens and max_input_tokens are correctly initialized from model info."""- # Create LLM instance with GPT-4 model which has known token limits- config = LLMConfig(model='gpt-4', api_key='test_key')- llm = LLM(config, service_id='test-service')-- # GPT-4 has specific token limits- # These are the expected values from litellm- assert llm.config.max_output_tokens == 4096- assert llm.config.max_input_tokens == 8192---def test_claude_3_7_sonnet_max_output_tokens():- """Test that Claude 3.7 Sonnet models get the special 64000 max_output_tokens value and default max_input_tokens."""- # Create LLM instance with Claude 3.7 Sonnet model- config = LLMConfig(model='claude-3-7-sonnet', api_key='test_key')- llm = LLM(config, service_id='test-service')-- # Verify max_output_tokens is set to 64000 for Claude 3.7 Sonnet- assert llm.config.max_output_tokens == 64000- # Verify max_input_tokens is set to None (default value)- assert llm.config.max_input_tokens is None---@patch('openhands.llm.llm.litellm.get_model_info')-def test_claude_sonnet_4_max_output_tokens(mock_get_model_info):- """Test that Claude Sonnet 4 models get the correct max_output_tokens and max_input_tokens values."""- mock_get_model_info.return_value = {- 'max_input_tokens': 100000,- 'max_output_tokens': 100000,- }- # Create LLM instance with a Claude Sonnet 4 model- config = LLMConfig(model='claude-sonnet-4-20250514', api_key='test_key')- llm = LLM(config, service_id='test-service')- llm.init_model_info()-- assert llm.config.max_output_tokens == 64000, 'output max should be decreased'- assert llm.config.max_input_tokens == 100000, (- 'input max should be the litellm value'- )---def test_sambanova_deepseek_model_max_output_tokens():- """Test that SambaNova DeepSeek-V3-0324 model gets the correct max_output_tokens value."""- # Create LLM instance with SambaNova DeepSeek model- config = LLMConfig(model='sambanova/DeepSeek-V3-0324', api_key='test_key')- llm = LLM(config, service_id='test-service')-- # SambaNova DeepSeek model has specific token limits- # This is the expected value from litellm- assert llm.config.max_output_tokens == 32768---def test_max_output_tokens_override_in_config():- """Test that max_output_tokens can be overridden in the config."""- # Create LLM instance with minimal config and overridden max_output_tokens- config = LLMConfig(- model='claude-sonnet-4-20250514', api_key='test_key', max_output_tokens=2048- )- llm = LLM(config, service_id='test-service')-- # Verify the config has the overridden max_output_tokens value- assert llm.config.max_output_tokens == 2048---def test_azure_model_default_max_tokens():- """Test that Azure models have the default max_output_tokens value."""- # Create minimal config for Azure model (without specifying max_output_tokens)- azure_config = LLMConfig(- model='azure/non-existent-model', # Use a non-existent model to avoid litellm having model info for it- api_key='test_key',- base_url='https://test.openai.azure.com/',- api_version='2024-12-01-preview',- )-- # Create LLM instance with Azure model- llm = LLM(azure_config, service_id='test-service')-- # Verify the config has the default max_output_tokens value- assert llm.config.max_output_tokens is None # Default value---# Gemini Performance Optimization Tests---def test_gemini_model_keeps_none_reasoning_effort():- """Test that Gemini models keep reasoning_effort=None for optimization."""- config = LLMConfig(model='gemini-2.5-pro', api_key='test_key')- # reasoning_effort should remain None for Gemini models- assert config.reasoning_effort is None---def test_non_gemini_model_explicit_reasoning_effort():- """Test that non-Gemini models get reasoning_effort ONLY if explicitly set."""- config = LLMConfig(model='gpt-4o', api_key='test_key', reasoning_effort='high')- assert config.reasoning_effort == 'high'---def test_non_gemini_model_default_reasoning_effort_none():- """Test that non-Gemini models do NOT get reasoning_effort by default after PR."""- config = LLMConfig(model='gpt-4o', api_key='test_key')- # Should be None by default after your change- assert config.reasoning_effort is None---def test_explicit_reasoning_effort_preserved():- """Test that explicitly set reasoning_effort is preserved."""- config = LLMConfig(- model='gemini-2.5-pro', api_key='test_key', reasoning_effort='medium'- )- # Explicitly set reasoning_effort should be preserved- assert config.reasoning_effort == 'medium'---@patch('openhands.llm.llm.litellm_completion')-def test_gemini_none_reasoning_effort_uses_thinking_budget(mock_completion):- """Test that Gemini with reasoning_effort=None uses thinking budget."""- config = LLMConfig(- model='gemini-2.5-pro', api_key='test_key', reasoning_effort=None- )-- # Mock the completion response- mock_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}],- 'usage': {'prompt_tokens': 10, 'completion_tokens': 5},- }-- llm = LLM(config, service_id='test-service')- sample_messages = [{'role': 'user', 'content': 'Hello, how are you?'}]- llm.completion(messages=sample_messages)-- # Verify that thinking budget was set and reasoning_effort was None- call_kwargs = mock_completion.call_args[1]- assert 'thinking' in call_kwargs- assert call_kwargs['thinking'] == {'budget_tokens': 128}- assert call_kwargs.get('reasoning_effort') is None---@patch('openhands.llm.llm.litellm_completion')-def test_gemini_low_reasoning_effort_uses_thinking_budget(mock_completion):- """Test that Gemini with reasoning_effort='low' uses thinking budget."""- config = LLMConfig(- model='gemini-2.5-pro', api_key='test_key', reasoning_effort='low'- )-- # Mock the completion response- mock_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}],- 'usage': {'prompt_tokens': 10, 'completion_tokens': 5},- }-- llm = LLM(config, service_id='test-service')- sample_messages = [{'role': 'user', 'content': 'Hello, how are you?'}]- llm.completion(messages=sample_messages)-- # Verify that thinking budget was set and reasoning_effort was None- call_kwargs = mock_completion.call_args[1]- assert 'thinking' in call_kwargs- assert call_kwargs['thinking'] == {'budget_tokens': 128}- assert call_kwargs.get('reasoning_effort') is None---@patch('openhands.llm.llm.litellm_completion')-def test_gemini_medium_reasoning_effort_passes_through(mock_completion):- """Test that Gemini with reasoning_effort='medium' passes through to litellm."""- config = LLMConfig(- model='gemini-2.5-pro', api_key='test_key', reasoning_effort='medium'- )-- # Mock the completion response- mock_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}],- 'usage': {'prompt_tokens': 10, 'completion_tokens': 5},- }-- llm = LLM(config, service_id='test-service')- sample_messages = [{'role': 'user', 'content': 'Hello, how are you?'}]- llm.completion(messages=sample_messages)-- # Verify that reasoning_effort was passed through and thinking budget was not set- call_kwargs = mock_completion.call_args[1]- assert 'thinking' not in call_kwargs- assert call_kwargs.get('reasoning_effort') == 'medium'---@patch('openhands.llm.llm.litellm_completion')-def test_opus_41_keeps_temperature_top_p(mock_completion):- mock_completion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(- model='anthropic/claude-opus-4-1-20250805',- api_key='k',- temperature=0.7,- top_p=0.9,- )- llm = LLM(config, service_id='svc')- llm.completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_completion.call_args[1]- assert call_kwargs.get('temperature') == 0.7- # Anthropic rejects both temperature and top_p together on Opus; we keep temperature and drop top_p- assert 'top_p' not in call_kwargs---@patch('openhands.llm.llm.litellm_completion')-def test_opus_45_keeps_temperature_drops_top_p(mock_completion):- mock_completion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(- model='anthropic/claude-opus-4-5-20251101',- api_key='k',- temperature=0.7,- top_p=0.9,- )- llm = LLM(config, service_id='svc')- llm.completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_completion.call_args[1]- assert call_kwargs.get('temperature') == 0.7- # Anthropic rejects both temperature and top_p together on Opus 4.5; we keep temperature and drop top_p- assert 'top_p' not in call_kwargs---@patch('openhands.llm.llm.litellm_completion')-def test_opus_46_keeps_temperature_drops_top_p(mock_completion):- mock_completion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(- model='anthropic/claude-opus-4-6',- api_key='k',- temperature=0.7,- top_p=0.9,- )- llm = LLM(config, service_id='svc')- llm.completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_completion.call_args[1]- assert call_kwargs.get('temperature') == 0.7- # Anthropic rejects both temperature and top_p together on Opus 4.6; we keep temperature and drop top_p- assert 'top_p' not in call_kwargs---@patch('openhands.llm.llm.litellm_completion')-def test_sonnet_4_keeps_temperature_drops_top_p(mock_completion):- mock_completion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(- model='anthropic/claude-sonnet-4-20250514',- api_key='k',- temperature=0.7,- top_p=0.9,- )- llm = LLM(config, service_id='svc')- llm.completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_completion.call_args[1]- assert call_kwargs.get('temperature') == 0.7- # Anthropic rejects both temperature and top_p together on Sonnet 4; we keep temperature and drop top_p- assert 'top_p' not in call_kwargs---@patch('openhands.llm.llm.litellm_completion')-def test_opus_4_keeps_temperature_top_p(mock_completion):- mock_completion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(- model='anthropic/claude-opus-4-20250514',- api_key='k',- temperature=0.7,- top_p=0.9,- )- llm = LLM(config, service_id='svc')- llm.completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_completion.call_args[1]- assert call_kwargs.get('temperature') == 0.7- assert call_kwargs.get('top_p') == 0.9---@patch('openhands.llm.llm.litellm_completion')-def test_opus_41_disables_thinking(mock_completion):- mock_completion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(- model='anthropic/claude-opus-4-1-20250805',- api_key='k',- )- llm = LLM(config, service_id='svc')- llm.completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_completion.call_args[1]- assert call_kwargs.get('thinking') == {'type': 'disabled'}---@patch('openhands.llm.llm.litellm.get_model_info')-def test_is_caching_prompt_active_anthropic_prefixed(mock_get_model_info):- # Avoid external calls, but behavior shouldn't depend on model info- mock_get_model_info.side_effect = Exception('skip')- config = LLMConfig(- model='anthropic/claude-3-7-sonnet', api_key='k', caching_prompt=True- )- llm = LLM(config, service_id='svc')- assert llm.is_caching_prompt_active() is True---@patch('openhands.llm.llm.httpx.get')-@patch('openhands.llm.llm.litellm.get_model_info')-def test_openhands_provider_rewrite_and_caching_prompt(- mock_get_model_info, mock_httpx_get-):- # Mock LiteLLM proxy /v1/model/info response- mock_httpx_get.return_value = type(- 'Resp',- (),- {- 'json': lambda self=None: {- 'data': [- {- 'model_name': 'claude-3.7-sonnet',- 'model_info': {- 'max_input_tokens': 200000,- 'max_output_tokens': 64000,- 'supports_vision': True,- },- }- ]- }- },- )()- mock_get_model_info.return_value = {- 'max_input_tokens': 200000,- 'max_output_tokens': 64000,- }-- config = LLMConfig(- model='openhands/claude-3.7-sonnet', api_key='k', caching_prompt=True- )- llm = LLM(config, service_id='svc')- # Model should be rewritten to litellm_proxy/...- assert llm.config.model.startswith('litellm_proxy/claude-3.7-sonnet')- # Caching prompt should be active for Claude- assert llm.is_caching_prompt_active() is True---@patch('openhands.llm.llm.litellm_completion')-def test_gemini_high_reasoning_effort_passes_through(mock_completion):- """Test that Gemini with reasoning_effort='high' passes through to litellm."""- config = LLMConfig(- model='gemini-2.5-pro', api_key='test_key', reasoning_effort='high'- )-- # Mock the completion response- mock_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}],- 'usage': {'prompt_tokens': 10, 'completion_tokens': 5},- }-- llm = LLM(config, service_id='test-service')- sample_messages = [{'role': 'user', 'content': 'Hello, how are you?'}]- llm.completion(messages=sample_messages)-- # Verify that reasoning_effort was passed through and thinking budget was not set- call_kwargs = mock_completion.call_args[1]- assert 'thinking' not in call_kwargs- assert call_kwargs.get('reasoning_effort') == 'high'---@patch('openhands.llm.llm.litellm_completion')-def test_non_gemini_uses_reasoning_effort(mock_completion):- """Test that non-Gemini models use reasoning_effort instead of thinking budget."""- config = LLMConfig(model='o1', api_key='test_key', reasoning_effort='high')-- # Mock the completion response- mock_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}],- 'usage': {'prompt_tokens': 10, 'completion_tokens': 5},- }-- llm = LLM(config, service_id='test-service')- sample_messages = [{'role': 'user', 'content': 'Hello, how are you?'}]- llm.completion(messages=sample_messages)---@patch('openhands.llm.async_llm.litellm_acompletion')-async def test_async_reasoning_effort_passthrough(mock_acompletion):- mock_acompletion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(- model='o3', api_key='k', temperature=0.7, top_p=0.9, reasoning_effort='low'- )- llm = AsyncLLM(config, service_id='svc')- await llm.async_completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_acompletion.call_args[1]- assert call_kwargs.get('reasoning_effort') == 'low'- # Async path does not pop temperature/top_p (parity with main)- assert call_kwargs.get('temperature') == 0.7- assert call_kwargs.get('top_p') == 0.9---@patch('openhands.llm.streaming_llm.AsyncLLM._call_acompletion')-async def test_streaming_reasoning_effort_passthrough(mock_call):- async def fake_stream(*args, **kwargs):- class Dummy:- async def __aiter__(self):- yield {'choices': [{'delta': {'content': 'x'}}]}-- return Dummy()-- mock_call.side_effect = fake_stream- config = LLMConfig(- model='o3', api_key='k', temperature=0.7, top_p=0.9, reasoning_effort='low'- )- sllm = StreamingLLM(config, service_id='svc')- async for _ in sllm.async_streaming_completion(- messages=[{'role': 'user', 'content': 'hi'}]- ):- break- call_kwargs = mock_call.call_args[1]- assert call_kwargs.get('reasoning_effort') == 'low'- assert call_kwargs.get('temperature') == 0.7- assert call_kwargs.get('top_p') == 0.9---@patch('openhands.llm.async_llm.litellm_acompletion')-async def test_async_streaming_no_thinking_for_gemini(mock_acompletion):- mock_acompletion.return_value = {- 'choices': [{'message': {'content': 'ok'}}],- }- config = LLMConfig(model='gemini-2.5-pro', api_key='k', reasoning_effort='low')- llm = AsyncLLM(config, service_id='svc')- await llm.async_completion(messages=[{'role': 'user', 'content': 'hi'}])- call_kwargs = mock_acompletion.call_args[1]- assert 'thinking' not in call_kwargs---@patch('openhands.llm.llm.litellm_completion')-def test_non_reasoning_model_no_optimization(mock_completion):- """Test that non-reasoning models don't get optimization parameters."""- config = LLMConfig(- model='gpt-3.5-turbo', # Not in REASONING_EFFORT_SUPPORTED_MODELS- api_key='test_key',- )-- # Mock the completion response- mock_completion.return_value = {- 'choices': [{'message': {'content': 'Test response'}}],- 'usage': {'prompt_tokens': 10, 'completion_tokens': 5},- }-- llm = LLM(config, service_id='test-service')- sample_messages = [{'role': 'user', 'content': 'Hello, how are you?'}]- llm.completion(messages=sample_messages)-- # Verify that neither thinking budget nor reasoning_effort were set- call_kwargs = mock_completion.call_args[1]- assert 'thinking' not in call_kwargs- assert 'reasoning_effort' not in call_kwargs---@patch('openhands.llm.llm.litellm_completion')-def test_gemini_performance_optimization_end_to_end(mock_completion):- """Test the complete Gemini performance optimization flow end-to-end."""- # Mock the completion response- mock_completion.return_value = {- 'choices': [{'message': {'content': 'Optimized response'}}],- 'usage': {'prompt_tokens': 50, 'completion_tokens': 25},- }-- # Create Gemini configuration- config = LLMConfig(model='gemini-2.5-pro', api_key='test_key')-- # Verify config has optimized defaults- assert config.reasoning_effort is None-- # Create LLM and make completion- llm = LLM(config, service_id='test-service')- messages = [{'role': 'user', 'content': 'Solve this complex problem'}]-- response = llm.completion(messages=messages)-- # Verify response was generated- assert response['choices'][0]['message']['content'] == 'Optimized response'-- # Verify optimization parameters were applied- call_kwargs = mock_completion.call_args[1]- assert 'thinking' in call_kwargs- assert call_kwargs['thinking'] == {'budget_tokens': 128}- assert call_kwargs.get('reasoning_effort') is None-- # Verify temperature and top_p were removed for reasoning models- assert 'temperature' not in call_kwargs- assert 'top_p' not in call_kwargsdiff --git a/tests/unit/llm/test_llm_fncall_converter.py b/tests/unit/llm/test_llm_fncall_converter.pydeleted file mode 100644index b4270f89b0..0000000000--- a/tests/unit/llm/test_llm_fncall_converter.py+++ /dev/null@@ -1,1134 +0,0 @@-"""Test for FunctionCallingConverter."""--import copy-import json--import pytest-from litellm import ChatCompletionToolParam--from openhands.llm.fn_call_converter import (- IN_CONTEXT_LEARNING_EXAMPLE_PREFIX,- IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX,- TOOL_EXAMPLES,- FunctionCallConversionError,- convert_fncall_messages_to_non_fncall_messages,- convert_from_multiple_tool_calls_to_single_tool_call_messages,- convert_non_fncall_messages_to_fncall_messages,- convert_tool_call_to_string,- convert_tools_to_description,- get_example_for_tools,-)--FNCALL_TOOLS: list[ChatCompletionToolParam] = [- {- 'type': 'function',- 'function': {- 'name': 'execute_bash',- 'description': 'Execute a bash command in the terminal.\n* Long running commands: For commands that may run indefinitely, it should be run in the background and the output should be redirected to a file, e.g. command = `python3 app.py > server.log 2>&1 &`.\n* Interactive: If a bash command returns exit code `-1`, this means the process is not yet finished. The assistant must then send a second call to terminal with an empty `command` (which will retrieve any additional logs), or it can send additional text (set `command` to the text) to STDIN of the running process, or it can send command=`ctrl+c` to interrupt the process.\n* Timeout: If a command execution result says "Command timed out. Sending SIGINT to the process", the assistant should retry running the command in the background.\n',- 'parameters': {- 'type': 'object',- 'properties': {- 'command': {- 'type': 'string',- 'description': 'The bash command to execute. Can be empty to view additional logs when previous exit code is `-1`. Can be `ctrl+c` to interrupt the currently running process.',- }- },- 'required': ['command'],- },- },- },- {- 'type': 'function',- 'function': {- 'name': 'finish',- 'description': 'Finish the interaction when the task is complete OR if the assistant cannot proceed further with the task.',- },- },- {- 'type': 'function',- 'function': {- 'name': 'str_replace_editor',- 'description': 'Custom editing tool for viewing, creating and editing files\n* State is persistent across command calls and discussions with the user\n* If `path` is a file, `view` displays the result of applying `cat -n`. If `path` is a directory, `view` lists non-hidden files and directories up to 2 levels deep\n* The `create` command cannot be used if the specified `path` already exists as a file\n* If a `command` generates a long output, it will be truncated and marked with `<response clipped>`\n* The `undo_edit` command will revert the last edit made to the file at `path`\n\nNotes for using the `str_replace` command:\n* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces!\n* If the `old_str` parameter is not unique in the file, the replacement will not be performed. Make sure to include enough context in `old_str` to make it unique\n* The `new_str` parameter should contain the edited lines that should replace the `old_str`\n',- 'parameters': {- 'type': 'object',- 'properties': {- 'command': {- 'description': 'The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.',- 'enum': [- 'view',- 'create',- 'str_replace',- 'insert',- 'undo_edit',- ],- 'type': 'string',- },- 'path': {- 'description': 'Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.',- 'type': 'string',- },- 'file_text': {- 'description': 'Required parameter of `create` command, with the content of the file to be created.',- 'type': 'string',- },- 'old_str': {- 'description': 'Required parameter of `str_replace` command containing the string in `path` to replace.',- 'type': 'string',- },- 'new_str': {- 'description': 'Optional parameter of `str_replace` command containing the new string (if not given, no string will be added). Required parameter of `insert` command containing the string to insert.',- 'type': 'string',- },- 'insert_line': {- 'description': 'Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.',- 'type': 'integer',- },- 'view_range': {- 'description': 'Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.',- 'items': {'type': 'integer'},- 'type': 'array',- },- },- 'required': ['command', 'path'],- },- },- },-]---def test_malformed_parameter_parsing_recovery():- """Ensure we can recover when models emit malformed parameter tags like <parameter=command=str_replace</parameter>.-- This simulates a tool call to str_replace_editor where the 'command' parameter is malformed.- """- from openhands.llm.fn_call_converter import (- convert_non_fncall_messages_to_fncall_messages,- )-- # Construct an assistant message with malformed parameter tag for 'command'- assistant_message = {- 'role': 'assistant',- 'content': (- '<function=str_replace_editor>\n'- '<parameter=command=str_replace</parameter>\n' # malformed form- '<parameter=path>/repo/app.py</parameter>\n'- '<parameter=old_str>foo</parameter>\n'- '<parameter=new_str>bar</parameter>\n'- '</function>'- ),- }-- messages = [- {'role': 'system', 'content': 'test'},- {'role': 'user', 'content': 'do edit'},- assistant_message,- ]-- converted = convert_non_fncall_messages_to_fncall_messages(messages, FNCALL_TOOLS)-- # The last message should be assistant with a parsed tool call- last = converted[-1]- assert last['role'] == 'assistant'- assert 'tool_calls' in last and len(last['tool_calls']) == 1- tool_call = last['tool_calls'][0]- assert tool_call['type'] == 'function'- assert tool_call['function']['name'] == 'str_replace_editor'-- # Arguments must be a valid JSON with command=str_replace and proper params- args = json.loads(tool_call['function']['arguments'])- assert args['command'] == 'str_replace'- assert args['path'] == '/repo/app.py'- assert args['old_str'] == 'foo'- assert args['new_str'] == 'bar'---def test_convert_tools_to_description():- formatted_tools = convert_tools_to_description(FNCALL_TOOLS)- print(formatted_tools)- assert (- formatted_tools.strip()- == """---- BEGIN FUNCTION #1: execute_bash -----Description: Execute a bash command in the terminal.-* Long running commands: For commands that may run indefinitely, it should be run in the background and the output should be redirected to a file, e.g. command = `python3 app.py > server.log 2>&1 &`.-* Interactive: If a bash command returns exit code `-1`, this means the process is not yet finished. The assistant must then send a second call to terminal with an empty `command` (which will retrieve any additional logs), or it can send additional text (set `command` to the text) to STDIN of the running process, or it can send command=`ctrl+c` to interrupt the process.-* Timeout: If a command execution result says "Command timed out. Sending SIGINT to the process", the assistant should retry running the command in the background.--Parameters:- (1) command (string, required): The bash command to execute. Can be empty to view additional logs when previous exit code is `-1`. Can be `ctrl+c` to interrupt the currently running process.----- END FUNCTION #1 ---------- BEGIN FUNCTION #2: finish -----Description: Finish the interaction when the task is complete OR if the assistant cannot proceed further with the task.-No parameters are required for this function.----- END FUNCTION #2 ---------- BEGIN FUNCTION #3: str_replace_editor -----Description: Custom editing tool for viewing, creating and editing files-* State is persistent across command calls and discussions with the user-* If `path` is a file, `view` displays the result of applying `cat -n`. If `path` is a directory, `view` lists non-hidden files and directories up to 2 levels deep-* The `create` command cannot be used if the specified `path` already exists as a file-* If a `command` generates a long output, it will be truncated and marked with `<response clipped>`-* The `undo_edit` command will revert the last edit made to the file at `path`--Notes for using the `str_replace` command:-* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces!-* If the `old_str` parameter is not unique in the file, the replacement will not be performed. Make sure to include enough context in `old_str` to make it unique-* The `new_str` parameter should contain the edited lines that should replace the `old_str`--Parameters:- (1) command (string, required): The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.-Allowed values: [`view`, `create`, `str_replace`, `insert`, `undo_edit`]- (2) path (string, required): Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.- (3) file_text (string, optional): Required parameter of `create` command, with the content of the file to be created.- (4) old_str (string, optional): Required parameter of `str_replace` command containing the string in `path` to replace.- (5) new_str (string, optional): Optional parameter of `str_replace` command containing the new string (if not given, no string will be added). Required parameter of `insert` command containing the string to insert.- (6) insert_line (integer, optional): Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.- (7) view_range (array, optional): Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.----- END FUNCTION #3 ----""".strip()- )---def test_get_example_for_tools_no_tools():- """Test that get_example_for_tools returns empty string when no tools are available."""- tools = []- example = get_example_for_tools(tools)- assert example == ''---def test_get_example_for_tools_single_tool():- """Test that get_example_for_tools generates correct example with a single tool."""- tools = [- {- 'type': 'function',- 'function': {- 'name': 'execute_bash',- 'description': 'Execute a bash command in the terminal.',- 'parameters': {- 'type': 'object',- 'properties': {- 'command': {- 'type': 'string',- 'description': 'The bash command to execute.',- }- },- 'required': ['command'],- },- },- }- ]- example = get_example_for_tools(tools)- assert example.startswith(- "Here's a running example of how to perform a task with the provided tools."- )- assert (- 'USER: Create a list of numbers from 1 to 10, and display them in a web page at port 5000.'- in example- )- assert TOOL_EXAMPLES['execute_bash']['check_dir'] in example- assert TOOL_EXAMPLES['execute_bash']['run_server'] in example- assert TOOL_EXAMPLES['execute_bash']['kill_server'] in example- assert TOOL_EXAMPLES['str_replace_editor']['create_file'] not in example- assert TOOL_EXAMPLES['browser']['view_page'] not in example- assert TOOL_EXAMPLES['finish']['example'] not in example---def test_get_example_for_tools_single_tool_is_finish():- """Test get_example_for_tools with only the finish tool."""- tools = [- {- 'type': 'function',- 'function': {- 'name': 'finish',- 'description': 'Finish the interaction when the task is complete.',- },- }- ]- example = get_example_for_tools(tools)- assert example.startswith(- "Here's a running example of how to perform a task with the provided tools."- )- assert (- 'USER: Create a list of numbers from 1 to 10, and display them in a web page at port 5000.'- in example- )- assert TOOL_EXAMPLES['finish']['example'] in example- assert TOOL_EXAMPLES['execute_bash']['check_dir'] not in example- assert TOOL_EXAMPLES['str_replace_editor']['create_file'] not in example- assert TOOL_EXAMPLES['browser']['view_page'] not in example---def test_get_example_for_tools_multiple_tools():- """Test that get_example_for_tools generates correct example with multiple tools."""- tools = [- {- 'type': 'function',- 'function': {- 'name': 'execute_bash',- 'description': 'Execute a bash command in the terminal.',- 'parameters': {- 'type': 'object',- 'properties': {- 'command': {- 'type': 'string',- 'description': 'The bash command to execute.',- }- },- 'required': ['command'],- },- },- },- {- 'type': 'function',- 'function': {- 'name': 'str_replace_editor',- 'description': 'Custom editing tool for viewing, creating and editing files.',- 'parameters': {- 'type': 'object',- 'properties': {- 'command': {- 'type': 'string',- 'description': 'The commands to run.',- 'enum': [- 'view',- 'create',- 'str_replace',- 'insert',- 'undo_edit',- ],- },- 'path': {- 'type': 'string',- 'description': 'Absolute path to file or directory.',- },- },- 'required': ['command', 'path'],- },- },- },- ]- example = get_example_for_tools(tools)- assert example.startswith(- "Here's a running example of how to perform a task with the provided tools."- )- assert (- 'USER: Create a list of numbers from 1 to 10, and display them in a web page at port 5000.'- in example- )- assert TOOL_EXAMPLES['execute_bash']['check_dir'] in example- assert TOOL_EXAMPLES['execute_bash']['run_server'] in example- assert TOOL_EXAMPLES['execute_bash']['kill_server'] in example- assert TOOL_EXAMPLES['str_replace_editor']['create_file'] in example- assert TOOL_EXAMPLES['str_replace_editor']['edit_file'] in example- assert TOOL_EXAMPLES['browser']['view_page'] not in example- assert TOOL_EXAMPLES['finish']['example'] not in example---def test_get_example_for_tools_multiple_tools_with_finish():- """Test get_example_for_tools with multiple tools including finish."""- # Uses execute_bash and finish tools- tools = [- {- 'type': 'function',- 'function': {- 'name': 'execute_bash',- 'description': 'Execute a bash command in the terminal.',- 'parameters': { # Params added for completeness, not strictly needed by get_example_for_tools- 'type': 'object',- 'properties': {- 'command': {- 'type': 'string',- 'description': 'The bash command to execute.',- }- },- 'required': ['command'],- },- },- },- {- 'type': 'function',- 'function': {- 'name': 'str_replace_editor',- 'description': 'Custom editing tool for viewing, creating and editing files.',- 'parameters': {- 'type': 'object',- 'properties': {- 'command': {- 'type': 'string',- 'description': 'The commands to run.',- 'enum': [- 'view',- 'create',- 'str_replace',- 'insert',- 'undo_edit',- ],- },- 'path': {- 'type': 'string',- 'description': 'Absolute path to file or directory.',- },- },- 'required': ['command', 'path'],- },- },- },- {- 'type': 'function',- 'function': {- 'name': 'browser',- 'description': 'Interact with the browser.',- 'parameters': {- 'type': 'object',- 'properties': {- 'code': {- 'type': 'string',- 'description': 'The Python code that interacts with the browser.',- }- },- 'required': ['code'],- },- },- },- {- 'type': 'function',- 'function': {- 'name': 'finish',- 'description': 'Finish the interaction.',- },- },- ]- example = get_example_for_tools(tools)- assert example.startswith(- "Here's a running example of how to perform a task with the provided tools."- )- assert (- 'USER: Create a list of numbers from 1 to 10, and display them in a web page at port 5000.'- in example- )-- # Check for execute_bash parts (order matters for get_example_for_tools)- assert TOOL_EXAMPLES['execute_bash']['check_dir'].strip() in example- assert TOOL_EXAMPLES['execute_bash']['run_server'].strip() in example- assert TOOL_EXAMPLES['execute_bash']['kill_server'].strip() in example- assert TOOL_EXAMPLES['execute_bash']['run_server_again'].strip() in example-- # Check for str_replace_editor parts- assert TOOL_EXAMPLES['str_replace_editor']['create_file'] in example- assert TOOL_EXAMPLES['str_replace_editor']['edit_file'] in example-- # Check for browser part- assert TOOL_EXAMPLES['browser']['view_page'] in example-- # Check for finish part- assert TOOL_EXAMPLES['finish']['example'] in example---def test_get_example_for_tools_all_tools():- """Test that get_example_for_tools generates correct example with all tools."""- tools = FNCALL_TOOLS # FNCALL_TOOLS already includes 'finish'- example = get_example_for_tools(tools)- assert example.startswith(- "Here's a running example of how to perform a task with the provided tools."- )- assert (- 'USER: Create a list of numbers from 1 to 10, and display them in a web page at port 5000.'- in example- )- assert TOOL_EXAMPLES['execute_bash']['check_dir'] in example- assert TOOL_EXAMPLES['execute_bash']['run_server'] in example- assert TOOL_EXAMPLES['execute_bash']['kill_server'] in example- assert TOOL_EXAMPLES['str_replace_editor']['create_file'] in example- assert TOOL_EXAMPLES['str_replace_editor']['edit_file'] in example- assert TOOL_EXAMPLES['finish']['example'] in example-- # These are not in global FNCALL_TOOLS- # assert TOOL_EXAMPLES['web_read']['read_docs'] not in example # web_read is removed- assert TOOL_EXAMPLES['browser']['view_page'] not in example---FNCALL_MESSAGES = [- {- 'content': [- {- 'type': 'text',- 'text': "You are a helpful assistant that can interact with a computer to solve tasks.\n<IMPORTANT>\n* If user provides a path, you should NOT assume it's relative to the current working directory. Instead, you should explore the file system to find the file before working on it.\n</IMPORTANT>\n\n",- 'cache_control': {'type': 'ephemeral'},- }- ],- 'role': 'system',- },- {- 'content': [- {- 'type': 'text',- 'text': "<uploaded_files>\n/workspace/astropy__astropy__5.1\n</uploaded_files>\nI've uploaded a python code repository in the directory astropy__astropy__5.1. LONG DESCRIPTION:\n\n",- }- ],- 'role': 'user',- },- {- 'content': [- {- 'type': 'text',- 'text': "I'll help you implement the necessary changes to meet the requirements. Let's follow the steps:\n\n1. First, let's explore the repository structure:",- }- ],- 'role': 'assistant',- 'tool_calls': [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "ls -la /workspace/astropy__astropy__5.1"}',- 'name': 'execute_bash',- },- 'id': 'toolu_01',- 'type': 'function',- }- ],- },- {- 'content': [- {- 'type': 'text',- 'text': 'ls -la /workspace/astropy__astropy__5.1\r\nls: /workspace/astropy__astropy__5.1: Bad file descriptor\r\nlrwxrwxrwx 1 root root 8 Oct 28 21:58 /workspace/astropy__astropy__5.1 -> /testbed[Python Interpreter: /opt/miniconda3/envs/testbed/bin/python]\nroot@openhands-workspace:/workspace/astropy__astropy__5.1 # \n[Command finished with exit code 0]',- }- ],- 'role': 'tool',- 'tool_call_id': 'toolu_01',- 'name': 'execute_bash',- },- {- 'content': [- {- 'type': 'text',- 'text': "I see there's a symlink. Let's explore the actual directory:",- }- ],- 'role': 'assistant',- 'tool_calls': [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "ls -la /testbed"}',- 'name': 'execute_bash',- },- 'id': 'toolu_02',- 'type': 'function',- }- ],- },- {- 'content': [- {- 'type': 'text',- 'text': 'SOME OBSERVATION',- }- ],- 'role': 'tool',- 'tool_call_id': 'toolu_02',- 'name': 'execute_bash',- },- {- 'content': [- {- 'type': 'text',- 'text': "Let's look at the source code file mentioned in the PR description:",- }- ],- 'role': 'assistant',- 'tool_calls': [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "view", "path": "/testbed/astropy/io/fits/card.py"}',- 'name': 'str_replace_editor',- },- 'id': 'toolu_03',- 'type': 'function',- }- ],- },- {- 'content': [- {- 'type': 'text',- 'text': "Here's the result of running `cat -n` on /testbed/astropy/io/fits/card.py:\n 1\t# Licensed under a 3-clause BSD style license - see PYFITS.rst...VERY LONG TEXT",- }- ],- 'role': 'tool',- 'tool_call_id': 'toolu_03',- 'name': 'str_replace_editor',- },-]--NON_FNCALL_MESSAGES = [- {- 'role': 'system',- 'content': [- {- 'type': 'text',- 'text': 'You are a helpful assistant that can interact with a computer to solve tasks.\n<IMPORTANT>\n* If user provides a path, you should NOT assume it\'s relative to the current working directory. Instead, you should explore the file system to find the file before working on it.\n</IMPORTANT>\n\n\nYou have access to the following functions:\n\n---- BEGIN FUNCTION #1: execute_bash ----\nDescription: Execute a bash command in the terminal.\n* Long running commands: For commands that may run indefinitely, it should be run in the background and the output should be redirected to a file, e.g. command = `python3 app.py > server.log 2>&1 &`.\n* Interactive: If a bash command returns exit code `-1`, this means the process is not yet finished. The assistant must then send a second call to terminal with an empty `command` (which will retrieve any additional logs), or it can send additional text (set `command` to the text) to STDIN of the running process, or it can send command=`ctrl+c` to interrupt the process.\n* Timeout: If a command execution result says "Command timed out. Sending SIGINT to the process", the assistant should retry running the command in the background.\n\nParameters:\n (1) command (string, required): The bash command to execute. Can be empty to view additional logs when previous exit code is `-1`. Can be `ctrl+c` to interrupt the currently running process.\n---- END FUNCTION #1 ----\n\n---- BEGIN FUNCTION #2: finish ----\nDescription: Finish the interaction when the task is complete OR if the assistant cannot proceed further with the task.\nNo parameters are required for this function.\n---- END FUNCTION #2 ----\n\n---- BEGIN FUNCTION #3: str_replace_editor ----\nDescription: Custom editing tool for viewing, creating and editing files\n* State is persistent across command calls and discussions with the user\n* If `path` is a file, `view` displays the result of applying `cat -n`. If `path` is a directory, `view` lists non-hidden files and directories up to 2 levels deep\n* The `create` command cannot be used if the specified `path` already exists as a file\n* If a `command` generates a long output, it will be truncated and marked with `<response clipped>`\n* The `undo_edit` command will revert the last edit made to the file at `path`\n\nNotes for using the `str_replace` command:\n* The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file. Be mindful of whitespaces!\n* If the `old_str` parameter is not unique in the file, the replacement will not be performed. Make sure to include enough context in `old_str` to make it unique\n* The `new_str` parameter should contain the edited lines that should replace the `old_str`\n\nParameters:\n (1) command (string, required): The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.\nAllowed values: [`view`, `create`, `str_replace`, `insert`, `undo_edit`]\n (2) path (string, required): Absolute path to file or directory, e.g. `/repo/file.py` or `/repo`.\n (3) file_text (string, optional): Required parameter of `create` command, with the content of the file to be created.\n (4) old_str (string, optional): Required parameter of `str_replace` command containing the string in `path` to replace.\n (5) new_str (string, optional): Optional parameter of `str_replace` command containing the new string (if not given, no string will be added). Required parameter of `insert` command containing the string to insert.\n (6) insert_line (integer, optional): Required parameter of `insert` command. The `new_str` will be inserted AFTER the line `insert_line` of `path`.\n (7) view_range (array, optional): Optional parameter of `view` command when `path` points to a file. If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[start_line, -1]` shows all lines from `start_line` to the end of the file.\n---- END FUNCTION #3 ----\n\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<function=example_function_name>\n<parameter=example_parameter_1>value_1</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format, start with <function= and end with </function>\n- Required parameters MUST be specified\n- Only call one function at a time\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after.\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>\n',- 'cache_control': {'type': 'ephemeral'},- }- ],- },- {- 'content': [- {- 'type': 'text',- 'text': IN_CONTEXT_LEARNING_EXAMPLE_PREFIX(FNCALL_TOOLS)- + "<uploaded_files>\n/workspace/astropy__astropy__5.1\n</uploaded_files>\nI've uploaded a python code repository in the directory astropy__astropy__5.1. LONG DESCRIPTION:\n\n"- + IN_CONTEXT_LEARNING_EXAMPLE_SUFFIX,- }- ],- 'role': 'user',- },- {- 'role': 'assistant',- 'content': [- {- 'type': 'text',- 'text': "I'll help you implement the necessary changes to meet the requirements. Let's follow the steps:\n\n1. First, let's explore the repository structure:\n\n<function=execute_bash>\n<parameter=command>ls -la /workspace/astropy__astropy__5.1</parameter>\n</function>",- }- ],- },- {- 'role': 'user',- 'content': [- {- 'type': 'text',- 'text': 'EXECUTION RESULT of [execute_bash]:\nls -la /workspace/astropy__astropy__5.1\r\nls: /workspace/astropy__astropy__5.1: Bad file descriptor\r\nlrwxrwxrwx 1 root root 8 Oct 28 21:58 /workspace/astropy__astropy__5.1 -> /testbed[Python Interpreter: /opt/miniconda3/envs/testbed/bin/python]\nroot@openhands-workspace:/workspace/astropy__astropy__5.1 # \n[Command finished with exit code 0]',- }- ],- },- {- 'role': 'assistant',- 'content': [- {- 'type': 'text',- 'text': "I see there's a symlink. Let's explore the actual directory:\n\n<function=execute_bash>\n<parameter=command>ls -la /testbed</parameter>\n</function>",- }- ],- },- {- 'role': 'user',- 'content': [- {- 'type': 'text',- 'text': 'EXECUTION RESULT of [execute_bash]:\nSOME OBSERVATION',- }- ],- },- {- 'role': 'assistant',- 'content': [- {- 'type': 'text',- 'text': "Let's look at the source code file mentioned in the PR description:\n\n<function=str_replace_editor>\n<parameter=command>view</parameter>\n<parameter=path>/testbed/astropy/io/fits/card.py</parameter>\n</function>",- }- ],- },- {- 'role': 'user',- 'content': [- {- 'type': 'text',- 'text': "EXECUTION RESULT of [str_replace_editor]:\nHere's the result of running `cat -n` on /testbed/astropy/io/fits/card.py:\n 1\t# Licensed under a 3-clause BSD style license - see PYFITS.rst...VERY LONG TEXT",- }- ],- },-]--FNCALL_RESPONSE_MESSAGE = {- 'content': [- {- 'type': 'text',- 'text': 'Let me search for the `_format_float` method mentioned in the PR description:',- }- ],- 'role': 'assistant',- 'tool_calls': [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "grep -n \\"_format_float\\" /testbed/astropy/io/fits/card.py"}',- 'name': 'execute_bash',- },- 'id': 'toolu_04',- 'type': 'function',- }- ],-}--NON_FNCALL_RESPONSE_MESSAGE = {- 'content': [- {- 'type': 'text',- 'text': 'Let me search for the `_format_float` method mentioned in the PR description:\n\n<function=execute_bash>\n<parameter=command>grep -n "_format_float" /testbed/astropy/io/fits/card.py</parameter>\n</function>',- }- ],- 'role': 'assistant',-}--- 'tool_calls, expected',- [- # Original test case- (- FNCALL_RESPONSE_MESSAGE['tool_calls'],- """<function=execute_bash>-<parameter=command>grep -n "_format_float" /testbed/astropy/io/fits/card.py</parameter>-</function>""",- ),- # Test case with multiple parameters- (- [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "view", "path": "/test/file.py", "view_range": [1, 10]}',- 'name': 'str_replace_editor',- },- 'id': 'test_id',- 'type': 'function',- }- ],- """<function=str_replace_editor>-<parameter=command>view</parameter>-<parameter=path>/test/file.py</parameter>-<parameter=view_range>[1, 10]</parameter>-</function>""",- ),- # Test case with indented code block to verify indentation is preserved- # Note: multiline parameter values should NOT have extra newlines before/after- # to prevent newline accumulation across multiple LLM response cycles- (- [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "str_replace", "path": "/test/file.py", "old_str": "def example():\\n pass", "new_str": "def example():\\n # This is indented\\n print(\\"hello\\")\\n return True"}',- 'name': 'str_replace_editor',- },- 'id': 'test_id',- 'type': 'function',- }- ],- """<function=str_replace_editor>-<parameter=command>str_replace</parameter>-<parameter=path>/test/file.py</parameter>-<parameter=old_str>def example():- pass</parameter>-<parameter=new_str>def example():- # This is indented- print("hello")- return True</parameter>-</function>""",- ),- # Test case with list parameter value- (- [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "test", "path": "/test/file.py", "tags": ["tag1", "tag2", "tag with spaces"]}',- 'name': 'test_function',- },- 'id': 'test_id',- 'type': 'function',- }- ],- """<function=test_function>-<parameter=command>test</parameter>-<parameter=path>/test/file.py</parameter>-<parameter=tags>["tag1", "tag2", "tag with spaces"]</parameter>-</function>""",- ),- # Test case with dict parameter value- (- [- {- 'index': 1,- 'function': {- 'arguments': '{"command": "test", "path": "/test/file.py", "metadata": {"key1": "value1", "key2": 42, "nested": {"subkey": "subvalue"}}}',- 'name': 'test_function',- },- 'id': 'test_id',- 'type': 'function',- }- ],- """<function=test_function>-<parameter=command>test</parameter>-<parameter=path>/test/file.py</parameter>-<parameter=metadata>{"key1": "value1", "key2": 42, "nested": {"subkey": "subvalue"}}</parameter>-</function>""",- ),- ],-)-def test_convert_tool_call_to_string(tool_calls, expected):- assert len(tool_calls) == 1- converted = convert_tool_call_to_string(tool_calls[0])- print(converted)- assert converted == expected---def test_convert_fncall_messages_to_non_fncall_messages():- converted_non_fncall = convert_fncall_messages_to_non_fncall_messages(- FNCALL_MESSAGES, FNCALL_TOOLS- )- assert converted_non_fncall == NON_FNCALL_MESSAGES---def test_convert_non_fncall_messages_to_fncall_messages():- converted = convert_non_fncall_messages_to_fncall_messages(- NON_FNCALL_MESSAGES, FNCALL_TOOLS- )- print(json.dumps(converted, indent=2))- assert converted == FNCALL_MESSAGES---def test_two_way_conversion_nonfn_to_fn_to_nonfn():- non_fncall_copy = copy.deepcopy(NON_FNCALL_MESSAGES)- converted_fncall = convert_non_fncall_messages_to_fncall_messages(- NON_FNCALL_MESSAGES, FNCALL_TOOLS- )- assert (- non_fncall_copy == NON_FNCALL_MESSAGES- ) # make sure original messages are not modified- assert converted_fncall == FNCALL_MESSAGES-- fncall_copy = copy.deepcopy(FNCALL_MESSAGES)- converted_non_fncall = convert_fncall_messages_to_non_fncall_messages(- FNCALL_MESSAGES, FNCALL_TOOLS- )- assert (- fncall_copy == FNCALL_MESSAGES- ) # make sure original messages are not modified- assert converted_non_fncall == NON_FNCALL_MESSAGES---def test_two_way_conversion_fn_to_nonfn_to_fn():- fncall_copy = copy.deepcopy(FNCALL_MESSAGES)- converted_non_fncall = convert_fncall_messages_to_non_fncall_messages(- FNCALL_MESSAGES, FNCALL_TOOLS- )- assert (- fncall_copy == FNCALL_MESSAGES- ) # make sure original messages are not modified- assert converted_non_fncall == NON_FNCALL_MESSAGES-- non_fncall_copy = copy.deepcopy(NON_FNCALL_MESSAGES)- converted_fncall = convert_non_fncall_messages_to_fncall_messages(- NON_FNCALL_MESSAGES, FNCALL_TOOLS- )- assert (- non_fncall_copy == NON_FNCALL_MESSAGES- ) # make sure original messages are not modified- assert converted_fncall == FNCALL_MESSAGES---def test_infer_fncall_on_noncall_model():- messages_for_llm_inference = convert_fncall_messages_to_non_fncall_messages(- FNCALL_MESSAGES, FNCALL_TOOLS- )- assert messages_for_llm_inference == NON_FNCALL_MESSAGES- # Mock LLM inference- response_message_from_llm_inference = NON_FNCALL_RESPONSE_MESSAGE-- # Convert back to fncall messages to hand back to the agent- # so agent is model-agnostic- all_nonfncall_messages = NON_FNCALL_MESSAGES + [response_message_from_llm_inference]- converted_fncall_messages = convert_non_fncall_messages_to_fncall_messages(- all_nonfncall_messages, FNCALL_TOOLS- )- assert converted_fncall_messages == FNCALL_MESSAGES + [FNCALL_RESPONSE_MESSAGE]- assert converted_fncall_messages[-1] == FNCALL_RESPONSE_MESSAGE---def test_convert_from_multiple_tool_calls_to_single_tool_call_messages():- # Test case with multiple tool calls in one message- input_messages = [- {- 'role': 'assistant',- 'content': 'Let me help you with that.',- 'tool_calls': [- {- 'id': 'call1',- 'type': 'function',- 'function': {'name': 'func1', 'arguments': '{}'},- },- {- 'id': 'call2',- 'type': 'function',- 'function': {'name': 'func2', 'arguments': '{}'},- },- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call1',- 'content': 'Result 1',- 'name': 'func1',- },- {- 'role': 'tool',- 'tool_call_id': 'call2',- 'content': 'Result 2',- 'name': 'func2',- },- {- 'role': 'assistant',- 'content': 'Test again',- 'tool_calls': [- {- 'id': 'call3',- 'type': 'function',- 'function': {'name': 'func3', 'arguments': '{}'},- },- {- 'id': 'call4',- 'type': 'function',- 'function': {'name': 'func4', 'arguments': '{}'},- },- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call3',- 'content': 'Result 3',- 'name': 'func3',- },- {- 'role': 'tool',- 'tool_call_id': 'call4',- 'content': 'Result 4',- 'name': 'func4',- },- ]-- expected_output = [- {- 'role': 'assistant',- 'content': 'Let me help you with that.',- 'tool_calls': [- {- 'id': 'call1',- 'type': 'function',- 'function': {'name': 'func1', 'arguments': '{}'},- }- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call1',- 'content': 'Result 1',- 'name': 'func1',- },- {- 'role': 'assistant',- 'content': '',- 'tool_calls': [- {- 'id': 'call2',- 'type': 'function',- 'function': {'name': 'func2', 'arguments': '{}'},- }- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call2',- 'content': 'Result 2',- 'name': 'func2',- },- {- 'role': 'assistant',- 'content': 'Test again',- 'tool_calls': [- {- 'id': 'call3',- 'type': 'function',- 'function': {'name': 'func3', 'arguments': '{}'},- }- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call3',- 'content': 'Result 3',- 'name': 'func3',- },- {- 'role': 'assistant',- 'content': '',- 'tool_calls': [- {- 'id': 'call4',- 'type': 'function',- 'function': {'name': 'func4', 'arguments': '{}'},- }- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call4',- 'content': 'Result 4',- 'name': 'func4',- },- ]-- result = convert_from_multiple_tool_calls_to_single_tool_call_messages(- input_messages- )- assert result == expected_output---def test_convert_from_multiple_tool_calls_to_single_tool_call_messages_incomplete():- # Test case with multiple tool calls in one message- input_messages = [- {- 'role': 'assistant',- 'content': 'Let me help you with that.',- 'tool_calls': [- {- 'id': 'call1',- 'type': 'function',- 'function': {'name': 'func1', 'arguments': '{}'},- },- {- 'id': 'call2',- 'type': 'function',- 'function': {'name': 'func2', 'arguments': '{}'},- },- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call1',- 'content': 'Result 1',- 'name': 'func1',- },- ]-- with pytest.raises(FunctionCallConversionError):- convert_from_multiple_tool_calls_to_single_tool_call_messages(input_messages)---def test_convert_from_multiple_tool_calls_no_changes_needed():- # Test case where no conversion is needed (single tool call)- input_messages = [- {- 'role': 'assistant',- 'content': 'Let me help you with that.',- 'tool_calls': [- {- 'id': 'call1',- 'type': 'function',- 'function': {'name': 'func1', 'arguments': '{}'},- }- ],- },- {- 'role': 'tool',- 'tool_call_id': 'call1',- 'content': 'Result 1',- 'name': 'func1',- },- ]-- result = convert_from_multiple_tool_calls_to_single_tool_call_messages(- input_messages- )- assert result == input_messages---def test_convert_from_multiple_tool_calls_no_tool_calls():- # Test case with no tool calls- input_messages = [- {'role': 'user', 'content': 'Hello'},- {'role': 'assistant', 'content': 'Hi there!'},- ]-- result = convert_from_multiple_tool_calls_to_single_tool_call_messages(- input_messages- )- assert result == input_messages---def test_convert_fncall_messages_with_cache_control():- """Test that cache_control is properly handled in tool messages."""- # Prepare test data- messages = [- {- 'role': 'tool',- 'name': 'test_tool',- 'content': [{'type': 'text', 'text': 'test content'}],- 'cache_control': {'type': 'ephemeral'},- }- ]-- # Call the function- result = convert_fncall_messages_to_non_fncall_messages(messages, [])-- # Verify the result- assert len(result) == 1- assert result[0]['role'] == 'user'- assert 'cache_control' in result[0]['content'][-1]- assert result[0]['content'][-1]['cache_control'] == {'type': 'ephemeral'}- assert (- result[0]['content'][0]['text']- == 'EXECUTION RESULT of [test_tool]:\ntest content'- )---def test_convert_fncall_messages_without_cache_control():- """Test that tool messages without cache_control work as expected."""- # Prepare test data- messages = [- {- 'role': 'tool',- 'name': 'test_tool',- 'content': [{'type': 'text', 'text': 'test content'}],- }- ]-- # Call the function- result = convert_fncall_messages_to_non_fncall_messages(messages, [])-- # Verify the result- assert len(result) == 1- assert result[0]['role'] == 'user'- assert 'cache_control' not in result[0]['content'][-1]- assert (- result[0]['content'][0]['text']- == 'EXECUTION RESULT of [test_tool]:\ntest content'- )---def test_convert_fncall_messages_with_image_url():- """Test that convert_fncall_messages_to_non_fncall_messages handles image URLs correctly."""- messages = [- {- 'role': 'tool',- 'name': 'browser',- 'content': [- {- 'type': 'text',- 'text': 'some browser tool results',- },- {- 'type': 'image_url',- 'image_url': {'url': 'data:image/gif;base64,R0lGODlhAQABAAAAACw='},- },- ],- }- ]- converted_messages = convert_fncall_messages_to_non_fncall_messages(messages, [])- assert len(converted_messages) == 1- assert converted_messages[0]['role'] == 'user'- assert len(converted_messages[0]['content']) == len(messages[0]['content'])- assert (- next(c for c in converted_messages[0]['content'] if c['type'] == 'text')['text']- == f'EXECUTION RESULT of [{messages[0]["name"]}]:\n{messages[0]["content"][0]["text"]}'- )diff --git a/tests/unit/llm/test_model_features.py b/tests/unit/llm/test_model_features.pydeleted file mode 100644index 7226ed33b0..0000000000--- a/tests/unit/llm/test_model_features.py+++ /dev/null@@ -1,307 +0,0 @@-import pytest--from openhands.llm.model_features import (- ModelFeatures,- get_features,- model_matches,- normalize_model_name,-)--- 'raw,expected',- [- (' OPENAI/gpt-4o ', 'gpt-4o'),- ('anthropic/claude-3-7-sonnet', 'claude-3-7-sonnet'),- ('litellm_proxy/gemini-2.5-pro', 'gemini-2.5-pro'),- ('qwen3-coder-480b-a35b-instruct', 'qwen3-coder-480b-a35b-instruct'),- ('gpt-5', 'gpt-5'),- ('deepseek/DeepSeek-R1-0528:671b-Q4_K_XL', 'deepseek-r1-0528'),- ('openai/GLM-4.5-GGUF', 'glm-4.5'),- ('openrouter/gpt-4o-mini', 'gpt-4o-mini'),- (- 'bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0',- 'anthropic.claude-3-5-sonnet-20241022-v2',- ),- ('', ''),- (None, ''), # type: ignore[arg-type]- ],-)-def test_normalize_model_name(raw, expected):- assert normalize_model_name(raw) == expected--- 'name,pattern,expected',- [- ('gpt-4o', 'gpt-4o*', True),- ('openai/gpt-4o', 'gpt-4o*', True),- ('litellm_proxy/gpt-4o-mini', 'gpt-4o*', True),- ('claude-3-7-sonnet-20250219', 'claude-3-7-sonnet*', True),- ('o1-2024-12-17', 'o1*', True),- ('grok-4-0709', 'grok-4-0709', True),- ('grok-4-0801', 'grok-4-0709', False),- ],-)-def test_model_matches(name, pattern, expected):- assert model_matches(name, [pattern]) is expected--- 'name,pattern,expected',- [- ('openai/gpt-4o', 'openai/gpt-4o*', True),- ('openrouter/gpt-4o', 'openai/gpt-4o*', False),- ('litellm_proxy/gpt-4o-mini', 'litellm_proxy/gpt-4o*', True),- (- 'gpt-4o',- 'openai/gpt-4o*',- False,- ), # basename alone should not match provider-qualified- ('unknown-model', 'gpt-5*', False),- ],-)-def test_model_matches_provider_qualified(name, pattern, expected):- assert model_matches(name, [pattern]) is expected--- 'model,expect',- [- (- 'gpt-4o',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=False,- supports_prompt_cache=False,- supports_stop_words=True,- ),- ),- (- 'gpt-5',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=True,- supports_prompt_cache=False,- supports_stop_words=True,- ),- ),- (- 'gpt-5-mini-2025-08-07',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=True,- supports_prompt_cache=False,- supports_stop_words=True,- ),- ),- (- 'o3-mini',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=True,- supports_prompt_cache=False,- supports_stop_words=True,- ),- ),- (- 'o1-2024-12-17',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=True,- supports_prompt_cache=False,- supports_stop_words=False,- ),- ),- (- 'xai/grok-4-0709',- ModelFeatures(- supports_function_calling=False,- supports_reasoning_effort=False,- supports_prompt_cache=False,- supports_stop_words=False,- ),- ),- (- 'anthropic/claude-3-7-sonnet',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=False,- supports_prompt_cache=True,- supports_stop_words=True,- ),- ),- (- 'litellm_proxy/claude-3.7-sonnet',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=False,- supports_prompt_cache=True,- supports_stop_words=True,- ),- ),- (- 'gemini-2.5-pro',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=True,- supports_prompt_cache=False,- supports_stop_words=True,- ),- ),- (- 'openai/gpt-4o',- ModelFeatures(- supports_function_calling=True,- supports_reasoning_effort=False,- supports_prompt_cache=False,- supports_stop_words=True,- ),- ), # provider-qualified still matches basename patterns- ],-)-def test_get_features(model, expect):- features = get_features(model)- assert features == expect--- 'model',- [- # Anthropic families- 'claude-3-7-sonnet-20250219',- 'claude-3.7-sonnet',- 'claude-sonnet-3-7-latest',- 'claude-3-5-sonnet',- 'claude-3.5-haiku',- 'claude-3-5-haiku-20241022',- 'claude-sonnet-4-latest',- 'claude-opus-4-1-20250805',- # OpenAI families- 'gpt-4o',- 'gpt-4.1',- 'gpt-5',- 'gpt-5-mini-2025-08-07',- # o-series- 'o1-2024-12-17',- 'o3-mini',- 'o4-mini',- # Google Gemini- 'gemini-2.5-pro',- 'gemini-3-pro-preview',- # Others- 'kimi-k2-0711-preview',- 'kimi-k2-instruct',- 'qwen3-coder',- 'qwen3-coder-480b-a35b-instruct',- ],-)-def test_function_calling_models(model):- features = get_features(model)- assert features.supports_function_calling is True--- 'model',- [- 'o1-2024-12-17',- 'o3-mini',- 'o4-mini',- 'gemini-2.5-flash',- 'gemini-2.5-pro',- 'gpt-5',- 'gpt-5-mini-2025-08-07',- ],-)-def test_reasoning_effort_models(model):- features = get_features(model)- assert features.supports_reasoning_effort is True--- 'model',- [- 'deepseek/DeepSeek-R1-0528:671b-Q4_K_XL',- 'DeepSeek-R1-0528',- ],-)-def test_deepseek_reasoning_effort_models(model):- features = get_features(model)- assert features.supports_reasoning_effort is True--- 'model',- [- 'claude-3-7-sonnet-20250219',- 'claude-3.7-sonnet',- 'claude-sonnet-3-7-latest',- 'claude-3-5-sonnet',- 'claude-3-5-haiku-20241022',- 'claude-3-haiku-20240307',- 'claude-3-opus-20240229',- 'claude-sonnet-4-latest',- ],-)-def test_prompt_cache_models(model):- features = get_features(model)- assert features.supports_prompt_cache is True--- 'model,expected',- [- # Positive cases: exactly those supported on main- ('o1', True),- ('o1-2024-12-17', True),- ('o3', True),- ('o3-2025-04-16', True),- ('o3-mini', True),- ('o3-mini-2025-01-31', True),- ('o4-mini', True),- ('o4-mini-2025-04-16', True),- ('gemini-2.5-flash', True),- ('gemini-2.5-pro', True),- ('gpt-5', True),- ('gpt-5-2025-08-07', True),- ('gpt-5-mini-2025-08-07', True),- ('claude-opus-4-1-20250805', False),- # DeepSeek- ('deepseek/DeepSeek-R1-0528:671b-Q4_K_XL', True),- ('DeepSeek-R1-0528', True),- # Negative cases: ensure we didn't unintentionally expand- ('o1-mini', False),- ('o1-preview', False),- ('gemini-1.0-pro', False),- ],-)-def test_reasoning_effort_parity_with_main(model, expected):- assert get_features(model).supports_reasoning_effort is expected---def test_prompt_cache_haiku_variants():- assert get_features('claude-3-5-haiku-20241022').supports_prompt_cache is True- assert get_features('claude-3.5-haiku-20241022').supports_prompt_cache is True---def test_stop_words_grok_provider_prefixed():- assert get_features('xai/grok-4-0709').supports_stop_words is False- assert get_features('grok-4-0709').supports_stop_words is False- assert get_features('xai/grok-code-fast-1').supports_stop_words is False- assert get_features('grok-code-fast-1').supports_stop_words is False--- 'model',- [- 'o1-mini',- 'o1-2024-12-17',- 'xai/grok-4-0709',- 'xai/grok-code-fast-1',- 'deepseek/DeepSeek-R1-0528:671b-Q4_K_XL',- 'DeepSeek-R1-0528',- ],-)-def test_supports_stop_words_false_models(model):- features = get_features(model)- assert features.supports_stop_words is Falsediff --git a/tests/unit/test_conversation_summary.py b/tests/unit/test_conversation_summary.pydeleted file mode 100644index 4fad19b9cb..0000000000--- a/tests/unit/test_conversation_summary.py+++ /dev/null@@ -1,81 +0,0 @@-"""Tests for the conversation summary generator."""--from unittest.mock import MagicMock--import pytest--from openhands.core.config import LLMConfig-from openhands.utils.conversation_summary import generate_conversation_title---async def test_generate_conversation_title_empty_message():- """Test that an empty message returns None."""- mock_llm_registry = MagicMock()- mock_llm_config = LLMConfig(model='test-model')-- result = await generate_conversation_title('', mock_llm_config, mock_llm_registry)- assert result is None-- result = await generate_conversation_title(- ' ', mock_llm_config, mock_llm_registry- )- assert result is None---async def test_generate_conversation_title_success():- """Test successful title generation."""- # Create a mock LLM registry that returns a title- mock_llm_registry = MagicMock()- mock_llm_registry.request_extraneous_completion.return_value = 'Generated Title'-- mock_llm_config = LLMConfig(model='test-model')-- result = await generate_conversation_title(- 'Can you help me with Python?', mock_llm_config, mock_llm_registry- )-- assert result == 'Generated Title'- # Verify the mock was called with the expected arguments- mock_llm_registry.request_extraneous_completion.assert_called_once()---async def test_generate_conversation_title_long_title():- """Test that long titles are truncated."""- # Create a mock LLM registry that returns a long title- mock_llm_registry = MagicMock()- mock_llm_registry.request_extraneous_completion.return_value = 'This is a very long title that should be truncated because it exceeds the maximum length'-- mock_llm_config = LLMConfig(model='test-model')-- result = await generate_conversation_title(- 'Can you help me with Python?',- mock_llm_config,- mock_llm_registry,- max_length=30,- )-- # Verify the title is truncated correctly- assert len(result) <= 30- assert result.endswith('...')---async def test_generate_conversation_title_exception():- """Test that exceptions are handled gracefully."""- # Create a mock LLM registry that raises an exception- mock_llm_registry = MagicMock()- mock_llm_registry.request_extraneous_completion.side_effect = Exception(- 'Test error'- )-- mock_llm_config = LLMConfig(model='test-model')-- result = await generate_conversation_title(- 'Can you help me with Python?', mock_llm_config, mock_llm_registry- )-- # Verify that None is returned when an exception occurs- assert result is Nonediff --git a/tests/unit/test_llm_registry.py b/tests/unit/test_llm_registry.pydeleted file mode 100644index beb873966d..0000000000--- a/tests/unit/test_llm_registry.py+++ /dev/null@@ -1,178 +0,0 @@-from __future__ import annotations--import unittest-from unittest.mock import MagicMock, patch--from openhands.core.config.llm_config import LLMConfig-from openhands.core.config.openhands_config import OpenHandsConfig-from openhands.llm.llm_registry import LLMRegistry, RegistryEvent---class TestLLMRegistry(unittest.TestCase):- def setUp(self):- """Set up test environment before each test."""- # Create a basic LLM config for testing- self.llm_config = LLMConfig(model='test-model')-- # Create a basic OpenHands config for testing- self.config = OpenHandsConfig(- llms={'llm': self.llm_config}, default_agent='CodeActAgent'- )-- # Create a registry for testing- self.registry = LLMRegistry(config=self.config)-- def test_get_llm_creates_new_llm(self):- """Test that get_llm creates a new LLM when service doesn't exist."""- service_id = 'test-service'-- # Mock the _create_new_llm method to avoid actual LLM initialization- with patch.object(self.registry, '_create_new_llm') as mock_create:- mock_llm = MagicMock()- mock_llm.config = self.llm_config- mock_create.return_value = mock_llm-- # Get LLM for the first time- llm = self.registry.get_llm(service_id, self.llm_config)-- # Verify LLM was created and stored- self.assertEqual(llm, mock_llm)- mock_create.assert_called_once_with(- config=self.llm_config, service_id=service_id- )-- def test_get_llm_returns_existing_llm(self):- """Test that get_llm returns existing LLM when service already exists."""- service_id = 'test-service'-- # Mock the _create_new_llm method to avoid actual LLM initialization- with patch.object(self.registry, '_create_new_llm') as mock_create:- mock_llm = MagicMock()- mock_llm.config = self.llm_config- mock_create.return_value = mock_llm-- # Get LLM for the first time- llm1 = self.registry.get_llm(service_id, self.llm_config)-- # Manually add to registry to simulate existing LLM- self.registry.service_to_llm[service_id] = mock_llm-- # Get LLM for the second time - should return the same instance- llm2 = self.registry.get_llm(service_id, self.llm_config)-- # Verify same LLM instance is returned- self.assertEqual(llm1, llm2)- self.assertEqual(llm1, mock_llm)-- # Verify _create_new_llm was only called once- mock_create.assert_called_once()-- def test_get_llm_with_different_config_raises_error(self):- """Test that requesting same service ID with different config raises an error."""- service_id = 'test-service'- different_config = LLMConfig(model='different-model')-- # Manually add an LLM to the registry to simulate existing service- mock_llm = MagicMock()- mock_llm.config = self.llm_config- self.registry.service_to_llm[service_id] = mock_llm-- # Attempt to get LLM with different config should raise ValueError- with self.assertRaises(ValueError) as context:- self.registry.get_llm(service_id, different_config)-- self.assertIn('Requesting same service ID', str(context.exception))- self.assertIn('with different config', str(context.exception))-- def test_get_llm_without_config_raises_error(self):- """Test that requesting new LLM without config raises an error."""- service_id = 'test-service'-- # Attempt to get LLM without providing config should raise ValueError- with self.assertRaises(ValueError) as context:- self.registry.get_llm(service_id, None)-- self.assertIn(- 'Requesting new LLM without specifying LLM config', str(context.exception)- )-- def test_request_extraneous_completion(self):- """Test that requesting an extraneous completion creates a new LLM if needed."""- service_id = 'extraneous-service'- messages = [{'role': 'user', 'content': 'Hello, world!'}]-- # Mock the _create_new_llm method to avoid actual LLM initialization- with patch.object(self.registry, '_create_new_llm') as mock_create:- mock_llm = MagicMock()- mock_response = MagicMock()- mock_response.choices = [MagicMock()]- mock_response.choices[0].message.content = ' Hello from the LLM! '- mock_llm.completion.return_value = mock_response- mock_create.return_value = mock_llm-- # Mock the side effect to add the LLM to the registry- def side_effect(*args, **kwargs):- self.registry.service_to_llm[service_id] = mock_llm- return mock_llm-- mock_create.side_effect = side_effect-- # Request a completion- response = self.registry.request_extraneous_completion(- service_id=service_id,- llm_config=self.llm_config,- messages=messages,- )-- # Verify the response (should be stripped)- self.assertEqual(response, 'Hello from the LLM!')-- # Verify that _create_new_llm was called with correct parameters- mock_create.assert_called_once_with(- config=self.llm_config, service_id=service_id, with_listener=False- )-- # Verify completion was called with correct messages- mock_llm.completion.assert_called_once_with(messages=messages)-- def test_get_active_llm(self):- """Test that get_active_llm returns the active agent LLM."""- active_llm = self.registry.get_active_llm()- self.assertEqual(active_llm, self.registry.active_agent_llm)-- def test_subscribe_and_notify(self):- """Test the subscription and notification system."""- events_received = []-- def callback(event: RegistryEvent):- events_received.append(event)-- # Subscribe to events- self.registry.subscribe(callback)-- # Should receive notification for the active agent LLM- self.assertEqual(len(events_received), 1)- self.assertEqual(events_received[0].llm, self.registry.active_agent_llm)- self.assertEqual(- events_received[0].service_id, self.registry.active_agent_llm.service_id- )-- # Test that the subscriber is set correctly- self.assertIsNotNone(self.registry.subscriber)-- # Test notify method directly with a mock event- with patch.object(self.registry, 'subscriber') as mock_subscriber:- mock_event = MagicMock()- self.registry.notify(mock_event)- mock_subscriber.assert_called_once_with(mock_event)-- def test_registry_has_unique_id(self):- """Test that each registry instance has a unique ID."""- registry2 = LLMRegistry(config=self.config)- self.assertNotEqual(self.registry.registry_id, registry2.registry_id)- self.assertTrue(len(self.registry.registry_id) > 0)- self.assertTrue(len(registry2.registry_id) > 0)---if __name__ == '__main__':- unittest.main()diff --git a/tests/unit/utils/test_auto_generate_title.py b/tests/unit/utils/test_auto_generate_title.pydeleted file mode 100644index d5207a4416..0000000000--- a/tests/unit/utils/test_auto_generate_title.py+++ /dev/null@@ -1,164 +0,0 @@-"""Tests for the auto-generate title functionality."""--from datetime import datetime, timezone-from unittest.mock import MagicMock, patch--import pytest--from openhands.events.action import MessageAction-from openhands.events.event import EventSource-from openhands.events.event_store import EventStore-from openhands.llm.llm_registry import LLMRegistry-from openhands.storage.data_models.settings import Settings-from openhands.storage.memory import InMemoryFileStore-from openhands.utils.conversation_summary import auto_generate_title---async def test_auto_generate_title_with_llm():- """Test auto-generating a title using LLM."""- # Mock dependencies- file_store = InMemoryFileStore()- llm_registry = MagicMock(spec=LLMRegistry)-- # Create test conversation with a user message- conversation_id = 'test-conversation'- user_id = 'test-user'-- # Create a mock event- user_message = MessageAction(- content='Help me write a Python script to analyze data'- )- user_message._source = EventSource.USER- user_message._id = 1- user_message._timestamp = datetime.now(timezone.utc).isoformat()-- # Mock the EventStore class- with patch(- 'openhands.utils.conversation_summary.EventStore'- ) as mock_event_store_cls:- # Configure the mock event stream to return our test message- mock_event_store = MagicMock(spec=EventStore)- mock_event_store.search_events.return_value = [user_message]- mock_event_store_cls.return_value = mock_event_store-- # Mock the LLM registry response- llm_registry.request_extraneous_completion.return_value = (- 'Python Data Analysis Script'- )-- # Create test settings with LLM config- settings = Settings(- llm_model='test-model',- llm_api_key='test-key',- llm_base_url='test-url',- )-- # Call the auto_generate_title function directly- title = await auto_generate_title(- conversation_id, user_id, file_store, settings, llm_registry- )-- # Verify the result- assert title == 'Python Data Analysis Script'-- # Verify EventStore was created with the correct parameters- mock_event_store_cls.assert_called_once_with(- conversation_id, file_store, user_id- )-- # Verify LLM registry was called with appropriate parameters- llm_registry.request_extraneous_completion.assert_called_once()---async def test_auto_generate_title_fallback():- """Test auto-generating a title with fallback to truncation when LLM fails."""- # Mock dependencies- file_store = InMemoryFileStore()- llm_registry = MagicMock(spec=LLMRegistry)-- # Create test conversation with a user message- conversation_id = 'test-conversation'- user_id = 'test-user'-- # Create a mock event with a long message- long_message = 'This is a very long message that should be truncated when used as a title because it exceeds the maximum length allowed for titles'- user_message = MessageAction(content=long_message)- user_message._source = EventSource.USER- user_message._id = 1- user_message._timestamp = datetime.now(timezone.utc).isoformat()-- # Mock the EventStore class- with patch(- 'openhands.utils.conversation_summary.EventStore'- ) as mock_event_store_cls:- # Configure the mock event stream to return our test message- mock_event_store = MagicMock(spec=EventStore)- mock_event_store.search_events.return_value = [user_message]- mock_event_store_cls.return_value = mock_event_store-- # Mock the LLM registry to raise an exception- llm_registry.request_extraneous_completion.side_effect = Exception('Test error')-- # Create test settings with LLM config- settings = Settings(- llm_model='test-model',- llm_api_key='test-key',- llm_base_url='test-url',- )-- # Call the auto_generate_title function directly- title = await auto_generate_title(- conversation_id, user_id, file_store, settings, llm_registry- )-- # Verify the result is a truncated version of the message- assert title == 'This is a very long message th...'- assert len(title) <= 35-- # Verify EventStore was created with the correct parameters- mock_event_store_cls.assert_called_once_with(- conversation_id, file_store, user_id- )---async def test_auto_generate_title_no_messages():- """Test auto-generating a title when there are no user messages."""- # Mock dependencies- file_store = InMemoryFileStore()- llm_registry = MagicMock(spec=LLMRegistry)-- # Create test conversation with no messages- conversation_id = 'test-conversation'- user_id = 'test-user'-- # Mock the EventStore class- with patch(- 'openhands.utils.conversation_summary.EventStore'- ) as mock_event_store_cls:- # Configure the mock event store to return no events- mock_event_store = MagicMock(spec=EventStore)- mock_event_store.search_events.return_value = []- mock_event_store_cls.return_value = mock_event_store-- # Create test settings- settings = Settings(- llm_model='test-model',- llm_api_key='test-key',- llm_base_url='test-url',- )-- # Call the auto_generate_title function directly- title = await auto_generate_title(- conversation_id, user_id, file_store, settings, llm_registry- )-- # Verify the result is empty- assert title == ''-- # Verify EventStore was created with the correct parameters- mock_event_store_cls.assert_called_once_with(- conversation_id, file_store, user_id- )