[GH-ISSUE #70] Error in middleware when using @langchain/google-genai, undefined is not an object (evaluating 'candidateContent.parts') #21

Closed
opened 2026-02-16 06:16:50 -05:00 by yindo · 2 comments
Owner

Originally created by @eyueldk on GitHub (Nov 26, 2025).
Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/70

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

Using a middleware in an agent, can randomly cause the following an error in @langchain/google-genai. I encountered this using createDeepAgent and also createAgent with a TodosMiddleware

It frequently shows up, but not always.

Error Message and Stack Trace (if applicable)

259 | generations: [],
260 | llmOutput: { filters: response.promptFeedback }
261 | };
262 | const [candidate] = response.candidates;
263 | const { content: candidateContent, ...generationInfo } = candidate;
264 | const functionCalls = candidateContent.parts.reduce((acc, p) => {
^
TypeError: Error in middleware "todoListMiddleware": Error in middleware "FilesystemMiddleware": Error in middleware "subAgentMiddleware": Error in middleware "PromptCachingMiddleware": undefined is not an object (evaluating 'candidateContent.parts')
pregelTaskId: "e99087a4-aea9-519f-bf01-37ac0fdce23a",

  at mapGenerateContentResultToChatResult (C:\Users\User\source\repos\project\node_modules\@langchain\google-genai\dist\utils\common.js:264:24)
  at _generate (C:\Users\User\source\repos\project\node_modules\@langchain\google-genai\dist\chat_models.js:546:28)

Description

Model

const model = new ChatGoogleGenerativeAI({
  model: "gemini-2.5-flash",
  apiKey: process.env.GOOGLE_API_KEY,
});

Agent

const agent = createDeepAgent({
  model: this.model,
  tools,
  systemPrompt: SYSTEM_PROMPT,
});

agent.invoke causes an error.

System Info

  • Environment: bun 1.3.1
  • Packages:
    • "@langchain/google-genai": "^1.0.3"
    • "langchain": "^1.0.6",
Originally created by @eyueldk on GitHub (Nov 26, 2025). Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/70 ### Checked other resources - [x] This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/). - [x] I added a very descriptive title to this issue. - [x] I searched the LangChain.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 LangChain.js rather than my code. - [x] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package). ### Example Code Using a middleware in an agent, can randomly cause the following an error in @langchain/google-genai. I encountered this using `createDeepAgent` and also `createAgent` with a `TodosMiddleware` It frequently shows up, but not always. ### Error Message and Stack Trace (if applicable) 259 | generations: [], 260 | llmOutput: { filters: response.promptFeedback } 261 | }; 262 | const [candidate] = response.candidates; 263 | const { content: candidateContent, ...generationInfo } = candidate; 264 | const functionCalls = candidateContent.parts.reduce((acc, p) => { ^ TypeError: Error in middleware "todoListMiddleware": Error in middleware "FilesystemMiddleware": Error in middleware "subAgentMiddleware": Error in middleware "PromptCachingMiddleware": undefined is not an object (evaluating 'candidateContent.parts') pregelTaskId: "e99087a4-aea9-519f-bf01-37ac0fdce23a", at mapGenerateContentResultToChatResult (C:\Users\User\source\repos\project\node_modules\@langchain\google-genai\dist\utils\common.js:264:24) at _generate (C:\Users\User\source\repos\project\node_modules\@langchain\google-genai\dist\chat_models.js:546:28) ### Description Model ```typescript const model = new ChatGoogleGenerativeAI({ model: "gemini-2.5-flash", apiKey: process.env.GOOGLE_API_KEY, }); ``` Agent ```typescript const agent = createDeepAgent({ model: this.model, tools, systemPrompt: SYSTEM_PROMPT, }); ``` `agent.invoke` causes an error. ### System Info * Environment: `bun 1.3.1` * Packages: * `"@langchain/google-genai": "^1.0.3"` * `"langchain": "^1.0.6",`
yindo added the bug label 2026-02-16 06:16:50 -05:00
Author
Owner

@fernandolangaro commented on GitHub (Nov 26, 2025):

I have the exact same problem. I just fave up trying to fix it.

I am currently using:

const { ChatVertexAI } = require('@langchain/google-vertexai');
const authOptions = {
  projectId: process.env.GOOGLE_AI_PROJECT_ID,
  credentials: {
    client_email: process.env.GOOGLE_AI_CLIENT_EMAIL,
    private_key: process.env.GOOGLE_AI_PRIVATE_KEY.replace(/\\n/g, '\n'),
  },
};

new ChatVertexAI({ model: 'gemini-2.5-flash', authOptions })

instead of google. it still shows this error at langsmith, but it is not so bad

@fernandolangaro commented on GitHub (Nov 26, 2025): I have the exact same problem. I just fave up trying to fix it. I am currently using: ``` ts const { ChatVertexAI } = require('@langchain/google-vertexai'); const authOptions = { projectId: process.env.GOOGLE_AI_PROJECT_ID, credentials: { client_email: process.env.GOOGLE_AI_CLIENT_EMAIL, private_key: process.env.GOOGLE_AI_PRIVATE_KEY.replace(/\\n/g, '\n'), }, }; new ChatVertexAI({ model: 'gemini-2.5-flash', authOptions }) ``` instead of google. it still shows this error at langsmith, but it is not so bad
yindo changed title from Error in middleware when using @langchain/google-genai, `undefined is not an object (evaluating 'candidateContent.parts')` to [GH-ISSUE #70] Error in middleware when using @langchain/google-genai, `undefined is not an object (evaluating 'candidateContent.parts')` 2026-06-05 17:21:00 -04:00
yindo closed this issue 2026-06-05 17:21:00 -04:00
Author
Owner

@hntrl commented on GitHub (Apr 14, 2026):

Sorry for the delayed response, but I believe this was fixed in later versions of google packages.

<!-- gh-comment-id:4241641049 --> @hntrl commented on GitHub (Apr 14, 2026): Sorry for the delayed response, but I believe this was fixed in later versions of google packages.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#21