Skip to content

Commit 04161ff

Browse files
refactor: apply Gemini review round 2 feedback
1 parent 0003f12 commit 04161ff

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/google/adk/tools/function_tool.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _preprocess_args(
142142
if inspect.isclass(target_type) and issubclass(
143143
target_type, pydantic.BaseModel
144144
):
145-
if args[param_name] is None:
145+
if args[param_name] is None and is_optional:
146146
continue
147147
if not isinstance(args[param_name], target_type):
148148
try:
@@ -173,10 +173,16 @@ def _preprocess_args(
173173
f"Parameter '{param_name}': expected type '{target_type}',"
174174
f' validation error: {e}'
175175
)
176-
except (TypeError, NameError):
176+
except (TypeError, NameError) as e:
177177
# TypeAdapter could not handle this annotation (e.g. a forward
178-
# reference string). Skip validation silently.
179-
pass
178+
# reference string). Skip validation but log a warning.
179+
logger.warning(
180+
"Skipping validation for parameter '%s' due to unhandled"
181+
" annotation type '%s': %s",
182+
param_name,
183+
target_type,
184+
e,
185+
)
180186

181187
return converted_args, validation_errors
182188

tests/unittests/tools/test_function_tool_arg_validation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ def test_multiple_param_errors(self):
155155
args, errors = tool._preprocess_args(
156156
{"name": 123, "count": "not_a_number", "flag": "not_a_bool"}
157157
)
158-
# name: int->str coercion might fail in strict, but lax mode might
159-
# handle it. count: "not_a_number"->int will fail. flag: depends on
160-
# pydantic behavior.
158+
# All three fail: pydantic rejects int->str, "not_a_number"->int,
159+
# and "not_a_bool"->bool.
160+
assert len(errors) == 3
161+
assert any("name" in e for e in errors)
161162
assert any("count" in e for e in errors)
163+
assert any("flag" in e for e in errors)
162164

163165

164166
# --- run_async integration tests ---

0 commit comments

Comments
 (0)