Skip to content

google-common[patch]: remove all additionalProperties for gemini #5247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion libs/langchain-google-common/src/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ test("zodToGeminiParameters can convert zod schema to gemini schema", () => {
.describe("The type of operation to execute"),
number1: z.number().describe("The first number to operate on."),
number2: z.number().describe("The second number to operate on."),
childObject: z.object({}),
})
.describe("A simple calculator tool");

const convertedSchema = zodToGeminiParameters(zodSchema);

expect(convertedSchema.type).toBe("object");
expect(convertedSchema.description).toBe("A simple calculator tool");
expect(convertedSchema).not.toContain("additionalProperties");
expect(convertedSchema.properties).toEqual({
operation: {
type: "string",
Expand All @@ -31,6 +33,15 @@ test("zodToGeminiParameters can convert zod schema to gemini schema", () => {
type: "number",
description: "The second number to operate on.",
},
childObject: {
type: "object",
properties: {},
},
});
expect(convertedSchema.required).toEqual(["operation", "number1", "number2"]);
expect(convertedSchema.required).toEqual([
"operation",
"number1",
"number2",
"childObject",
]);
});
10 changes: 10 additions & 0 deletions libs/langchain-google-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,13 @@ export interface GoogleAISafetyHandler {
export interface GoogleAISafetyParams {
safetyHandler?: GoogleAISafetyHandler;
}

export type GeminiJsonSchema = Record<string, unknown> & {
properties?: Record<string, GeminiJsonSchema>;
type: GeminiFunctionSchemaType;
};

export interface GeminiJsonSchemaDirty extends GeminiJsonSchema {
properties?: Record<string, GeminiJsonSchemaDirty>;
additionalProperties?: boolean;
}
43 changes: 40 additions & 3 deletions libs/langchain-google-common/src/utils/zod_to_gemini_parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,41 @@

import type { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { GeminiFunctionSchema } from "../types.js";
import {
GeminiFunctionSchema,
GeminiJsonSchema,
GeminiJsonSchemaDirty,
} from "../types.js";

function removeAdditionalProperties(
schema: GeminiJsonSchemaDirty
): GeminiJsonSchema {
const updatedSchema: GeminiJsonSchemaDirty = { ...schema };
if (Object.hasOwn(updatedSchema, "additionalProperties")) {
delete updatedSchema.additionalProperties;
}
if (updatedSchema.properties) {
const keys = Object.keys(updatedSchema.properties);
removeProperties(updatedSchema.properties, keys, 0);
}

return updatedSchema;
}

function removeProperties(
properties: Record<string, GeminiJsonSchemaDirty>,
keys: string[],
index: number
): void {
if (index >= keys.length) {
return;
}

const key = keys[index];
// eslint-disable-next-line no-param-reassign
properties[key] = removeAdditionalProperties(properties[key]);
removeProperties(properties, keys, index + 1);
}

export function zodToGeminiParameters(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -11,8 +45,11 @@ export function zodToGeminiParameters(
// Gemini doesn't accept either the $schema or additionalProperties
// attributes, so we need to explicitly remove them.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const jsonSchema = zodToJsonSchema(zodObj) as any;
const { $schema, additionalProperties, ...rest } = jsonSchema;
// const jsonSchema = zodToJsonSchema(zodObj) as any;
const jsonSchema = removeAdditionalProperties(
zodToJsonSchema(zodObj) as GeminiJsonSchemaDirty
);
const { $schema, ...rest } = jsonSchema;

return rest;
}
Loading