Skip to content

Commit 0003f12

Browse files
refactor: narrow except clause and extract validation error helper
1 parent 4b83906 commit 0003f12

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

src/google/adk/flows/llm_flows/functions.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,7 @@ def run_sync_tool():
149149
if isinstance(tool, FunctionTool):
150150
args_to_call, validation_errors = tool._preprocess_args(args)
151151
if validation_errors:
152-
validation_errors_str = '\n'.join(validation_errors)
153-
return {
154-
'error': (
155-
f'Invoking `{tool.name}()` failed due to argument'
156-
f' validation errors:\n{validation_errors_str}\nYou could'
157-
' retry calling this tool with corrected argument types.'
158-
)
159-
}
152+
return tool._build_validation_error_response(validation_errors)
160153
signature = inspect.signature(tool.func)
161154
valid_params = {param for param in signature.parameters}
162155
if tool._context_param_name in valid_params:

src/google/adk/tools/crewai_tool.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ async def run_async(
7878
args_to_call, validation_errors = self._preprocess_args(args)
7979

8080
if validation_errors:
81-
validation_errors_str = '\n'.join(validation_errors)
82-
error_str = f"""Invoking `{self.name}()` failed due to argument validation errors:
83-
{validation_errors_str}
84-
You could retry calling this tool with corrected argument types."""
85-
return {'error': error_str}
81+
return self._build_validation_error_response(validation_errors)
8682

8783
signature = inspect.signature(self.func)
8884
valid_params = {param for param in signature.parameters}

src/google/adk/tools/function_tool.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,26 @@ def _preprocess_args(
173173
f"Parameter '{param_name}': expected type '{target_type}',"
174174
f' validation error: {e}'
175175
)
176-
except Exception:
176+
except (TypeError, NameError):
177177
# TypeAdapter could not handle this annotation (e.g. a forward
178178
# reference string). Skip validation silently.
179179
pass
180180

181181
return converted_args, validation_errors
182182

183+
def _build_validation_error_response(
184+
self, validation_errors: list[str]
185+
) -> dict[str, str]:
186+
"""Formats validation errors into an error dict for the LLM."""
187+
validation_errors_str = '\n'.join(validation_errors)
188+
return {
189+
'error': (
190+
f'Invoking `{self.name}()` failed due to argument validation'
191+
f' errors:\n{validation_errors_str}\nYou could retry calling'
192+
' this tool with corrected argument types.'
193+
)
194+
}
195+
183196
@override
184197
async def run_async(
185198
self, *, args: dict[str, Any], tool_context: ToolContext
@@ -190,11 +203,7 @@ async def run_async(
190203
args_to_call, validation_errors = self._preprocess_args(args)
191204

192205
if validation_errors:
193-
validation_errors_str = '\n'.join(validation_errors)
194-
error_str = f"""Invoking `{self.name}()` failed due to argument validation errors:
195-
{validation_errors_str}
196-
You could retry calling this tool with corrected argument types."""
197-
return {'error': error_str}
206+
return self._build_validation_error_response(validation_errors)
198207

199208
signature = inspect.signature(self.func)
200209
valid_params = {param for param in signature.parameters}

0 commit comments

Comments
 (0)