Skip to content

Custom Tools with a zod schema do not send field descriptions to OpenAI API #9099

@louislatreille-cb

Description

@louislatreille-cb

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { HumanMessage, SystemMessage } from "@langchain/core/messages";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { DynamicStructuredTool } from "@langchain/core/tools";
import { ChatOpenAI } from "@langchain/openai";
import z from "zod";

const testToolSchema = z.object({
  firstField: z.string().describe("Some description"),
});

const toolWithZodSchema = new DynamicStructuredTool({
  name: "A_Tool",
  description: "This is a test tool",
  schema: testToolSchema,
  func: async () => {
    return "Dummy response";
  },
});

const toolWithJsonSchema = new DynamicStructuredTool({
  name: "A_Tool",
  description: "This is a test tool",
  schema: z.toJSONSchema(testToolSchema),
  func: async () => {
    return "Dummy response";
  },
});

const modelWithZodTool = new ChatOpenAI({
  model: "gpt-4.1-2025-04-14",
  temperature: 0.2,
}).bindTools([toolWithZodSchema]);

const modelWithJsonTool = new ChatOpenAI({
  model: "gpt-4.1-2025-04-14",
  temperature: 0.2,
}).bindTools([toolWithJsonSchema]);

const messages = ChatPromptTemplate.fromMessages([
  new SystemMessage({ content: "You are a helpful assistant" }),
  new HumanMessage({ content: "What is the meaning of life?" }),
]);

// This will be missing the description field when sent to OpenAI
messages.pipe(modelWithZodTool).invoke({});

// This will contain the description field when sent to OpenAI
messages.pipe(modelWithJsonTool).invoke({});

Error Message and Stack Trace (if applicable)

No response

Description

When I create the schema for my custom tools, I use zod schema, and include description for certain fields to help the LLM understand what to put in these fields.

When creating the custom tools with the zod schema object directly, the descriptions are stripped out of what is sent to OpenAI. I've noticed that using LangSmith.

Tool in LangSmith when using zod schema objects directly:

Image

Tool in LangSmith when converting to JSON schema manually:

Image

System Info

langchain@0.3.35 | MIT | deps: 11 | versions: 337
Typescript bindings for langchain
https://github.com/langchain-ai/langchainjs/tree/main/langchain/

keywords: llm, ai, gpt3, chain, prompt, prompt engineering, chatgpt, machine learning, ml, openai, embeddings, vectorstores

dist
.tarball: https://registry.npmjs.org/langchain/-/langchain-0.3.35.tgz
.shasum: 66c859a10aacaf04ed864f2b219a128887c19f56
.integrity: sha512-OkPstP43L3rgaAk72UAVcXy4BzJSiyzXfJsHRBTx9xD3rRtgrAu/jsWpMcsbFAoNO3iGerK+ULzkTzaBJBz6kg==
.unpackedSize: 2.9 MB

dependencies:
@langchain/openai: >=0.1.0 <0.7.0 js-yaml: ^4.1.0 openapi-types: ^12.1.3 yaml: ^2.2.1
@langchain/textsplitters: >=0.0.0 <0.2.0 jsonpointer: ^5.0.1 p-retry: 4 zod: ^3.25.32
js-tiktoken: ^1.0.12 langsmith: ^0.3.67 uuid: ^10.0.0

Node version: v22.17.0
Pnpm version: 10.15.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions