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.id

View on GitHub (pinned to abe4a8601a)

Solutions

  1. Make every code path return an object literal, e.g. return { summary: result }.
  2. If the value is a JSON string, parse it first: return JSON.parse(str).
  3. 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

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


AI-assisted analysis of FlowiseAI/Flowise@abe4a8601a (2026-08-12). Data as JSON: /api/errors/8d1da1d1bc253ddf. Report an issue: GitHub.