diff --git a/langchain-core/src/tools/index.ts b/langchain-core/src/tools/index.ts index 3a73a0b53dde..a2000a24b21d 100644 --- a/langchain-core/src/tools/index.ts +++ b/langchain-core/src/tools/index.ts @@ -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,11 @@ 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 || + (isZodSchema(fields.schema) && + (!("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..2988eb64d9cf 100644 --- a/langchain-core/src/tools/tests/tools.test.ts +++ b/langchain-core/src/tools/tests/tools.test.ts @@ -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,