Skip to content

fix(community): handle both Zod and JSON schemas in Watson tool conversion #8272

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

Closed
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
7 changes: 6 additions & 1 deletion libs/langchain-community/src/chat_models/ibm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,17 @@ function _convertToolToWatsonxTool(
if ("type" in tool) {
return tool as WatsonXAI.TextChatParameterTools;
}
// Check if schema is a Zod schema or already a JSON schema
const parameters = isZodSchema(tool.schema)
? zodToJsonSchema(tool.schema)
: tool.schema;

return {
type: "function",
function: {
name: tool.name,
description: tool.description ?? "Tool: " + tool.name,
parameters: zodToJsonSchema(tool.schema),
parameters,
},
};
});
Expand Down
58 changes: 58 additions & 0 deletions libs/langchain-community/src/chat_models/tests/ibm.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable no-process-env */
/* eslint-disable @typescript-eslint/no-explicit-any */
import WatsonxAiMlVml_v1 from "@ibm-cloud/watsonx-ai/dist/watsonx-ai-ml/vml_v1.js";
import { z } from "zod";
import { DynamicStructuredTool } from "@langchain/core/tools";
import {
ChatWatsonx,
ChatWatsonxConstructor,
Expand Down Expand Up @@ -154,6 +156,62 @@ describe("LLM unit tests", () => {
});
expect(intance).toBeDefined();
});

test("Tool conversion handles both Zod schemas and JSON schemas", () => {
const testProps: ChatWatsonxInput = {
model: "ibm/granite-13b-chat-v2",
version: "2024-05-31",
serviceUrl: process.env.WATSONX_AI_SERVICE_URL as string,
projectId: process.env.WATSONX_AI_PROJECT_ID || "testString",
};
const model = new ChatWatsonx({ ...testProps, ...fakeAuthProp });

const zodTool = {
name: "zodTool",
description: "A tool with Zod schema",
schema: z.object({
input: z.string().describe("Input parameter"),
}),
};

const jsonSchemaTool = {
name: "jsonSchemaTool",
description: "A tool with JSON schema",
schema: {
type: "object",
properties: {
input: {
type: "string",
description: "Input parameter",
},
},
required: ["input"],
},
};

expect(() => {
const modelWithTools = model.bindTools([zodTool, jsonSchemaTool]);
expect(modelWithTools).toBeDefined();
}).not.toThrow();

const mcpLikeTool = new DynamicStructuredTool({
name: "mcpLikeTool",
description: "Tool similar to MCP tools",
schema: {
type: "object",
properties: {
city: { type: "string" },
},
required: ["city"],
},
func: async () => "test result",
});

expect(() => {
const modelWithMcpTool = model.bindTools([mcpLikeTool]);
expect(modelWithMcpTool).toBeDefined();
}).not.toThrow();
});
});

describe("Negative tests", () => {
Expand Down