From aeb3ccef323858b915ea9d4a5cc60de9657b96f1 Mon Sep 17 00:00:00 2001 From: Sachin Padmanabhan Date: Mon, 17 Feb 2025 22:03:51 -0800 Subject: [PATCH 1/2] fully translate to google schema --- packages/proxy/src/proxy.ts | 86 ++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/packages/proxy/src/proxy.ts b/packages/proxy/src/proxy.ts index 32a8359..e2f2f10 100644 --- a/packages/proxy/src/proxy.ts +++ b/packages/proxy/src/proxy.ts @@ -1608,55 +1608,63 @@ async function fetchAnthropic( }; } -async function googleSchemaFromJsonSchemaInternal( - root: any, - schema: any, -): Promise { - if (!schema || typeof schema !== "object") { - return schema; +function convertToNullable(obj: any) { + const anyOf = obj.anyOf; + if (anyOf) { + if (anyOf.length !== 2) { + throw new ProxyBadRequestError( + "Google only supports Optional types for unions", + ); + } + const [a, b] = anyOf; + if (a.type === "null") { + Object.assign(obj, b); + } else if (b.type === "null") { + Object.assign(obj, a); + } else { + throw new ProxyBadRequestError( + "Google only supports Optional types for unions", + ); + } + delete obj.anyOf; + obj.nullable = true; } - await $RefParser.dereference(schema); - const allowedFields = [ - "type", - "format", - "description", - "nullable", - "items", - "enum", - "properties", - "required", - "example", - ]; - - const result: any = {}; - - for (const [key, value] of Object.entries(schema)) { - if (!allowedFields.includes(key)) { - continue; + if (obj.properties) { + for (const value of Object.values(obj.properties)) { + convertToNullable(value); } + } - if (key === "properties") { - result[key] = Object.fromEntries( - await Promise.all( - Object.entries(value as Record).map(async ([k, v]) => [ - k, - await googleSchemaFromJsonSchemaInternal(root, v), - ]), - ), - ); - } else if (key === "items") { - result[key] = await googleSchemaFromJsonSchemaInternal(root, value); - } else { - result[key] = value; + if (obj.items) { + convertToNullable(obj.items); + } +} + +function stripFields(obj: any) { + delete obj.title; + delete obj.additionalProperties; + + if (obj.properties) { + for (const value of Object.values(obj.properties)) { + stripFields(value); } } - return result; + if (obj.items) { + stripFields(obj.items); + } } async function googleSchemaFromJsonSchema(schema: any): Promise { - return await googleSchemaFromJsonSchemaInternal(schema, schema); + if (!schema || typeof schema !== "object") { + return schema; + } + await $RefParser.dereference(schema); + delete schema.$defs; + convertToNullable(schema); + stripFields(schema); + return schema; } async function openAIToolsToGoogleTools(params: ChatCompletionCreateParams) { From ec3afa80e70603addb632ad4f87aff2e013abd86 Mon Sep 17 00:00:00 2001 From: Sachin Padmanabhan Date: Mon, 17 Feb 2025 22:28:20 -0800 Subject: [PATCH 2/2] rm default --- packages/proxy/src/proxy.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/proxy/src/proxy.ts b/packages/proxy/src/proxy.ts index e2f2f10..fdeebec 100644 --- a/packages/proxy/src/proxy.ts +++ b/packages/proxy/src/proxy.ts @@ -1644,6 +1644,7 @@ function convertToNullable(obj: any) { function stripFields(obj: any) { delete obj.title; delete obj.additionalProperties; + delete obj.default; if (obj.properties) { for (const value of Object.values(obj.properties)) {