From d3aabf9a36ef3559ac40f89b14da83215ff7059e Mon Sep 17 00:00:00 2001 From: Mac Howe Date: Sat, 11 Jul 2026 14:35:28 -0400 Subject: [PATCH] fix: Return empty list instead of None from _get_required_fields for schemas without properties Fixes https://github.com/google/adk-python/issues/5920 --- .../tools/_function_parameter_parse_util.py | 2 +- .../tools/test_build_function_declaration.py | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/google/adk/tools/_function_parameter_parse_util.py b/src/google/adk/tools/_function_parameter_parse_util.py index 1558076f0fb..99f2d08c9f6 100644 --- a/src/google/adk/tools/_function_parameter_parse_util.py +++ b/src/google/adk/tools/_function_parameter_parse_util.py @@ -548,7 +548,7 @@ def _parse_schema_from_parameter( def _get_required_fields(schema: types.Schema) -> list[str]: if not schema.properties: - return + return [] return [ field_name for field_name, field_schema in schema.properties.items() diff --git a/tests/unittests/tools/test_build_function_declaration.py b/tests/unittests/tools/test_build_function_declaration.py index 04d7ed7f60d..e6a311f733e 100644 --- a/tests/unittests/tools/test_build_function_declaration.py +++ b/tests/unittests/tools/test_build_function_declaration.py @@ -17,6 +17,7 @@ from google.adk.features import FeatureName from google.adk.features._feature_registry import temporary_feature_override from google.adk.tools import _automatic_function_calling_util +from google.adk.tools import _function_parameter_parse_util from google.adk.tools.tool_context import ToolContext from google.adk.utils.variant_utils import GoogleLLMVariant from google.genai import types @@ -198,6 +199,46 @@ def simple_function(input_str: str, tool_context: ToolContext) -> str: assert function_decl.parameters.properties['input_str'].type == 'STRING' assert 'tool_context' not in function_decl.parameters.properties + def test_get_required_fields_no_properties_returns_empty_list(self): + # Regression for https://github.com/google/adk-python/issues/5920: the + # declared return type is list[str], so a schema without properties must + # yield [] rather than None for any caller that relies on the declared + # list[str] return. + assert ( + _function_parameter_parse_util._get_required_fields( + types.Schema(type='OBJECT') + ) + == [] + ) + assert ( + _function_parameter_parse_util._get_required_fields( + types.Schema(type='OBJECT', properties={}) + ) + == [] + ) + + def test_declaration_required_fields_is_list_not_none(self): + def simple_function(input_str: str) -> str: + return {'result': input_str} + + function_decl = _automatic_function_calling_util.build_function_declaration( + func=simple_function + ) + + assert function_decl.parameters.required == ['input_str'] + + def test_toolcontext_only_builds_without_none_required(self): + def only_context(tool_context: ToolContext) -> str: + return '' + + function_decl = _automatic_function_calling_util.build_function_declaration( + func=only_context, ignore_params=['tool_context'] + ) + + # No parameters remain after ignoring tool_context, so the declaration + # carries no parameter schema instead of one with required=None. + assert function_decl.parameters is None + def test_basemodel(self): class SimpleFunction(BaseModel): input_str: str