Skip to content

Commit d752baf

Browse files
committed
fix: support functools.partial objects as tools in t_tool()
Fixes #907 inspect.isfunction() returns False for functools.partial objects, so passing a partial to tools= would silently fall through to the else branch and return the partial object unchanged. The API then received an invalid tool object and rejected it with 400 INVALID_ARGUMENT. Add functools.partial to the callable check so partial functions get wrapped in a FunctionDeclaration the same way regular functions do.
1 parent 2afdeff commit d752baf

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

google/genai/_transformers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import base64
1919
from collections.abc import Iterable, Mapping
2020
from enum import Enum, EnumMeta
21+
from functools import partial as _partial
2122
import inspect
2223
import io
2324
import logging
@@ -959,7 +960,7 @@ def t_tool(
959960
) -> Optional[Union[types.Tool, Any]]:
960961
if not origin:
961962
return None
962-
if inspect.isfunction(origin) or inspect.ismethod(origin):
963+
if inspect.isfunction(origin) or inspect.ismethod(origin) or isinstance(origin, _partial):
963964
return types.Tool(
964965
function_declarations=[
965966
types.FunctionDeclaration.from_callable(

0 commit comments

Comments
 (0)