Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/google/adk/tools/_automatic_function_calling_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from ..features import FeatureName
from ..features import is_feature_enabled
from ..utils.variant_utils import GoogleLLMVariant
from ._gemini_schema_util import _sanitize_schema_formats_for_gemini

_py_type_2_schema_type = {
'str': types.Type.STRING,
Expand Down Expand Up @@ -366,7 +367,7 @@ def from_function_with_options(
)

parameters_json_schema[name] = types.Schema.model_validate(
json_schema_dict
_sanitize_schema_formats_for_gemini(json_schema_dict)
)
if param.default is not inspect.Parameter.empty:
if param.default is not None:
Expand Down
26 changes: 26 additions & 0 deletions tests/unittests/tools/test_from_function_with_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,29 @@ def complex_tool(
),
},
)


def test_sanitized_in_json_schema_fallback():
"""Test schema is sanitzed for complex union type."""

def complex_tool(
query: str,
mode: str = 'default',
tags: dict[str, str] | None = None,
) -> str:
return query

declaration = _automatic_function_calling_util.from_function_with_options(
complex_tool, GoogleLLMVariant.GEMINI_API
)

assert declaration.parameters.properties['tags'] == types.Schema(
any_of=[
types.Schema(
# should not contain `additional_properties={'type': 'string'} from pydantic
type=types.Type.OBJECT,
),
types.Schema(type=types.Type.NULL),
],
nullable=True,
)