Improve Function Call argument validation and typing (#2881)

Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
This commit is contained in:
Tommaso Sciortino
2025-07-07 23:48:44 -07:00
committed by GitHub
parent 137ffec3f6
commit 4dab31f1c8
22 changed files with 239 additions and 246 deletions

View File

@@ -6,6 +6,7 @@
import { GroundingMetadata } from '@google/genai';
import { BaseTool, ToolResult } from './tools.js';
import { Type } from '@google/genai';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { getErrorMessage } from '../utils/errors.js';
@@ -69,10 +70,10 @@ export class WebSearchTool extends BaseTool<
'GoogleSearch',
'Performs a web search using Google Search (via the Gemini API) and returns the results. This tool is useful for finding information on the internet based on a query.',
{
type: 'object',
type: Type.OBJECT,
properties: {
query: {
type: 'string',
type: Type.STRING,
description: 'The search query to find information on the web.',
},
},
@@ -86,16 +87,12 @@ export class WebSearchTool extends BaseTool<
* @param params The parameters to validate
* @returns An error message string if validation fails, null if valid
*/
validateToolParams(params: WebSearchToolParams): string | null {
if (
this.schema.parameters &&
!SchemaValidator.validate(
this.schema.parameters as Record<string, unknown>,
params,
)
) {
return "Parameters failed schema validation. Ensure 'query' is a string.";
validateParams(params: WebSearchToolParams): string | null {
const errors = SchemaValidator.validate(this.schema.parameters, params);
if (errors) {
return errors;
}
if (!params.query || params.query.trim() === '') {
return "The 'query' parameter cannot be empty.";
}