BaseChatOpenAI.withStructuredOutput mutates receiver #328

Closed
opened 2026-02-15 18:15:57 -05:00 by yindo · 1 comment
Owner

Originally created by @fauxbytes on GitHub (Aug 5, 2025).

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangGraph.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 LangGraph.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package).

Example Code

import 'dotenv/config';
import { z } from 'zod';
import { ChatOpenAI } from '@langchain/openai';

const shema = z.object({num: z.number()})
const llm = new ChatOpenAI();

const main = async () => {
    console.log(await llm.withStructuredOutput(shema).invoke("a random number pls?"));
  const input = 'a joke, pls?';
  console.log({content: (await llm.invoke(input)).content});  //llm modified by prev call to withStructuredOutput()
  console.log({content: (await new ChatOpenAI().invoke(input)).content});
}

main().catch(console.error);

Error Message and Stack Trace (if applicable)

#output
{ num: 42 }
{ content: '' }
{
  content: "Sure! Here's one for you:\n" +
    '\n' +
    'Why did the scarecrow win an award?\n' +
    '\n' +
    'Because he was outstanding in his field!'
}

Description

Given the jsdoc is stating that withStructuredOutput returns "A new runnable", as-well as multiple howtos on https://langchain-ai.lang.chat/ reusing an llm, I was surprised that the llm was mutated by the call.

Is this WAD?

If so, think it should be highlighted in docs, and examples adapted.

System Info

$ cat package.json |jq '{dependencies, devDependencies}'
{
  "dependencies": {
    "@langchain/anthropic": "^0.3.25",
    "@langchain/core": "^0.3.66",
    "@langchain/langgraph": "^0.3.12",
    "@langchain/openai": "^0.6.3",
    "@langchain/tavily": "^0.1.4",
    "dotenv": "^17.2.0",
    "langchain": "^0.3.30",
    "sqlite": "^5.1.1",
    "typeorm": "^0.3.25",
    "zod": "^4.0.5"
  },
  "devDependencies": {
    "@types/node": "^24.0.14",
    "ts-node": "^10.9.2",
    "typescript": "^5.8.3"
  }
}

$ pnpm -v
10.11.0

$ node -v
v20.18.3

$ wmic os get caption
Caption
Microsoft Windows 11 Pro
Originally created by @fauxbytes on GitHub (Aug 5, 2025). ### Checked other resources - [x] I added a very descriptive title to this issue. - [x] I searched the LangGraph.js documentation with the integrated search. - [x] I used the GitHub search to find a similar question and didn't find it. - [x] I am sure that this is a bug in LangGraph.js rather than my code. - [x] The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package). ### Example Code ```typescript import 'dotenv/config'; import { z } from 'zod'; import { ChatOpenAI } from '@langchain/openai'; const shema = z.object({num: z.number()}) const llm = new ChatOpenAI(); const main = async () => { console.log(await llm.withStructuredOutput(shema).invoke("a random number pls?")); const input = 'a joke, pls?'; console.log({content: (await llm.invoke(input)).content}); //llm modified by prev call to withStructuredOutput() console.log({content: (await new ChatOpenAI().invoke(input)).content}); } main().catch(console.error); ``` ### Error Message and Stack Trace (if applicable) ```bash #output { num: 42 } { content: '' } { content: "Sure! Here's one for you:\n" + '\n' + 'Why did the scarecrow win an award?\n' + '\n' + 'Because he was outstanding in his field!' } ``` ### Description Given the [jsdoc](https://github.com/langchain-ai/langchainjs/blob/055f9724a4fffbe06e705f2828761714bd472f0c/langchain-core/src/language_models/base.ts#L618) is stating that `withStructuredOutput` returns "A new runnable", as-well as multiple howtos on https://langchain-ai.lang.chat/ reusing an llm, I was surprised that the llm was mutated by the call. Is this WAD? If so, think it should be highlighted in docs, and examples adapted. ### System Info ```bash $ cat package.json |jq '{dependencies, devDependencies}' { "dependencies": { "@langchain/anthropic": "^0.3.25", "@langchain/core": "^0.3.66", "@langchain/langgraph": "^0.3.12", "@langchain/openai": "^0.6.3", "@langchain/tavily": "^0.1.4", "dotenv": "^17.2.0", "langchain": "^0.3.30", "sqlite": "^5.1.1", "typeorm": "^0.3.25", "zod": "^4.0.5" }, "devDependencies": { "@types/node": "^24.0.14", "ts-node": "^10.9.2", "typescript": "^5.8.3" } } $ pnpm -v 10.11.0 $ node -v v20.18.3 $ wmic os get caption Caption Microsoft Windows 11 Pro ```
yindo added the bug label 2026-02-15 18:15:57 -05:00
yindo closed this issue 2026-02-15 18:15:57 -05:00
Author
Owner

@dqbd commented on GitHub (Sep 11, 2025):

Hello! Cannot reproduce on my end and looking at the actual output, I think this is most likely a fluke in LLM response instead, since the second output returns { content: "" } instead, rather than { num: ... } as implied.

My actual output, matching the expected behaviour.

{ num: 256 }
{
  content: 'Why did the scarecrow win an award?\n' +
    '\n' +
    'Because he was outstanding in his field!'
}
{
  content: "Why don't scientists trust atoms?\n\nBecause they make up everything!"
}

Closing as can't repro.

@dqbd commented on GitHub (Sep 11, 2025): Hello! Cannot reproduce on my end and looking at the actual output, I think this is most likely a fluke in LLM response instead, since the second output returns `{ content: "" }` instead, rather than `{ num: ... }` as implied. My actual output, matching the expected behaviour. ``` { num: 256 } { content: 'Why did the scarecrow win an award?\n' + '\n' + 'Because he was outstanding in his field!' } { content: "Why don't scientists trust atoms?\n\nBecause they make up everything!" } ``` Closing as can't repro.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#328