From 35804f7c2f437f26e0ed7947048d1cd80f2f70ce Mon Sep 17 00:00:00 2001 From: betacatsling <113584199+betacatsling@users.noreply.github.com> Date: Mon, 14 Sep 2026 17:59:14 +0800 Subject: [PATCH] fix: exclude *args and **kwargs from converted tool parameters --- ollama/_utils.py | 2 +- tests/test_utils.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ollama/_utils.py b/ollama/_utils.py index 15f1cc0c..d0449dc7 100644 --- a/ollama/_utils.py +++ b/ollama/_utils.py @@ -60,7 +60,7 @@ def convert_function_to_tool(func: Callable) -> Tool: func.__name__, (pydantic.BaseModel,), { - '__annotations__': {k: v.annotation if v.annotation != inspect._empty else str for k, v in inspect.signature(func).parameters.items()}, + '__annotations__': {k: v.annotation if v.annotation != inspect._empty else str for k, v in inspect.signature(func).parameters.items() if v.kind not in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD)}, '__signature__': inspect.signature(func), '__doc__': parsed_docstring[doc_string_hash], }, diff --git a/tests/test_utils.py b/tests/test_utils.py index cb9e0d4f..295fb08c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -230,6 +230,20 @@ def no_types(a, b): assert tool['function']['parameters']['properties']['b']['type'] == 'string' +def test_function_with_varargs(): + def func_with_varargs(x: int, *args, **kwargs) -> int: + """ + A function with varargs. + Args: + x: The first number + """ + return x + + tool = convert_function_to_tool(func_with_varargs).model_dump() + assert set(tool['function']['parameters']['properties']) == {'x'} + assert tool['function']['parameters']['required'] == ['x'] + + def test_function_with_parentheses(): def func_with_parentheses(a: int, b: int) -> int: """