FlowiseAI/Flowise · error · Error
Custom function must return an object!
Error message
Custom function must return an object!
What it means
When returnValueAs is 'stateObj', the Custom Function's return value is spread into the agent state as {...state, ...response}, which requires response to be a plain object. The guard rejects any non-object (string, number, boolean, array, null, undefined) before the spread.
Source
Thrown at packages/components/nodes/sequentialagents/CustomFunction/CustomFunction.ts:173
// Create additional sandbox variables
const additionalSandbox: ICommonObject = {}
// Add input variables to sandbox
if (Object.keys(inputVars).length) {
for (const item in inputVars) {
additionalSandbox[`$${item}`] = inputVars[item]
}
}
const sandbox = createCodeExecutionSandbox(input, variables, flow, additionalSandbox)
try {
const response = await executeJavaScriptCode(javascriptFunction, sandbox)
if (returnValueAs === 'stateObj') {
if (typeof response !== 'object') {
throw new Error('Custom function must return an object!')
}
return {
...state,
...response
}
}
if (typeof response !== 'string') {
throw new Error('Custom function must return a string!')
}
if (returnValueAs === 'humanMessage') {
return {
messages: [
new HumanMessage({
content: response,
additional_kwargs: {
nodeId: nodeData.idView on GitHub (pinned to abe4a8601a)
Solutions
- Make every code path return an object literal, e.g. return { summary: result }.
- If the value is a JSON string, parse it first: return JSON.parse(str).
- Ensure returnValueAs matches the actual return type — switch back to a message type if the function must return text.
Example fix
// before (returnValueAs = 'stateObj')
return JSON.stringify({ count: n })
// after
return { count: n } Defensive patterns
Strategy: type-guard
Validate before calling
if (returnValueAs === 'stateObj') {
const r = await executeJavaScriptCode(javascriptFunction, sandbox)
if (r === null || typeof r !== 'object' || Array.isArray(r)) {
throw new Error('Custom function must return a plain object when returnValueAs is stateObj')
}
} Type guard
const isPlainObject = (x: any): x is Record<string, unknown> => typeof x === 'object' && x !== null && !Array.isArray(x)
Prevention
- Keep returnValueAs and the function's return type in sync — change them together.
- Return object literals explicitly; never JSON.stringify when stateObj is expected.
When it happens
Trigger: returnValueAs === 'stateObj' but the sandboxed JS returns a string, number, boolean, undefined (no/explicit empty return), or a JSON string instead of a parsed object.
Common situations: Switching returnValueAs from a message type to 'stateObj' without updating the function body; function returns JSON.stringify(obj) instead of obj; an early `return` with no operand on some code path.
Related errors
- Custom function must return a string!
- Invalid JSON in the Custom Function Input Variables: ${excep
- Return output must be an object
- ${e}
- Failed to load fs/promises. Make sure you are running in Nod
AI-assisted analysis of FlowiseAI/Flowise@abe4a8601a (2026-08-12).
Data as JSON: /api/errors/8d1da1d1bc253ddf.
Report an issue: GitHub.