vespperhq/vespper

Langchain throws `Cannot read properties of null`

Closed this issue · 6 comments

For somereason, sometimes messages array is null in the backend, thus resulting in an error from langchain

2024-07-23 09:37:04 TypeError: Cannot read properties of null (reading 'length')
2024-07-23 09:37:04 at new AIMessage (/app/node_modules/@langchain/core/dist/messages/ai.cjs:36:30)
2024-07-23 09:37:04 at openAIResponseToChatMessage (/app/node_modules/@langchain/openai/dist/chat_models.cjs:65:20)
2024-07-23 09:37:04 at ChatOpenAI._generate (/app/node_modules/@langchain/openai/dist/chat_models.cjs:641:30)
2024-07-23 09:37:04 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2024-07-23 09:37:04 at async Promise.allSettled (index 0)
2024-07-23 09:37:04 at async ChatOpenAI._generateUncached (/app/node_modules/@langchain/core/dist/language_models/chat_models.cjs:118:25)
2024-07-23 09:37:04 at async ChatOpenAI.invoke (/app/node_modules/@langchain/core/dist/language_models/chat_models.cjs:54:24)
2024-07-23 09:37:04 at async RunnableSequence.invoke (/app/node_modules/@langchain/core/dist/runnables/base.cjs:1066:33)
2024-07-23 09:37:04 at async RunnableAgent.plan (/app/node_modules/langchain/dist/agents/agent.cjs:123:24)
2024-07-23 09:37:04 at async AgentExecutor._call (/app/node_modules/langchain/dist/agents/executor.cjs:374:26)
2024-07-23 09:37:04 at async AgentExecutor.call (/app/node_modules/langchain/dist/chains/base.cjs:120:28)
2024-07-23 09:37:04 at async runAgent (/app/services/api/dist/src/agent/helper.js:66:24)
2024-07-23 09:37:04 at async getCompletions (/app/services/api/dist/src/routers/chat.js:99:47)
2024-07-23 09:37:04 developmentError error: AppError: Cannot read properties of null (reading 'length')
2024-07-23 09:37:04 at getCompletions (/app/services/api/dist/src/routers/chat.js:115:19)
2024-07-23 09:37:04 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
2024-07-23 09:37:04 statusCode: 500,
2024-07-23 09:37:04 status: 'error',
2024-07-23 09:37:04 internalCode: 33
2024-07-23 09:37:04 }

Seems like langchain has solved this issue in their latest version. We might need to upgrade.

Hey, I'm the reporter of this bug. Sorry for the lack of clarity, it's not the messages array being null, but the tool calls array being null. I speculate that this happens when the model does not call tools in the response. In earlier versions, Langchain incorrectly checks if the array is strictly undefined, so the null array can still pass, causing the error. The latest version changes the check to a null check instead.

I see. Can you share to which version you upgraded to? I tried to upgrade to the latest version and there are lots of breaking changes, mostly around the imports.

Sorry, I only checked that the latest version of that specific broken file fixes that specific bug. As I said before, I encountered the same problem as you did when trying to upgrade the library, I could not find a version that works. I'm currently working around the problem by manually fixing the buggy file, since the fix is only changing the undefine check to a null check.

@LMHoang20 ok thanks! I think I managed to reproduce this error on an EC2 machine (running ubuntu). I'll try to investigate a bit.

@LMHoang20 Okay turns out the problem was with LiteLLM. Seems like there was a change in the response that caused the rawToolCalls to be null, instead of the undefined. Ultimately the problem is in Langchain.js that doesn't have guards against this edge case, but for now I fixed our LiteLLM version to 1.41.3. I merged this PR.

If you can try and verify the fix in your local machine, that'd be great :) make sure the new LiteLLM image is used (main-v1.41.3 instead of main-latest).

merged a fix