Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: deduplicate tool function declarations to fix Vertex AI 400 error (#12604)

YuTengjinge733397f5daf
diff --git a/packages/model-runtime/src/core/contextBuilders/google.test.ts b/packages/model-runtime/src/core/contextBuilders/google.test.ts
index 0e67ac118a9..08a419b0a4f 100644
--- a/packages/model-runtime/src/core/contextBuilders/google.test.ts
+++ b/packages/model-runtime/src/core/contextBuilders/google.test.ts
@@ -24,6 +24,14 @@ vi.mock('../../utils/imageToBase64', () => ({
}));
describe('google contextBuilders', () => {
+ describe('GEMINI_MAGIC_THOUGHT_SIGNATURE', () => {
+ it('should use skip_thought_signature_validator for Vertex AI compatibility', () => {
+ // Vertex AI only accepts `skip_thought_signature_validator`, not `context_engineering_is_the_way_to_go`
+ // see: https://github.com/pydantic/pydantic-ai/issues/3881
+ expect(GEMINI_MAGIC_THOUGHT_SIGNATURE).toBe('skip_thought_signature_validator');
+ });
+ });
+
describe('buildGooglePart', () => {
it('should handle text type messages', async () => {
const content: UserMessageContentPart = {
@@ -1448,5 +1456,73 @@ describe('google contextBuilders', () => {
expect(googleTools![0].functionDeclarations![0].name).toBe('get_weather');
expect(googleTools![0].functionDeclarations![1].name).toBe('get_time');
});
+
+ it('should deduplicate tools with the same function name', () => {
+ const tools: ChatCompletionTool[] = [
+ {
+ function: {
+ description: 'Search the web',
+ name: 'lobe-web-browsing____search____builtin',
+ parameters: {
+ properties: { query: { type: 'string' } },
+ required: ['query'],
+ type: 'object',
+ },
+ },
+ type: 'function',
+ },
+ {
+ function: {
+ description: 'Get weather',
+ name: 'get_weather',
+ parameters: {
+ properties: { city: { type: 'string' } },
+ required: ['city'],
+ type: 'object',
+ },
+ },
+ type: 'function',
+ },
+ {
+ function: {
+ description: 'Search the web (duplicate)',
+ name: 'lobe-web-browsing____search____builtin',
+ parameters: {
+ properties: { query: { type: 'string' } },
+ required: ['query'],
+ type: 'object',
+ },
+ },
+ type: 'function',
+ },
+ ];
+
+ const googleTools = buildGoogleTools(tools);
+
+ expect(googleTools).toHaveLength(1);
+ expect(googleTools![0].functionDeclarations).toHaveLength(2);
+ expect(googleTools![0].functionDeclarations![0].name).toBe(
+ 'lobe-web-browsing____search____builtin',
+ );
+ expect(googleTools![0].functionDeclarations![0].description).toBe('Search the web');
+ expect(googleTools![0].functionDeclarations![1].name).toBe('get_weather');
+ });
+
+ it('should keep all tools when there are no duplicates', () => {
+ const tools: ChatCompletionTool[] = [
+ {
+ function: { description: 'Tool A', name: 'tool_a', parameters: { type: 'object' } },
+ type: 'function',
+ },
+ {
+ function: { description: 'Tool B', name: 'tool_b', parameters: { type: 'object' } },
+ type: 'function',
+ },
+ ];
+
+ const googleTools = buildGoogleTools(tools);
+
+ expect(googleTools![0].functionDeclarations).toHaveLength(2);
+ });
});
});
diff --git a/packages/model-runtime/src/core/contextBuilders/google.ts b/packages/model-runtime/src/core/contextBuilders/google.ts
index 794d961c803..109f1e59ba5 100644
--- a/packages/model-runtime/src/core/contextBuilders/google.ts
+++ b/packages/model-runtime/src/core/contextBuilders/google.ts
@@ -25,10 +25,13 @@ const isImageTypeSupported = (mimeType: string | null): boolean => {
};
/**
- * Magic thoughtSignature
- * @see https://ai.google.dev/gemini-api/docs/thought-signatures#model-behavior:~:text=context_engineering_is_the_way_to_go
+ * Magic thoughtSignature to bypass Gemini thought signature validation.
+ * Use `skip_thought_signature_validator` instead of `context_engineering_is_the_way_to_go`
+ * because Vertex AI only accepts `skip_thought_signature_validator`.
+ * @see https://ai.google.dev/gemini-api/docs/thought-signatures
+ * @see https://github.com/pydantic/pydantic-ai/issues/3881
*/
-export const GEMINI_MAGIC_THOUGHT_SIGNATURE = 'context_engineering_is_the_way_to_go';
+export const GEMINI_MAGIC_THOUGHT_SIGNATURE = 'skip_thought_signature_validator';
/**
* Convert OpenAI content part to Google Part format
@@ -332,9 +335,19 @@ export const buildGoogleTools = (
): GoogleFunctionCallTool[] | undefined => {
if (!tools || tools.length === 0) return;
+ // Deduplicate by function name to prevent Vertex AI 400 error:
+ // "Duplicate function declaration found: xxx"
+ const seenToolNames = new Set<string>();
+ const uniqueTools = tools.filter((tool) => {
+ const name = tool.function.name;
+ if (seenToolNames.has(name)) return false;
+ seenToolNames.add(name);
+ return true;
+ });
+
return [
{
- functionDeclarations: tools.map((tool) => buildGoogleTool(tool)),
+ functionDeclarations: uniqueTools.map((tool) => buildGoogleTool(tool)),
},
];
};
diff --git a/packages/utils/src/dedupeBy.test.ts b/packages/utils/src/dedupeBy.test.ts
new file mode 100644
index 00000000000..e03775599b7
--- /dev/null
+++ b/packages/utils/src/dedupeBy.test.ts
@@ -0,0 +1,49 @@
+import { describe, expect, it } from 'vitest';
+
+import { dedupeBy } from './dedupeBy';
+
+describe('dedupeBy', () => {
+ it('should deduplicate items by key, keeping the first occurrence', () => {
+ const items = [
+ { id: '1', name: 'alice' },
+ { id: '2', name: 'bob' },
+ { id: '3', name: 'alice' },
+ ];
+
+ const result = dedupeBy(items, (item) => item.name);
+
+ expect(result).toEqual([
+ { id: '1', name: 'alice' },
+ { id: '2', name: 'bob' },
+ ]);
+ });
+
+ it('should return all items when there are no duplicates', () => {
+ const items = [
+ { id: '1', name: 'alice' },
+ { id: '2', name: 'bob' },
+ ];
+
+ const result = dedupeBy(items, (item) => item.name);
+
+ expect(result).toEqual(items);
+ });
+
+ it('should return empty array for empty input', () => {
+ expect(dedupeBy([], (item: string) => item)).toEqual([]);
+ });
+
+ it('should deduplicate tools by function name (real-world scenario)', () => {
+ const tools = [
+ { function: { name: 'lobe-web-browsing____search____builtin' }, type: 'function' },
+ { function: { name: 'get_weather' }, type: 'function' },
+ { function: { name: 'lobe-web-browsing____search____builtin' }, type: 'function' },
+ ];
+
+ const result = dedupeBy(tools, (tool) => tool.function.name);
+
+ expect(result).toHaveLength(2);
+ expect(result[0].function.name).toBe('lobe-web-browsing____search____builtin');
+ expect(result[1].function.name).toBe('get_weather');
+ });
+});
diff --git a/packages/utils/src/dedupeBy.ts b/packages/utils/src/dedupeBy.ts
new file mode 100644
index 00000000000..a59e338aa71
--- /dev/null
+++ b/packages/utils/src/dedupeBy.ts
@@ -0,0 +1,12 @@
+/**
+ * Deduplicate an array by a key derived from each item, keeping the first occurrence.
+ */
+export const dedupeBy = <T>(items: T[], getKey: (item: T) => string): T[] => {
+ const seen = new Set<string>();
+ return items.filter((item) => {
+ const key = getKey(item);
+ if (seen.has(key)) return false;
+ seen.add(key);
+ return true;
+ });
+};
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index 2b45d29cdab..bc208b09fba 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -1,4 +1,5 @@
export * from './base64';
+export * from './dedupeBy';
export * from './chunkers';
export * from './client/cookie';
export * from './detectChinese';
diff --git a/src/store/chat/agents/createAgentExecutors.ts b/src/store/chat/agents/createAgentExecutors.ts
index de1fcf2f390..2ff5ea6753a 100644
--- a/src/store/chat/agents/createAgentExecutors.ts
+++ b/src/store/chat/agents/createAgentExecutors.ts
@@ -30,6 +30,7 @@ import {
type ModelUsage,
TraceNameMap,
} from '@lobechat/types';
+import { dedupeBy } from '@lobechat/utils';
import debug from 'debug';
import pMap from 'p-map';
@@ -349,17 +350,26 @@ export const createAgentExecutors = (context: {
});
if (additional.tools?.length) {
- resolvedAgentConfig = {
- ...context.agentConfig,
- enabledManifests: [
- ...(context.agentConfig.enabledManifests || []),
- ...additional.enabledManifests,
- ],
- enabledToolIds: [
+ const mergedEnabledManifests = dedupeBy(
+ [...(context.agentConfig.enabledManifests || []), ...additional.enabledManifests],
+ (manifest) => manifest.identifier,
+ );
+ const mergedEnabledToolIds = [
+ ...new Set([
...(context.agentConfig.enabledToolIds || []),
...additional.enabledToolIds,
- ],
- tools: [...(context.agentConfig.tools || []), ...additional.tools],
+ ]),
+ ];
+ const mergedTools = dedupeBy(
+ [...(context.agentConfig.tools || []), ...additional.tools],
+ (tool) => tool.function.name,
+ );
+
+ resolvedAgentConfig = {
+ ...context.agentConfig,
+ enabledManifests: mergedEnabledManifests,
+ enabledToolIds: mergedEnabledToolIds,
+ tools: mergedTools,
};
log(