How can I use JSON Schema instead of Zod for tools? #5828
-
Checked other resources
Commit to Help
Example Codeconst myTool = tool(async input => `Hello ${input.name}`, {
name: 'helloWorld',
description: 'say hello',
schema: { // JSON Schema invalid according to types
type: 'object',
properties: {
name: {type: 'string'}
},
required: ['name']
}
}); DescriptionWe already have JSON schemas stored in a database and want to use them to define tool input. It feels wrong to convert them to Zod when most LLMs accept JSON Schema for tools. Requiring the use of Zod this is an unnecessary point of friction, it is wasteful and potentially error prone to transform to back and forth. Ideally there would be a way to create a System Info
Node v18.19.0 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hey @asprouse! I'm here to help you with any bugs, questions, or contributions you may have. Let's tackle this JSON Schema issue together! To use JSON Schema instead of Zod for tools in LangChain, you can directly define your tool's parameters using JSON Schema. The LangChain framework supports JSON Schema natively, so you don't need to convert between JSON Schema and Zod. Here's how you can define a tool using JSON Schema: import { ChatAnthropicTools } from '@langchain/core/tools';
// Define your tool using JSON Schema
const myTool = {
type: "function",
function: {
name: "helloWorld",
description: "say hello",
parameters: {
type: "object",
properties: {
name: { type: "string", description: "The name to greet" },
},
required: ["name"],
},
},
};
// Create an instance of ChatAnthropicTools with the tool defined using JSON Schema
const chatAnthropicTools = new ChatAnthropicTools({
tools: [myTool],
});
// Now you can use chatAnthropicTools with the defined tool This approach allows you to avoid the need to convert between JSON Schema and Zod, as the LangChain framework already handles both formats [1][2]. |
Beta Was this translation helpful? Give feedback.
-
Hey @asprouse! This is actually something we're aiming to standardize in the next few days. Currently, you can pass in JSON schema to most model providers as that's the format they typically require under the hood. This PR (when released) will allow you to pass in tools in OpenAI format to every chat model: const toolInOpenAIFormat = {
type: "function",
function: {
name: "the name of your tool",
description: "the tool description",
parameters: {} // An object containing the JSON schema of the tool.
}
} To pass JSON schema to models until that PR is released, you'll need to use the provider specific tool schema. For example, OpenAI uses the above schema, so you'd use it like this: import { ChatOpenAI } from "@langchain/openai"
const model = new ChatOpenAI({...});
const modelWithTools = model.bindTools([toolInOpenAIFormat]);
const { tool_calls } = await modelWithTools.invoke(input); Or for anthropic: import { ChatAnthropic } from "@langchain/anthropic"
const toolInAnthropicFormat = {
name: "the name of your tool",
description: "the tool description",
input_schema: {} // An object containing the JSON schema of the tool.
};
const model = new ChatAnthropic({...});
const modelWithTools = model.bindTools([toolInAnthropicFormat]);
const { tool_calls } = await modelWithTools.invoke(input); etc etc. You can find the model specific tool format by looking at the Find the chat call options type/interface (will typically be an interface): Here, you can see one of the types for I hope this helps, and let me know if you run into other issues! |
Beta Was this translation helpful? Give feedback.
-
Since this ranks highly in google searches for "LangChain JSON Schema," I just wanted to drop a note here that we now fully support JSON Schema in tool definitions as of #7973. |
Beta Was this translation helpful? Give feedback.
Hey @asprouse! This is actually something we're aiming to standardize in the next few days. Currently, you can pass in JSON schema to most model providers as that's the format they typically require under the hood. This PR (when released) will allow you to pass in tools in OpenAI format to every chat model:
To pass JSON schema to models until that PR is released, you'll need to use the provider specific tool schema. For example, OpenAI uses the above schema, so you'd use it l…