π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(model-runtime): strip null/empty members from enum for Gemini (#15952)
Arvin Xu25387ada922c
diff --git a/packages/builtin-tool-memory/src/manifest.ts b/packages/builtin-tool-memory/src/manifest.tsindex 19b609cf1fc..9d547ee4034 100644--- a/packages/builtin-tool-memory/src/manifest.ts+++ b/packages/builtin-tool-memory/src/manifest.ts@@ -920,7 +920,7 @@ export const MemoryManifest: BuiltinToolManifest = {},memoryType: {description: 'Memory type, use null for omitting the field',- enum: [...MEMORY_TYPES, null],+ enum: MEMORY_TYPES,type: ['string', 'null'],},summary: {diff --git a/packages/model-runtime/src/core/contextBuilders/google.test.ts b/packages/model-runtime/src/core/contextBuilders/google.test.tsindex 58a2499c147..ac92476d557 100644--- a/packages/model-runtime/src/core/contextBuilders/google.test.ts+++ b/packages/model-runtime/src/core/contextBuilders/google.test.ts@@ -1434,7 +1434,7 @@ describe('google contextBuilders', () => {expect(result.parametersJsonSchema).toEqual(tool.function.parameters);});- it('should pass through nullable types without sanitization', () => {+ it('should keep nullable type but strip null/empty members from enum (Gemini proto)', () => {const tool: ChatCompletionTool = {function: {description: 'A tool with nullable enum',@@ -1442,7 +1442,7 @@ describe('google contextBuilders', () => {parameters: {properties: {status: {- enum: ['active', 'inactive', null],+ enum: ['active', 'inactive', null, ''],type: ['string', 'null'],},},@@ -1454,8 +1454,53 @@ describe('google contextBuilders', () => {const result = buildGoogleTool(tool);- // nullable types and null enum values should be passed through as-is- expect(result.parametersJsonSchema).toEqual(tool.function.parameters);+ // Gemini proto only accepts non-empty STRING enum members; null/'' get coerced+ // to '' and rejected with "enum[i]: cannot be empty", so they must be filtered.+ // The nullable `type` is preserved to keep the field optional.+ expect(result.parametersJsonSchema).toEqual({+ properties: {+ status: {+ enum: ['active', 'inactive'],+ type: ['string', 'null'],+ },+ },+ type: 'object',+ });+ });++ // Regression for LOBE-10456: the memory tool's `memoryType` enum carried a+ // trailing `null` sentinel (`[...MEMORY_TYPES, null]`), which Gemini rejected+ // with `enum[10]: cannot be empty`. The sanitizer must drop the null member.+ it('should strip the null sentinel from a memory-style nullable enum', () => {+ const memoryTypes = ['fact', 'event', 'people', 'preference'];+ const tool: ChatCompletionTool = {+ function: {+ description: 'updateIdentityMemory-style tool',+ name: 'updateIdentityMemory',+ parameters: {+ properties: {+ set: {+ properties: {+ memoryType: {+ description: 'Memory type, use null for omitting the field',+ enum: [...memoryTypes, null],+ type: ['string', 'null'],+ },+ },+ type: 'object',+ },+ },+ type: 'object',+ },+ },+ type: 'function',+ };++ const result = buildGoogleTool(tool) as any;++ expect(result.parametersJsonSchema.properties.set.properties.memoryType.enum).toEqual(+ memoryTypes,+ );});it('should pass through const values without conversion', () => {diff --git a/packages/model-runtime/src/core/contextBuilders/google.ts b/packages/model-runtime/src/core/contextBuilders/google.tsindex a8d87f8c89b..1706e290b25 100644--- a/packages/model-runtime/src/core/contextBuilders/google.ts+++ b/packages/model-runtime/src/core/contextBuilders/google.ts@@ -362,17 +362,26 @@ export const sanitizeGeminiSchema = (schema: any): any => {const isObjectType = (t: unknown): boolean =>typeof t === 'string' ? t === 'object' : Array.isArray(t) && t.includes('object');- // Strip enum from non-STRING types and empty enums- // Gemini proto: "enum: only allowed for STRING type"- if (- sanitized.enum !== undefined &&- (!isStringType(sanitized.type) || !Array.isArray(sanitized.enum) || sanitized.enum.length === 0)- ) {- console.warn(- '[google] sanitizeGeminiSchema stripped enum β not allowed for non-STRING type or empty',- { type: sanitized.type, enumLength: sanitized.enum?.length },- );- delete sanitized.enum;+ // Sanitize enum for Gemini proto compliance:+ // - enum is only allowed on STRING type fields+ // - enum members must be non-empty strings. Gemini's schema proto only accepts+ // STRING enum members, so a `null`/non-string sentinel gets coerced to "" and+ // rejected with "enum[i]: cannot be empty". Filter such members out first.+ if (sanitized.enum !== undefined) {+ if (Array.isArray(sanitized.enum)) {+ sanitized.enum = sanitized.enum.filter((v: unknown) => typeof v === 'string' && v !== '');+ }+ if (+ !isStringType(sanitized.type) ||+ !Array.isArray(sanitized.enum) ||+ sanitized.enum.length === 0+ ) {+ console.warn(+ '[google] sanitizeGeminiSchema stripped enum β not allowed for non-STRING type, empty, or no valid string members',+ { type: sanitized.type, enumLength: sanitized.enum?.length },+ );+ delete sanitized.enum;+ }}// Strip required from non-OBJECT types and empty required arraysdiff --git a/packages/model-runtime/src/providers/google/generateObject.test.ts b/packages/model-runtime/src/providers/google/generateObject.test.tsindex eaa5f004808..58f740acfdc 100644--- a/packages/model-runtime/src/providers/google/generateObject.test.ts+++ b/packages/model-runtime/src/providers/google/generateObject.test.ts@@ -400,8 +400,8 @@ describe('Google generateObject', () => {expect(sanitizeGeminiSchema(undefined)).toBeUndefined();});- // nullable string enums should be preserved- it('should preserve enum on nullable STRING types (type: array with string)', () => {+ // nullable string enums should keep string members but drop the null sentinel+ it('should strip null members from enum on nullable STRING types (type: array with string)', () => {const schema = {properties: {status: {@@ -414,10 +414,12 @@ describe('Google generateObject', () => {const result = sanitizeGeminiSchema(schema);+ // Gemini proto only accepts STRING enum members; null is filtered out while+ // nullability stays expressed via type: ['string', 'null'].expect(result).toEqual({properties: {status: {- enum: ['active', 'inactive', null],+ enum: ['active', 'inactive'],type: ['string', 'null'],},},@@ -1396,8 +1398,8 @@ describe('Google generateObject', () => {warnSpy.mockRestore();});- // buildGoogleTool should preserve nullable string enum- it('should preserve enum on nullable STRING type in tool parameters', () => {+ // buildGoogleTool should keep string enum members but drop the null sentinel+ it('should strip null members from enum on nullable STRING type in tool parameters', () => {const tool: any = {function: {description: 'A tool with nullable enum',@@ -1419,11 +1421,11 @@ describe('Google generateObject', () => {const result = buildGoogleTool(tool);- // nullable types and null enum values should be passed through as-is+ // nullable type is passed through, but the null enum member is filtered outexpect(result.parametersJsonSchema).toEqual({properties: {status: {- enum: ['active', 'inactive', null],+ enum: ['active', 'inactive'],type: ['string', 'null'],},},