From 798b557e061b5b7554e2a26ef92f1ec88e9518e9 Mon Sep 17 00:00:00 2001 From: Yusuke Oda Date: Wed, 2 Sep 2026 16:41:13 +0900 Subject: [PATCH] [Frontend] Add site-packages support for reasoning/tool parser plugins (#45241) Signed-off-by: odashi Co-authored-by: Chauncey --- tests/utils_/test_import_utils.py | 36 ++++++++++++++++++++++- vllm/reasoning/abs_reasoning_parsers.py | 18 ++---------- vllm/tool_parsers/abstract_tool_parser.py | 14 ++------- vllm/utils/import_utils.py | 23 ++++++++++++++- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/tests/utils_/test_import_utils.py b/tests/utils_/test_import_utils.py index 464f209f0f..61e471a181 100644 --- a/tests/utils_/test_import_utils.py +++ b/tests/utils_/test_import_utils.py @@ -1,10 +1,11 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project +import sys from unittest.mock import MagicMock, patch import pytest -from vllm.utils.import_utils import PlaceholderModule, _has_module +from vllm.utils.import_utils import PlaceholderModule, _has_module, import_plugin def _raises_module_not_found(): @@ -101,3 +102,36 @@ class TestHasModule: result = _has_module("json") # should hit cache mock_spec.assert_not_called() assert result is True + + +class TestImportPlugin: + def test_importing_from_site_packages(self): + import json + + result = import_plugin("json") + assert result is json + + def test_importing_from_file(self, tmp_path): + plugin_file = tmp_path / "my_test_plugin.py" + plugin_file.write_text("VALUE = 42\n") + + try: + result = import_plugin(str(plugin_file)) + assert result is not None + assert result.VALUE == 42 + finally: + sys.modules.pop("my_test_plugin", None) + + def test_returns_none_when_both_attempts_fail(self): + with ( + patch( + "vllm.utils.import_utils.import_from_path", + side_effect=FileNotFoundError("no such file"), + ), + patch( + "vllm.utils.import_utils.importlib.import_module", + side_effect=ModuleNotFoundError("no such module"), + ), + ): + result = import_plugin("nonexistent_plugin_xyz") + assert result is None diff --git a/vllm/reasoning/abs_reasoning_parsers.py b/vllm/reasoning/abs_reasoning_parsers.py index 2a0d4231f9..6fb694545a 100644 --- a/vllm/reasoning/abs_reasoning_parsers.py +++ b/vllm/reasoning/abs_reasoning_parsers.py @@ -2,7 +2,6 @@ # SPDX-FileCopyrightText: Copyright contributors to the vLLM project import importlib -import os from abc import abstractmethod from collections.abc import Callable, Iterable, Sequence from functools import cached_property @@ -11,7 +10,7 @@ from typing import TYPE_CHECKING, cast from vllm.entrypoints.mcp.tool_server import ToolServer from vllm.logger import init_logger from vllm.utils.collection_utils import is_list_of -from vllm.utils.import_utils import import_from_path +from vllm.utils.import_utils import import_plugin if TYPE_CHECKING: from vllm.config import ModelConfig @@ -359,16 +358,5 @@ class ReasoningParserManager: @classmethod def import_reasoning_parser(cls, plugin_path: str) -> None: - """ - Import a user-defined reasoning parser by the path - of the reasoning parser define file. - """ - module_name = os.path.splitext(os.path.basename(plugin_path))[0] - - try: - import_from_path(module_name, plugin_path) - except Exception: - logger.exception( - "Failed to load module '%s' from %s.", module_name, plugin_path - ) - return + """Import a user-defined reasoning parser.""" + import_plugin(plugin_path) diff --git a/vllm/tool_parsers/abstract_tool_parser.py b/vllm/tool_parsers/abstract_tool_parser.py index 42522d4350..3ee35365e4 100644 --- a/vllm/tool_parsers/abstract_tool_parser.py +++ b/vllm/tool_parsers/abstract_tool_parser.py @@ -3,7 +3,6 @@ import importlib import json -import os from collections.abc import Callable, Sequence from functools import cached_property from typing import Any @@ -33,7 +32,7 @@ from vllm.sampling_params import ( from vllm.tokenizers import TokenizerLike from vllm.tool_parsers.utils import Tool, get_json_schema_from_tools from vllm.utils.collection_utils import is_list_of -from vllm.utils.import_utils import import_from_path +from vllm.utils.import_utils import import_plugin __all__ = ["Tool"] @@ -367,12 +366,5 @@ class ToolParserManager: @classmethod def import_tool_parser(cls, plugin_path: str) -> None: - """Import a user-defined parser file from arbitrary path.""" - - module_name = os.path.splitext(os.path.basename(plugin_path))[0] - try: - import_from_path(module_name, plugin_path) - except Exception: - logger.exception( - "Failed to load module '%s' from %s.", module_name, plugin_path - ) + """Import a user-defined tool parser.""" + import_plugin(plugin_path) diff --git a/vllm/utils/import_utils.py b/vllm/utils/import_utils.py index 03e301317f..20c510edc0 100644 --- a/vllm/utils/import_utils.py +++ b/vllm/utils/import_utils.py @@ -82,7 +82,28 @@ def import_triton_kernels(): ) -def import_from_path(module_name: str, file_path: str | os.PathLike): +def import_plugin(plugin_path: str) -> ModuleType | None: + """ + Import a user-defined plugin. + + Plugin can be either: + * a module in site-packages + * a Python file specified by its path + """ + try: + return importlib.import_module(plugin_path) + except ModuleNotFoundError: + module_name = os.path.splitext(os.path.basename(plugin_path))[0] + try: + return import_from_path(module_name, plugin_path) + except Exception: + logger.exception( + "Failed to load module '%s' from %s.", module_name, plugin_path + ) + return None + + +def import_from_path(module_name: str, file_path: str | os.PathLike) -> ModuleType: """ Import a Python file according to its file path.