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
7 changes: 6 additions & 1 deletion src/google/adk/tools/_automatic_function_calling_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _map_pydantic_type_to_property_schema(
property_schema['type'] = _py_type_2_schema_type.get(
property_schema['type'], 'TYPE_UNSPECIFIED'
)
if property_schema['type'] == 'ARRAY':
if property_schema['type'] == 'ARRAY' and 'items' in property_schema:
_map_pydantic_type_to_property_schema(property_schema['items'])
for type_ in property_schema.get('anyOf', []):
if 'type' in type_:
Expand Down Expand Up @@ -335,6 +335,11 @@ def build_function_declaration_util(
func: Callable[..., Any],
before_param_pydantic_schema: _JsonSchema,
) -> types.FunctionDeclaration:
before_param_pydantic_schema = (
_function_parameter_parse_util._normalize_tuple_schema_for_genai_schema(
before_param_pydantic_schema
)
)
_map_pydantic_type_to_schema_type(before_param_pydantic_schema)
properties = before_param_pydantic_schema.get('properties', {})
required = before_param_pydantic_schema.get('required', [])
Expand Down
68 changes: 68 additions & 0 deletions tests/unittests/tools/test_build_function_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# TODO: crewai requires python 3.10 as minimum
# from crewai_tools import FileReadTool
from pydantic import BaseModel
from pydantic import create_model
from pydantic import Field
import pytest

Expand Down Expand Up @@ -1317,3 +1318,70 @@ def tool_func(count) -> str:
schema = decl.parameters.properties['count']
assert schema.type == 'INTEGER'
assert schema.any_of is None


@pytest.mark.parametrize('adapter', ['langchain', 'crewai'])
@pytest.mark.parametrize(
('annotation', 'nested', 'item_types'),
[
(tuple[float, float], False, ['NUMBER']),
(tuple[str, int], False, ['STRING', 'INTEGER']),
(tuple[float, float] | None, False, ['NUMBER']),
(list[tuple[float, float]], True, ['NUMBER']),
(tuple[()], False, []),
(list[tuple[()]], True, []),
],
ids=[
'fixed',
'heterogeneous',
'nullable',
'nested',
'empty',
'nested-empty',
],
)
def test_adapter_declares_tuple_parameters(
adapter: str,
annotation: Any,
nested: bool,
item_types: list[str],
):
"""Adapters declare tuple arguments using supported array schemas."""
args_schema = create_model('TupleArgs', coordinates=(annotation, ...))

def tool_func(coordinates):
return coordinates

util = _automatic_function_calling_util
schema = args_schema.model_json_schema()
if adapter == 'langchain':
declaration = util.build_function_declaration_for_langchain(
False,
'coordinates',
'Read coordinates.',
tool_func,
schema['properties'],
)
else:
declaration = util.build_function_declaration_for_params_for_crewai(
False,
'coordinates',
'Read coordinates.',
tool_func,
schema,
)

parameter = declaration.parameters.properties['coordinates']
assert parameter.type == 'ARRAY'
tuple_schema = parameter.items if nested else parameter
assert tuple_schema.type == 'ARRAY'
expected_length = 2 if item_types else 0
assert tuple_schema.min_items == expected_length
assert tuple_schema.max_items == expected_length
if not item_types:
assert tuple_schema.items is None
return
if len(item_types) == 1:
assert tuple_schema.items.type == item_types[0]
else:
assert [item.type for item in tuple_schema.items.any_of] == item_types