FlowiseAI/Flowise · error · Error

Custom function must return a string!

Error message

Custom function must return a string!

What it means

When returnValueAs is NOT 'stateObj' (i.e. a message type — humanMessage or the default aiMessage), the Custom Function's return value is wrapped as message content and must be a string. The guard rejects any non-string response.

Source

Thrown at packages/components/nodes/sequentialagents/CustomFunction/CustomFunction.ts:182

            }

            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
                                }
                            })
                        ]
                    }
                }

                return {
                    messages: [
                        new AIMessage({

View on GitHub (pinned to abe4a8601a)

Solutions

  1. Stringify the value: return typeof x === 'string' ? x : JSON.stringify(x).
  2. Or switch returnValueAs to 'stateObj' if the function genuinely returns an object.

Example fix

// before (returnValueAs = 'humanMessage'/'aiMessage')
return { score: 42 }

// after
return JSON.stringify({ score: 42 })
Defensive patterns

Strategy: type-guard

Validate before calling

if (returnValueAs !== 'stateObj') {
  const r = await executeJavaScriptCode(javascriptFunction, sandbox)
  if (typeof r !== 'string') throw new Error('Custom function must return a string when returnValueAs is a message type')
}

Type guard

const isStringReturn = (x: any): x is string => typeof x === 'string'

Prevention

When it happens

Trigger: returnValueAs is a message type but the sandboxed JS returns a number, object, array, boolean, or undefined.

Common situations: Function returns an object while returnValueAs is still on a message type; function returns a numeric result from a computation; returnValueAs and the return shape drifted out of sync.

Related errors


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