[Frontend] Add site-packages support for reasoning/tool parser plugins (#45241)

Signed-off-by: odashi <yusuke.oda@predicate.jp>
Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
Yusuke Oda
2026-09-02 07:41:13 +00:00
committed by GitHub
co-authored by Chauncey
parent f4e6136146
commit 798b557e06
4 changed files with 63 additions and 28 deletions
+35 -1
View File
@@ -1,10 +1,11 @@
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import sys
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
import pytest 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(): def _raises_module_not_found():
@@ -101,3 +102,36 @@ class TestHasModule:
result = _has_module("json") # should hit cache result = _has_module("json") # should hit cache
mock_spec.assert_not_called() mock_spec.assert_not_called()
assert result is True 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
+3 -15
View File
@@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import importlib import importlib
import os
from abc import abstractmethod from abc import abstractmethod
from collections.abc import Callable, Iterable, Sequence from collections.abc import Callable, Iterable, Sequence
from functools import cached_property 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.entrypoints.mcp.tool_server import ToolServer
from vllm.logger import init_logger from vllm.logger import init_logger
from vllm.utils.collection_utils import is_list_of 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: if TYPE_CHECKING:
from vllm.config import ModelConfig from vllm.config import ModelConfig
@@ -359,16 +358,5 @@ class ReasoningParserManager:
@classmethod @classmethod
def import_reasoning_parser(cls, plugin_path: str) -> None: def import_reasoning_parser(cls, plugin_path: str) -> None:
""" """Import a user-defined reasoning parser."""
Import a user-defined reasoning parser by the path import_plugin(plugin_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
+3 -11
View File
@@ -3,7 +3,6 @@
import importlib import importlib
import json import json
import os
from collections.abc import Callable, Sequence from collections.abc import Callable, Sequence
from functools import cached_property from functools import cached_property
from typing import Any from typing import Any
@@ -33,7 +32,7 @@ from vllm.sampling_params import (
from vllm.tokenizers import TokenizerLike from vllm.tokenizers import TokenizerLike
from vllm.tool_parsers.utils import Tool, get_json_schema_from_tools from vllm.tool_parsers.utils import Tool, get_json_schema_from_tools
from vllm.utils.collection_utils import is_list_of 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"] __all__ = ["Tool"]
@@ -367,12 +366,5 @@ class ToolParserManager:
@classmethod @classmethod
def import_tool_parser(cls, plugin_path: str) -> None: def import_tool_parser(cls, plugin_path: str) -> None:
"""Import a user-defined parser file from arbitrary path.""" """Import a user-defined tool parser."""
import_plugin(plugin_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
)
+22 -1
View File
@@ -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. Import a Python file according to its file path.