From febef6f71077f26612a63ef8b8d260a449e4aee8 Mon Sep 17 00:00:00 2001 From: dkundel Date: Fri, 16 Aug 2024 12:30:32 +0200 Subject: [PATCH 1/3] core[patch]: Pass input to invocation for JSON schema tools --- langchain-core/src/tools/index.ts | 11 ++++++++--- langchain-core/src/tools/tests/tools.test.ts | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/langchain-core/src/tools/index.ts b/langchain-core/src/tools/index.ts index 3a73a0b53dde..d2f13dfe3ac3 100644 --- a/langchain-core/src/tools/index.ts +++ b/langchain-core/src/tools/index.ts @@ -1,4 +1,4 @@ -import { z } from "zod"; +import { z, ZodType } from "zod"; import { CallbackManager, CallbackManagerForToolRun, @@ -430,7 +430,7 @@ export class DynamicStructuredTool< this.func = fields.func; this.returnDirect = fields.returnDirect ?? this.returnDirect; this.schema = ( - isZodSchema(fields.schema) ? fields.schema : z.object({}) + isZodSchema(fields.schema) ? fields.schema : z.object({}).passthrough() ) as T extends ZodObjectAny ? T : ZodObjectAny; } @@ -557,7 +557,12 @@ export function tool< | DynamicStructuredTool | DynamicTool { // If the schema is not provided, or it's a string schema, create a DynamicTool - if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) { + if ( + !fields.schema || + // eslint-disable-next-line no-instanceof/no-instanceof + (fields.schema instanceof ZodType && + (!("shape" in fields.schema) || !fields.schema.shape)) + ) { return new DynamicTool({ ...fields, description: diff --git a/langchain-core/src/tools/tests/tools.test.ts b/langchain-core/src/tools/tests/tools.test.ts index 4c38800b3489..3958d85ee4ba 100644 --- a/langchain-core/src/tools/tests/tools.test.ts +++ b/langchain-core/src/tools/tests/tools.test.ts @@ -1,6 +1,6 @@ import { test, expect } from "@jest/globals"; import { z } from "zod"; -import { DynamicStructuredTool, tool } from "../index.js"; +import { DynamicStructuredTool, DynamicTool, tool } from "../index.js"; import { ToolMessage } from "../../messages/tool.js"; test("Tool should error if responseFormat is content_and_artifact but the function doesn't return a tuple", async () => { @@ -128,7 +128,11 @@ test("Tool declared with JSON schema", async () => { required: ["location"], }; const weatherTool = tool( - (_) => { + (input) => { + // even without validation expect input to be passed + expect(input).toEqual({ + somethingSilly: true, + }); return "Sunny"; }, { @@ -136,15 +140,21 @@ test("Tool declared with JSON schema", async () => { schema: weatherSchema, } ); + expect(weatherTool).toBeInstanceOf(DynamicStructuredTool); const weatherTool2 = new DynamicStructuredTool({ name: "weather", description: "get the weather", - func: async (_) => { + func: async (input) => { + // even without validation expect input to be passed + expect(input).toEqual({ + somethingSilly: true, + }); return "Sunny"; }, schema: weatherSchema, }); + // No validation on JSON schema tools await weatherTool.invoke({ somethingSilly: true, From a11e37966e54a0279a59eaa6b6734cefcc53fc40 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Fri, 16 Aug 2024 04:15:07 -0700 Subject: [PATCH 2/3] Lint --- langchain-core/src/tools/tests/tools.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain-core/src/tools/tests/tools.test.ts b/langchain-core/src/tools/tests/tools.test.ts index 3958d85ee4ba..2988eb64d9cf 100644 --- a/langchain-core/src/tools/tests/tools.test.ts +++ b/langchain-core/src/tools/tests/tools.test.ts @@ -1,6 +1,6 @@ import { test, expect } from "@jest/globals"; import { z } from "zod"; -import { DynamicStructuredTool, DynamicTool, tool } from "../index.js"; +import { DynamicStructuredTool, tool } from "../index.js"; import { ToolMessage } from "../../messages/tool.js"; test("Tool should error if responseFormat is content_and_artifact but the function doesn't return a tuple", async () => { From 9ec46a2f4d4598bbca4b97034b63cde4146d8809 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Fri, 16 Aug 2024 04:52:40 -0700 Subject: [PATCH 3/3] Use type guard instead of instanceof --- langchain-core/src/tools/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/langchain-core/src/tools/index.ts b/langchain-core/src/tools/index.ts index d2f13dfe3ac3..a2000a24b21d 100644 --- a/langchain-core/src/tools/index.ts +++ b/langchain-core/src/tools/index.ts @@ -1,4 +1,4 @@ -import { z, ZodType } from "zod"; +import { z } from "zod"; import { CallbackManager, CallbackManagerForToolRun, @@ -559,8 +559,7 @@ export function tool< // If the schema is not provided, or it's a string schema, create a DynamicTool if ( !fields.schema || - // eslint-disable-next-line no-instanceof/no-instanceof - (fields.schema instanceof ZodType && + (isZodSchema(fields.schema) && (!("shape" in fields.schema) || !fields.schema.shape)) ) { return new DynamicTool({