{"record":{"id":"0a3e17bbbb843735","repo":"n8n-io/n8n","slug":"provided-message-is-not-a-valid-langchain-message","errorCode":null,"errorMessage":"Provided message is not a valid Langchain message: ${JSON.stringify(msg)}","messagePattern":"Provided message is not a valid Langchain message: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/ai-utilities/src/converters/message.ts","lineNumber":253,"sourceCode":"\t\t};\n\t}\n\tif (LangchainMessages.HumanMessage.isInstance(msg)) {\n\t\treturn {\n\t\t\trole: 'user',\n\t\t\tcontent: fromLcContent(msg.content),\n\t\t\tid: msg.id,\n\t\t\tname: msg.name,\n\t\t};\n\t}\n\tif (LangchainMessages.BaseMessage.isInstance(msg)) {\n\t\treturn {\n\t\t\trole: fromLcRole(msg.type),\n\t\t\tcontent: fromLcContent(msg.content),\n\t\t\tid: msg.id,\n\t\t\tname: msg.name,\n\t\t};\n\t}\n\tthrow new Error(`Provided message is not a valid Langchain message: ${JSON.stringify(msg)}`);\n}\n\nexport function toLcContent(block: N8nMessages.MessageContent): LangchainMessages.ContentBlock {\n\tif (isN8nTextBlock(block)) {\n\t\treturn { type: 'text', text: block.text };\n\t}\n\tif (isN8nReasoningBlock(block)) {\n\t\treturn { type: 'reasoning', reasoning: block.text };\n\t}\n\tif (isN8nFileBlock(block)) {\n\t\tconst { url, fileId, ...rest } = block.providerMetadata ?? {};\n\t\treturn {\n\t\t\ttype: 'file',\n\t\t\tmimeType: block.mediaType ?? 'application/octet-stream',\n\t\t\tdata: block.data,\n\t\t\t...(url ? { url } : {}),\n\t\t\t...(fileId ? { fileId } : {}),\n\t\t\t...(Object.keys(rest).length > 0 ? { metadata: rest } : {}),","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/ai-utilities/src/converters/message.ts#L235-L271","documentation":"In the message converter, after checking for AIMessage, SystemMessage, HumanMessage, and the base BaseMessage (via LangchainMessages.*.isInstance), any input that is none of these is rejected. The function expects a real Langchain.js message instance; anything else (a plain object, a string, a mistyped message from another Langchain version) cannot be converted to the n8n message shape.","triggerScenarios":"Passing a plain `{ role, content }` object instead of a Langchain message instance; passing a message constructed by a different/incompatible @langchain/core version (isInstance returns false across versions); passing a string, null, or a ChatPromptValue.","commonSituations":"Multiple @langchain/core versions in node_modules (nested installs) so `instanceof`/isInstance fails across the boundary; hand-building message-like objects instead of using the Langchain constructors; version drift after an upgrade.","solutions":["Construct messages with the Langchain constructors (new HumanMessage/AIMessage/SystemMessage) from the SAME @langchain/core the converter uses.","Deduplicate @langchain/core in node_modules (pnpm why / npm ls) so isInstance matches across packages.","If converting from n8n shapes, use toLcMessage rather than passing raw objects to this function."],"exampleFix":"// before\nfromLcMessage({ role: 'user', content: 'hi' });\n\n// after\nimport { HumanMessage } from '@n8n/langchain/messages';\nfromLcMessage(new HumanMessage('hi'));","handlingStrategy":"type-guard","validationCode":"import { BaseMessage } from '@n8n/langchain/messages';\nfunction isLangchainMessage(m: unknown): m is BaseMessage {\n  return BaseMessage.isInstance(m) || m instanceof BaseMessage;\n}\nif (!isLangchainMessage(msg)) throw new Error('pass a real Langchain message instance');","typeGuard":"import { BaseMessage } from '@n8n/langchain/messages';\nfunction isLangchainMessage(m: unknown): m is BaseMessage {\n  return m instanceof BaseMessage || (typeof BaseMessage.isInstance === 'function' && BaseMessage.isInstance(m));\n}","tryCatchPattern":null,"preventionTips":["Construct messages with the Langchain constructors from the SAME @langchain/core the converter uses.","Deduplicate @langchain/core (pnpm why @langchain/core) so isInstance works across packages.","Convert n8n shapes via toLcMessage; don't hand-build Langchain-like objects."],"tags":["converters","langchain","messages","versioning"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}