{"record":{"id":"f3fea9332f7f5d53","repo":"FlowiseAI/Flowise","slug":"custom-function-must-return-a-string","errorCode":null,"errorMessage":"Custom function must return a string!","messagePattern":"Custom function must return a string!","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/nodes/sequentialagents/CustomFunction/CustomFunction.ts","lineNumber":182,"sourceCode":"            }\n\n            const sandbox = createCodeExecutionSandbox(input, variables, flow, additionalSandbox)\n\n            try {\n                const response = await executeJavaScriptCode(javascriptFunction, sandbox)\n\n                if (returnValueAs === 'stateObj') {\n                    if (typeof response !== 'object') {\n                        throw new Error('Custom function must return an object!')\n                    }\n                    return {\n                        ...state,\n                        ...response\n                    }\n                }\n\n                if (typeof response !== 'string') {\n                    throw new Error('Custom function must return a string!')\n                }\n\n                if (returnValueAs === 'humanMessage') {\n                    return {\n                        messages: [\n                            new HumanMessage({\n                                content: response,\n                                additional_kwargs: {\n                                    nodeId: nodeData.id\n                                }\n                            })\n                        ]\n                    }\n                }\n\n                return {\n                    messages: [\n                        new AIMessage({","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/nodes/sequentialagents/CustomFunction/CustomFunction.ts#L164-L200","documentation":"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.","triggerScenarios":"returnValueAs is a message type but the sandboxed JS returns a number, object, array, boolean, or undefined.","commonSituations":"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.","solutions":["Stringify the value: return typeof x === 'string' ? x : JSON.stringify(x).","Or switch returnValueAs to 'stateObj' if the function genuinely returns an object."],"exampleFix":"// before (returnValueAs = 'humanMessage'/'aiMessage')\nreturn { score: 42 }\n\n// after\nreturn JSON.stringify({ score: 42 })","handlingStrategy":"type-guard","validationCode":"if (returnValueAs !== 'stateObj') {\n  const r = await executeJavaScriptCode(javascriptFunction, sandbox)\n  if (typeof r !== 'string') throw new Error('Custom function must return a string when returnValueAs is a message type')\n}","typeGuard":"const isStringReturn = (x: any): x is string => typeof x === 'string'","tryCatchPattern":null,"preventionTips":["When the function computes a non-string, wrap it with String(...) or JSON.stringify before returning.","Align returnValueAs with the return type before testing."],"tags":["custom-function","return-type","message","runtime"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}