8000 fix: handles function tool parsing corner case where type hints are s… · nx90/adk-python@a8a2074 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8a2074

Browse files
wuliang229copybara-github
authored andcommitted
fix: handles function tool parsing corner case where type hints are stored as strings.
Previously if you add `from __future__ import annotations` in your code, the parsing code would fail because the type hints will be a string instead of the class itself (e.g. input: 'str' instead of input: str). Also added "_" to the util file name. PiperOrigin-RevId: 764817339
1 parent d587270 commit a8a2074

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/google/adk/tools/_automatic_function_calling_util.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import inspect
1818
from types import FunctionType
19+
import typing
1920
from typing import Any
2021
from typing import Callable
2122
from typing import Dict
@@ -29,7 +30,7 @@
2930
from pydantic import create_model
3031
from pydantic import fields as pydantic_fields
3132

32-
from . import function_parameter_parse_util
33+
from . import _function_parameter_parse_util
3334

3435
_py_type_2_schema_type = {
3536
'str': types.Type.STRING,
@@ -306,7 +307,11 @@ def from_function_with_options(
306307
inspect.Parameter.KEYWORD_ONLY,
307308
inspect.Parameter.POSITIONAL_ONLY,
308309
):
309-
schema = function_parameter_parse_util._parse_schema_from_parameter(
310+
# This snippet catches the case when type hints are stored as strings
311+
if isinstance(param.annotation, str):
312+
param = param.replace(annotation=typing.get_type_hints(func)[name])
313+
314+
schema = _function_parameter_parse_util._parse_schema_from_parameter(
310315
variant, param, func.__name__
311316
)
312317
parameters_properties[name] = schema
@@ -320,7 +325,7 @@ def from_function_with_options(
320325
properties=parameters_properties,
321326
)
322327
declaration.parameters.required = (
323-
function_parameter_parse_util._get_required_fields(
328+
_function_parameter_parse_util._get_required_fields(
324329
declaration.parameters
325330
)
326331
)
@@ -331,14 +336,19 @@ def from_function_with_options(
331336
if return_annotation is inspect._empty:
332337
return declaration
333338

339+
return_value = inspect.Parameter(
340+
'return_value',
341+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
342+
annotation=return_annotation,
343+
)
344+
# This snippet catches the case when type hints are stored as strings
345+
if isinstance(return_value.annotation, str):
346+
return_value = return_value.replace(annotation=typing.get_type_hints(func)["return"])
347+
334348
declaration.response = (
335-
function_parameter_parse_util._parse_schema_from_parameter(
349+
_function_parameter_parse_util._parse_schema_from_parameter(
336350
variant,
337-
inspect.Parameter(
338-
'return_value',
339-
inspect.Parameter.POSITIONAL_OR_KEYWORD,
340-
annotation=return_annotation,
341-
),
351+
return_value,
342352
func.__name__,
343353
)
344354
)

src/google/adk/tools/function_parameter_parse_util.py renamed to src/google/adk/tools/_function_parameter_parse_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414
#
1515

16+
from __future__ import annotations
17+
1618
import inspect
1719
import logging
1820
import types as typing_types

0 commit comments

Comments
 (0)
0