Skip to content

Commit

Permalink
core[patch]: Pass input to invocation for JSON schema tools (#6549)
Browse files Browse the repository at this point in the history
* core[patch]: Pass input to invocation for JSON schema tools

* Lint

* Use type guard instead of instanceof

---------

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
dkundel and jacoblee93 authored Aug 16, 2024
1 parent abb95ca commit 057a102
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions langchain-core/src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -557,7 +557,11 @@ export function tool<
| DynamicStructuredTool<T extends ZodObjectAny ? T : ZodObjectAny>
| 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:
Expand Down
14 changes: 12 additions & 2 deletions langchain-core/src/tools/tests/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,33 @@ 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";
},
{
name: "weather",
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,
Expand Down

0 comments on commit 057a102

Please sign in to comment.