FlowiseAI/Flowise · error · Error
Returned message history must be an array
Error message
Returned message history must be an array
What it means
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.
Source
Thrown at packages/components/nodes/prompts/ChatPromptTemplate/ChatPromptTemplate.ts:139
const databaseEntities = options.databaseEntities as IDatabaseEntity
const variables = await getVars(appDataSource, databaseEntities, nodeData, options)
const flow = {
chatflowId: options.chatflowid,
sessionId: options.sessionId,
chatId: options.chatId
}
const sandbox = createCodeExecutionSandbox('', variables, flow)
try {
const response = await executeJavaScriptCode(messageHistoryCode, sandbox, {
libraries: ['axios', '@langchain/core']
})
const parsedResponse = JSON.parse(response)
if (!Array.isArray(parsedResponse)) {
throw new Error('Returned message history must be an array')
}
prompt = ChatPromptTemplate.fromMessages([
SystemMessagePromptTemplate.fromTemplate(systemMessagePrompt),
...parsedResponse,
HumanMessagePromptTemplate.fromTemplate(humanMessagePrompt)
])
} catch (e) {
throw new Error(e)
}
}
let promptValues: ICommonObject = {}
if (promptValuesStr) {
try {
promptValues = typeof promptValuesStr === 'object' ? promptValuesStr : JSON.parse(promptValuesStr)
} catch (exception) {
throw new Error("Invalid JSON in the ChatPromptTemplate's promptValues: " + exception)
}View on GitHub (pinned to abe4a8601a)
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])`.
Example fix
// before
const response = await executeJavaScriptCode(messageHistoryCode, sandbox, {...})
const parsedResponse = JSON.parse(response)
if (!Array.isArray(parsedResponse)) throw new Error('Returned message history must be an array')
// after
const response = await executeJavaScriptCode(messageHistoryCode, sandbox, {...})
const parsedResponse = JSON.parse(response)
const arr = Array.isArray(parsedResponse) ? parsedResponse : (parsedResponse?.messages ?? [parsedResponse])
if (!Array.isArray(arr)) throw new Error('Returned message history must be an array') Defensive patterns
Strategy: validation
Validate before calling
const arr = Array.isArray(parsedResponse) ? parsedResponse
: Array.isArray((parsedResponse as any)?.messages) ? (parsedResponse as any).messages
: [parsedResponse]
if (!Array.isArray(arr)) throw new Error('Returned message history must be an array') Type guard
const isMessageArray = (v: unknown): boolean => Array.isArray(v) && v.every((m) => m && typeof m === 'object' && 'role' in m)
Prevention
- Return a bare JSON array from messageHistoryCode.
- Unwrap nested `messages` keys before returning.
- JSON.stringify inside the sandbox to confirm shape.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid JSON in the Worker's Prompt Input Values: ${exceptio
- Invalid JSON in the ChatPromptTemplate's promptValues: ${exc
- Invalid JSON in the FewShotPromptTemplate's examples: ${exce
- Invalid JSON in the PromptTemplate's promptValues: ${excepti
- Invalid JSON in the PromptTemplate's promptValues: ${excepti
AI-assisted analysis of FlowiseAI/Flowise@abe4a8601a (2026-08-12).
Data as JSON: /api/errors/5529b43d73255823.
Report an issue: GitHub.