{"record":{"id":"5529b43d73255823","repo":"FlowiseAI/Flowise","slug":"returned-message-history-must-be-an-array","errorCode":null,"errorMessage":"Returned message history must be an array","messagePattern":"Returned message history must be an array","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/nodes/prompts/ChatPromptTemplate/ChatPromptTemplate.ts","lineNumber":139,"sourceCode":"            const databaseEntities = options.databaseEntities as IDatabaseEntity\n            const variables = await getVars(appDataSource, databaseEntities, nodeData, options)\n            const flow = {\n                chatflowId: options.chatflowid,\n                sessionId: options.sessionId,\n                chatId: options.chatId\n            }\n\n            const sandbox = createCodeExecutionSandbox('', variables, flow)\n\n            try {\n                const response = await executeJavaScriptCode(messageHistoryCode, sandbox, {\n                    libraries: ['axios', '@langchain/core']\n                })\n\n                const parsedResponse = JSON.parse(response)\n\n                if (!Array.isArray(parsedResponse)) {\n                    throw new Error('Returned message history must be an array')\n                }\n                prompt = ChatPromptTemplate.fromMessages([\n                    SystemMessagePromptTemplate.fromTemplate(systemMessagePrompt),\n                    ...parsedResponse,\n                    HumanMessagePromptTemplate.fromTemplate(humanMessagePrompt)\n                ])\n            } catch (e) {\n                throw new Error(e)\n            }\n        }\n\n        let promptValues: ICommonObject = {}\n        if (promptValuesStr) {\n            try {\n                promptValues = typeof promptValuesStr === 'object' ? promptValuesStr : JSON.parse(promptValuesStr)\n            } catch (exception) {\n                throw new Error(\"Invalid JSON in the ChatPromptTemplate's promptValues: \" + exception)\n            }","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/nodes/prompts/ChatPromptTemplate/ChatPromptTemplate.ts#L121-L157","documentation":"ChatPromptTemplate.init executes a user-supplied `messageHistoryCode` snippet in a sandbox, JSON.parses the response, and verifies it is an array. If the code returns a JSON object, primitive, or string-encoded non-array, the Array.isArray check fails and throws.","triggerScenarios":"The messageHistoryCode returns `{messages: [...]}` instead of `[...]`; returns a JSON string of an object; returns null/undefined/empty string; returns a single message object not wrapped in an array.","commonSituations":"Authoring custom message-history fetching code (e.g. querying a DB or API) and forgetting to extract/unwrap the array; the upstream API changed its response shape.","solutions":["Ensure messageHistoryCode returns a JSON array literal: `[{role:'user',content:'...'}, ...]`.","If fetching from an API, map/extract the array before returning.","JSON.stringify the value inside the code to confirm shape.","Wrap the response: `return JSON.stringify(Array.isArray(x) ? x : [x])`."],"exampleFix":"// before\nconst response = await executeJavaScriptCode(messageHistoryCode, sandbox, {...})\nconst parsedResponse = JSON.parse(response)\nif (!Array.isArray(parsedResponse)) throw new Error('Returned message history must be an array')\n// after\nconst response = await executeJavaScriptCode(messageHistoryCode, sandbox, {...})\nconst parsedResponse = JSON.parse(response)\nconst arr = Array.isArray(parsedResponse) ? parsedResponse : (parsedResponse?.messages ?? [parsedResponse])\nif (!Array.isArray(arr)) throw new Error('Returned message history must be an array')","handlingStrategy":"validation","validationCode":"const arr = Array.isArray(parsedResponse) ? parsedResponse\n  : Array.isArray((parsedResponse as any)?.messages) ? (parsedResponse as any).messages\n  : [parsedResponse]\nif (!Array.isArray(arr)) throw new Error('Returned message history must be an array')","typeGuard":"const isMessageArray = (v: unknown): boolean => Array.isArray(v) && v.every((m) => m && typeof m === 'object' && 'role' in m)","tryCatchPattern":null,"preventionTips":["Return a bare JSON array from messageHistoryCode.","Unwrap nested `messages` keys before returning.","JSON.stringify inside the sandbox to confirm shape."],"tags":["json","prompt","chat-history","validation","sandbox"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}