fix(proxy): preserve valid enums during schema normalization

This commit is contained in:
Tam Nhu Tran
2026-04-18 19:02:55 -04:00
parent 32d6bfdda7
commit 22ab58b02e
2 changed files with 86 additions and 0 deletions
+1
View File
@@ -216,6 +216,7 @@ export function sanitizeSchemaForOpenAICompat(schema: unknown): Record<string, u
const schemaWithoutEnum = { ...record };
delete schemaWithoutEnum.enum;
delete schemaWithoutEnum.const;
if (Array.isArray(record.enum)) {
const filteredEnum = record.enum.filter((value) => schemaAllowsValue(schemaWithoutEnum, value));
+85
View File
@@ -0,0 +1,85 @@
import { describe, expect, it } from 'bun:test';
import { normalizeSchemaForOpenAI } from '../../../src/utils/schema-sanitizer';
describe('normalizeSchemaForOpenAI', () => {
it('strips incompatible keywords and enforces strict object schemas', () => {
const result = normalizeSchemaForOpenAI({
type: 'object',
properties: {
query: { type: 'string', pattern: '^[a-z]+$', minLength: 3 },
limit: { type: 'integer', minimum: 1 },
},
required: ['query', 'missing'],
additionalProperties: true,
default: { query: 'docs' },
});
expect(result).toEqual({
type: 'object',
properties: {
query: { type: 'string' },
limit: { type: 'integer' },
},
required: ['query'],
additionalProperties: false,
});
});
it('drops enum and const values that no longer match the schema type', () => {
const result = normalizeSchemaForOpenAI({
type: 'string',
enum: ['ok', 1, null],
const: 1,
});
expect(result).toEqual({
type: 'string',
enum: ['ok'],
});
});
it('normalizes nested combinators and arrays recursively', () => {
const result = normalizeSchemaForOpenAI({
anyOf: [
{
type: 'object',
properties: {
image: {
type: 'array',
items: {
type: 'object',
properties: {
url: { type: 'string', format: 'uri' },
},
},
},
},
},
],
});
expect(result).toEqual({
anyOf: [
{
type: 'object',
properties: {
image: {
type: 'array',
items: {
type: 'object',
properties: {
url: { type: 'string' },
},
required: [],
additionalProperties: false,
},
},
},
required: [],
additionalProperties: false,
},
],
});
});
});