🐛 fix(model-runtime): filter null values from enum for Gemini compatibility (#11859)

Gemini API doesn't support null values in enum arrays, causing errors like:
`GenerateContentRequest.tools[0].function_declarations[29].parameters.properties[set].properties[memoryType].enum[10]: cannot be empty`

This fix filters out null values from enum arrays in the `sanitizeSchemaForGoogle` function.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Arvin Xu
2026-01-26 20:08:47 +08:00
committed by GitHub
parent 732bbf1948
commit 1163f71f39
2 changed files with 83 additions and 0 deletions

View File

@@ -1154,6 +1154,79 @@ describe('google contextBuilders', () => {
type: 'string',
});
});
it('should filter null values from enum arrays for Google compatibility', () => {
const tool: ChatCompletionTool = {
function: {
description: 'A tool with enum containing null',
name: 'enumTool',
parameters: {
properties: {
memoryType: {
enum: ['short_term', 'long_term', null, 'working'],
type: 'string',
},
nested: {
properties: {
status: {
enum: [null, 'active', 'inactive', null],
type: 'string',
},
},
type: 'object',
},
},
type: 'object',
},
},
type: 'function',
};
const result = buildGoogleTool(tool);
// null values should be filtered from enum arrays
expect(result.parameters?.properties).toEqual({
memoryType: {
enum: ['short_term', 'long_term', 'working'],
type: 'string',
},
nested: {
properties: {
status: {
enum: ['active', 'inactive'],
type: 'string',
},
},
type: 'object',
},
});
});
it('should handle enum with only null values', () => {
const tool: ChatCompletionTool = {
function: {
description: 'A tool with enum containing only null',
name: 'nullEnumTool',
parameters: {
properties: {
value: {
enum: [null],
type: 'string',
},
},
type: 'object',
},
},
type: 'function',
};
const result = buildGoogleTool(tool);
// When enum only contains null, the enum property should be removed
expect(result.parameters?.properties?.value).toEqual({
type: 'string',
});
});
});
describe('buildGoogleTools', () => {

View File

@@ -248,6 +248,16 @@ const sanitizeSchemaForGoogle = (schema: Record<string, any>): Record<string, an
continue;
}
// Filter null values from enum arrays (Google doesn't support null in enum)
if (key === 'enum' && Array.isArray(value)) {
const filteredEnum = value.filter((item) => item !== null);
// Only set enum if there are remaining values after filtering
if (filteredEnum.length > 0) {
result[key] = filteredEnum;
}
continue;
}
// Recursively process nested objects
if (value && typeof value === 'object') {
result[key] = sanitizeSchemaForGoogle(value);