{"record":{"id":"86c6d236a49d4589","repo":"n8n-io/n8n","slug":"failed-to-convert-to-langchain-content-block-js","errorCode":null,"errorMessage":"Failed to convert to Langchain content block: ${JSON.stringify(block)}","messagePattern":"Failed to convert to Langchain content block: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/ai-utilities/src/converters/message.ts","lineNumber":317,"sourceCode":"\t}\n\tif (isN8nCitationBlock(block)) {\n\t\treturn {\n\t\t\ttype: 'citation',\n\t\t\tsource: block.source,\n\t\t\turl: block.url,\n\t\t\ttitle: block.title,\n\t\t\tstartIndex: block.startIndex,\n\t\t\tendIndex: block.endIndex,\n\t\t\tcitedText: block.text,\n\t\t} as unknown as LangchainMessages.ContentBlock;\n\t}\n\tif (isN8nProviderBlock(block)) {\n\t\treturn {\n\t\t\ttype: 'non_standard',\n\t\t\tvalue: block.value,\n\t\t} as LangchainMessages.ContentBlock.NonStandard;\n\t}\n\tthrow new Error(`Failed to convert to Langchain content block: ${JSON.stringify(block)}`);\n}\n\nexport function toLcMessage(message: Message): LangchainMessages.BaseMessage {\n\tconst lcContent = message.content.map(toLcContent);\n\n\tswitch (message.role) {\n\t\tcase 'system':\n\t\t\treturn new LangchainMessages.SystemMessage({\n\t\t\t\tcontent: lcContent,\n\t\t\t\tid: message.id,\n\t\t\t\tname: message.name,\n\t\t\t});\n\t\tcase 'user':\n\t\t\treturn new LangchainMessages.HumanMessage({\n\t\t\t\tcontent: lcContent,\n\t\t\t\tid: message.id,\n\t\t\t\tname: message.name,\n\t\t\t});","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/ai-utilities/src/converters/message.ts#L299-L335","documentation":"toLcContent() maps an n8n content block to a Langchain content block by checking block types in turn (text, reasoning, file, tool_call, invalid_tool_call, tool_result, citation, provider/non_standard). If none match, the block is of an unknown/unsupported type and cannot be represented in the Langchain content model, so it throws with the serialized block for diagnosis.","triggerScenarios":"Passing a content block missing a recognized discriminator (e.g. `{ type: 'something_new' }`); a malformed block where the type guard predicates all return false; a newly added n8n block type that this converter has not been taught to handle.","commonSituations":"A provider returned a block shape the converter doesn't know yet (version lag); a tool/handler emitted an ad-hoc block object; schema drift between n8n message types and the converter.","solutions":["Inspect the serialized block in the error to see its shape, then construct it using a supported type guard (text/reasoning/file/tool_*).","If it's a genuinely new block type, add a branch + isN8nXBlock predicate to toLcContent in message.ts.","Sanitize upstream: drop or coerce unknown blocks before conversion."],"exampleFix":"// before\n{ type: 'mystery', payload: {...} }\n\n// after (coerce to text or drop)\n{ type: 'text', text: JSON.stringify(rawBlock) }","handlingStrategy":"type-guard","validationCode":"import { isN8nTextBlock, isN8nReasoningBlock, isN8nFileBlock } from '@n8n/ai-utilities';\nfunction isKnownBlock(b: unknown): boolean {\n  return isN8nTextBlock(b) || isN8nReasoningBlock(b) || isN8nFileBlock(b) /* or other supported guards */;\n}\ncontent.filter(isKnownBlock).map(toLcContent);","typeGuard":"function isSupportedBlock(b: unknown): boolean {\n  return (\n    typeof b === 'object' && b !== null && 'type' in b &&\n    ['text','reasoning','file','tool_call','invalid_tool_call','tool_result','citation','non_standard'].includes((b as { type: string }).type)\n  );\n}","tryCatchPattern":null,"preventionTips":["Filter content through known type guards before converting.","When adding a new n8n block type, add a branch + predicate to toLcContent the same commit.","Log unknown block types so converter gaps surface in dev."],"tags":["converters","langchain","content-blocks","messages"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}