Skip to content

Commit 057a102

Browse files
dkundeljacoblee93
andauthored
core[patch]: Pass input to invocation for JSON schema tools (#6549)
* core[patch]: Pass input to invocation for JSON schema tools * Lint * Use type guard instead of instanceof --------- Co-authored-by: jacoblee93 <[email protected]>
1 parent abb95ca commit 057a102

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

langchain-core/src/tools/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ export class DynamicStructuredTool<
430430
this.func = fields.func;
431431
this.returnDirect = fields.returnDirect ?? this.returnDirect;
432432
this.schema = (
433-
isZodSchema(fields.schema) ? fields.schema : z.object({})
433+
isZodSchema(fields.schema) ? fields.schema : z.object({}).passthrough()
434434
) as T extends ZodObjectAny ? T : ZodObjectAny;
435435
}
436436

@@ -557,7 +557,11 @@ export function tool<
557557
| DynamicStructuredTool<T extends ZodObjectAny ? T : ZodObjectAny>
558558
| DynamicTool {
559559
// If the schema is not provided, or it's a string schema, create a DynamicTool
560-
if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
560+
if (
561+
!fields.schema ||
562+
(isZodSchema(fields.schema) &&
563+
(!("shape" in fields.schema) || !fields.schema.shape))
564+
) {
561565
return new DynamicTool({
562566
...fields,
563567
description:

langchain-core/src/tools/tests/tools.test.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,33 @@ test("Tool declared with JSON schema", async () => {
128128
required: ["location"],
129129
};
130130
const weatherTool = tool(
131-
(_) => {
131+
(input) => {
132+
// even without validation expect input to be passed
133+
expect(input).toEqual({
134+
somethingSilly: true,
135+
});
132136
return "Sunny";
133137
},
134138
{
135139
name: "weather",
136140
schema: weatherSchema,
137141
}
138142
);
143+
expect(weatherTool).toBeInstanceOf(DynamicStructuredTool);
139144

140145
const weatherTool2 = new DynamicStructuredTool({
141146
name: "weather",
142147
description: "get the weather",
143-
func: async (_) => {
148+
func: async (input) => {
149+
// even without validation expect input to be passed
150+
expect(input).toEqual({
151+
somethingSilly: true,
152+
});
144153
return "Sunny";
145154
},
146155
schema: weatherSchema,
147156
});
157+
148158
// No validation on JSON schema tools
149159
await weatherTool.invoke({
150160
somethingSilly: true,

0 commit comments

Comments
 (0)