Skip to content

Commit 1890557

Browse files
ItsMactocopybara-github
authored andcommitted
fix: return empty list from _get_required_fields when no properties
Merge google#6377 Fixes google#5920 PiperOrigin-RevId: 951051604
1 parent 6f6106f commit 1890557

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/google/adk/tools/_function_parameter_parse_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def _parse_schema_from_parameter(
548548

549549
def _get_required_fields(schema: types.Schema) -> list[str]:
550550
if not schema.properties:
551-
return
551+
return []
552552
return [
553553
field_name
554554
for field_name, field_schema in schema.properties.items()

tests/unittests/tools/test_build_function_declaration.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from google.adk.features import FeatureName
1818
from google.adk.features._feature_registry import temporary_feature_override
1919
from google.adk.tools import _automatic_function_calling_util
20+
from google.adk.tools import _function_parameter_parse_util
2021
from google.adk.tools.tool_context import ToolContext
2122
from google.adk.utils.variant_utils import GoogleLLMVariant
2223
from google.genai import types
@@ -198,6 +199,44 @@ def simple_function(input_str: str, tool_context: ToolContext) -> str:
198199
assert function_decl.parameters.properties['input_str'].type == 'STRING'
199200
assert 'tool_context' not in function_decl.parameters.properties
200201

202+
def test_get_required_fields_no_properties_returns_empty_list(self):
203+
# A schema without properties must yield [] rather than None for any
204+
# caller that relies on the declared list[str] return.
205+
assert (
206+
_function_parameter_parse_util._get_required_fields(
207+
types.Schema(type='OBJECT')
208+
)
209+
== []
210+
)
211+
assert (
212+
_function_parameter_parse_util._get_required_fields(
213+
types.Schema(type='OBJECT', properties={})
214+
)
215+
== []
216+
)
217+
218+
def test_build_declaration_includes_required_parameters(self):
219+
def simple_function(input_str: str) -> str:
220+
return {'result': input_str}
221+
222+
function_decl = _automatic_function_calling_util.build_function_declaration(
223+
func=simple_function
224+
)
225+
226+
assert function_decl.parameters.required == ['input_str']
227+
228+
def test_all_parameters_ignored_results_in_no_parameters_schema(self):
229+
def only_context(tool_context: ToolContext) -> str:
230+
return ''
231+
232+
function_decl = _automatic_function_calling_util.build_function_declaration(
233+
func=only_context, ignore_params=['tool_context']
234+
)
235+
236+
# No parameters remain after ignoring tool_context, so the declaration
237+
# carries no parameter schema instead of one with required=None.
238+
assert function_decl.parameters is None
239+
201240
def test_basemodel(self):
202241
class SimpleFunction(BaseModel):
203242
input_str: str

0 commit comments

Comments
 (0)