Skip to content
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

core[patch]: Pass input to invocation for JSON schema tools #6549

Merged
merged 5 commits into from
Aug 16, 2024
Merged
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
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
Loading