mirror of
https://github.com/lobehub/lobehub.git
synced 2026-03-27 13:29:15 +07:00
🐛 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:
@@ -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', () => {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user