Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/google/src/google-prepare-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,19 @@ it('should add warnings for google maps on unsupported models', () => {
]
`);
});

it('should throw error for invalid tool name', () => {
expect(() =>
prepareTools({
tools: [
{
type: 'function',
name: '123invalid',
description: 'Invalid name',
inputSchema: { type: 'object', properties: {} },
},
],
modelId: 'gemini-2.5-flash',
}),
).toThrowError(/Invalid tool name/);
});
42 changes: 24 additions & 18 deletions packages/google/src/google-prepare-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ export function prepareTools({
modelId: GoogleGenerativeAIModelId;
}): {
tools:
| Array<
| {
functionDeclarations: Array<{
name: string;
description: string;
parameters: unknown;
}>;
}
| Record<string, any>
>
| undefined;
toolConfig:
| undefined
| Array<
| {
functionCallingConfig: {
mode: 'AUTO' | 'NONE' | 'ANY';
allowedFunctionNames?: string[];
};
};
functionDeclarations: Array<{
name: string;
description: string;
parameters: unknown;
}>;
}
| Record<string, any>
>
| undefined;
toolConfig:
| undefined
| {
functionCallingConfig: {
mode: 'AUTO' | 'NONE' | 'ANY';
allowedFunctionNames?: string[];
};
};
toolWarnings: SharedV3Warning[];
} {
// when the tools array is empty, change it to undefined to prevent errors:
Expand Down Expand Up @@ -199,6 +199,12 @@ export function prepareTools({
for (const tool of tools) {
switch (tool.type) {
case 'function':
if (!/^[a-zA-Z_][a-zA-Z0-9_.:-]*$/.test(tool.name)) {
throw new Error(
`Invalid tool name: ${tool.name}. Tool names must start with a letter or underscore and contain only alphanumeric characters, underscores, dots, colons, or dashes.`,
);
}

functionDeclarations.push({
name: tool.name,
description: tool.description ?? '',
Expand Down
Loading