Skip to content
Open
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
3 changes: 2 additions & 1 deletion libs/providers/langchain-google-genai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
} from "./types.js";
import { convertToolsToGenAI } from "./utils/tools.js";
import PROFILES from "./profiles.js";
import { cleanGeminiSchema } from "./cleanGeminiSchema";

interface TokenUsage {
completionTokens?: number;
Expand Down Expand Up @@ -1093,7 +1094,7 @@ export class ChatGoogleGenerativeAI
name: functionName,
description:
jsonSchema.description ?? "A function available to call.",
parameters: jsonSchema as GenerativeAIFunctionDeclarationSchema,
parameters: cleanGeminiSchema(jsonSchema) as GenerativeAIFunctionDeclarationSchema,
},
],
},
Expand Down
22 changes: 22 additions & 0 deletions libs/providers/langchain-google-genai/src/cleanGeminiSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export function cleanGeminiSchema(schema: any): any {
if (!schema || typeof schema !== "object") {
return schema;
}

const cleaned: any = Array.isArray(schema)
? schema.map((item) => cleanGeminiSchema(item))
: { ...schema };


delete cleaned.propertyNames;
delete cleaned.patternProperties;


for (const key of Object.keys(cleaned)) {
if (typeof cleaned[key] === "object") {
cleaned[key] = cleanGeminiSchema(cleaned[key]);
}
}

return cleaned;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { cleanGeminiSchema } from "../cleanGeminiSchema";

describe("cleanGeminiSchema", () => {
it("removes propertyNames from schema", () => {
const schema = {
type: "object",
propertyNames: {
pattern: "^[a-zA-Z]+$",
},
properties: {
name: { type: "string" },
},
};

const cleaned = cleanGeminiSchema(schema as any);

expect(cleaned.propertyNames).toBeUndefined();
expect(cleaned.properties).toBeDefined();
});

it("removes patternProperties from schema", () => {
const schema = {
type: "object",
patternProperties: {
"^S_": { type: "string" },
},
properties: {
id: { type: "number" },
},
};

const cleaned = cleanGeminiSchema(schema as any);

expect(cleaned.patternProperties).toBeUndefined();
expect(cleaned.properties).toBeDefined();
});

it("does not modify schema without unsupported fields", () => {
const schema = {
type: "object",
properties: {
age: { type: "number" },
},
};

const cleaned = cleanGeminiSchema(schema as any);

expect(cleaned).toEqual(schema);
});
});
8 changes: 5 additions & 3 deletions libs/providers/langchain-google-genai/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { isLangChainTool } from "@langchain/core/utils/function_calling";
import { isOpenAITool } from "@langchain/core/language_models/base";
import { ToolCallChunk } from "@langchain/core/messages/tool";
import { v4 as uuidv4 } from "uuid";
import { cleanGeminiSchema } from "../cleanGeminiSchema.js";
import {
jsonSchemaToGeminiParameters,
schemaToGenerativeAIParameters,
Expand Down Expand Up @@ -358,8 +359,8 @@ export function convertMessageContentToParts(

const result = Array.isArray(message.content)
? (message.content
.map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
.filter((p) => p !== undefined) as Part[])
.map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
.filter((p) => p !== undefined) as Part[])
: message.content;

if (message.status === "error") {
Expand Down Expand Up @@ -799,7 +800,8 @@ export function convertToGenerativeAITools(
return {
name: tool.name,
description: tool.description,
parameters: jsonSchema,
parameters: cleanGeminiSchema(jsonSchema),

};
}
if (isOpenAITool(tool)) {
Expand Down