From 49622d4796e206b901447a562839290c842cb06a Mon Sep 17 00:00:00 2001 From: bracesproul Date: Wed, 17 Jul 2024 10:54:01 -0700 Subject: [PATCH 1/2] google-genai[patch]: Fix removing additional properties from schema --- .../src/tests/chat_models.test.ts | 44 +++++++++++++------ .../src/utils/zod_to_genai_parameters.ts | 3 +- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/libs/langchain-google-genai/src/tests/chat_models.test.ts b/libs/langchain-google-genai/src/tests/chat_models.test.ts index 22c1de19cc4a..7363bb77e1e5 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.test.ts @@ -15,6 +15,19 @@ import { convertMessageContentToParts, } from "../utils/common.js"; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function extractKeys(obj: Record, keys: string[] = []) { + for (const key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + keys.push(key); + if (typeof obj[key] === "object" && obj[key] !== null) { + extractKeys(obj[key], keys); + } + } + } + return keys; +} + test("Google AI - `temperature` must be in range [0.0,1.0]", async () => { expect( () => @@ -89,19 +102,6 @@ test("Google AI - `safetySettings` category array must be unique", async () => { }); test("removeAdditionalProperties can remove all instances of additionalProperties", async () => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - function extractKeys(obj: Record, keys: string[] = []) { - for (const key in obj) { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - keys.push(key); - if (typeof obj[key] === "object" && obj[key] !== null) { - extractKeys(obj[key], keys); - } - } - } - return keys; - } - const idealResponseSchema = z.object({ idealResponse: z .string() @@ -137,6 +137,24 @@ test("removeAdditionalProperties can remove all instances of additionalPropertie expect( arrSchemaObj.find((key) => key === "additionalProperties") ).toBeUndefined(); + + const analysisSchema = z.object({ + decision: z.enum(["UseAPI", "UseFallback"]), + explanation: z.string(), + apiDetails: z + .object({ + serviceName: z.string(), + endpointName: z.string(), + parameters: z.record(z.unknown()), + extractionPath: z.string(), + }) + .optional(), + }); + const parsedAnalysisSchema = removeAdditionalProperties(zodToJsonSchema(analysisSchema)); + const analysisSchemaObj = extractKeys(parsedAnalysisSchema); + expect( + analysisSchemaObj.find((key) => key === "additionalProperties") + ).toBeUndefined(); }); test("convertMessageContentToParts correctly handles message types", () => { diff --git a/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts b/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts index e43285cc6639..a53302c2d848 100644 --- a/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts +++ b/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts @@ -25,8 +25,7 @@ export function removeAdditionalProperties( const newObj = { ...obj }; if ( - "additionalProperties" in newObj && - typeof newObj.additionalProperties === "boolean" + "additionalProperties" in newObj ) { delete newObj.additionalProperties; } From 9f7e3a090a6a1e9dd4e161ce2597b0341cc2c3b7 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Wed, 17 Jul 2024 10:55:02 -0700 Subject: [PATCH 2/2] chore: lint files --- libs/langchain-google-genai/src/tests/chat_models.test.ts | 4 +++- .../src/utils/zod_to_genai_parameters.ts | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/langchain-google-genai/src/tests/chat_models.test.ts b/libs/langchain-google-genai/src/tests/chat_models.test.ts index 7363bb77e1e5..ca41b8b5e100 100644 --- a/libs/langchain-google-genai/src/tests/chat_models.test.ts +++ b/libs/langchain-google-genai/src/tests/chat_models.test.ts @@ -150,7 +150,9 @@ test("removeAdditionalProperties can remove all instances of additionalPropertie }) .optional(), }); - const parsedAnalysisSchema = removeAdditionalProperties(zodToJsonSchema(analysisSchema)); + const parsedAnalysisSchema = removeAdditionalProperties( + zodToJsonSchema(analysisSchema) + ); const analysisSchemaObj = extractKeys(parsedAnalysisSchema); expect( analysisSchemaObj.find((key) => key === "additionalProperties") diff --git a/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts b/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts index a53302c2d848..dd59665164c8 100644 --- a/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts +++ b/libs/langchain-google-genai/src/utils/zod_to_genai_parameters.ts @@ -24,9 +24,7 @@ export function removeAdditionalProperties( if (typeof obj === "object" && obj !== null) { const newObj = { ...obj }; - if ( - "additionalProperties" in newObj - ) { + if ("additionalProperties" in newObj) { delete newObj.additionalProperties; }