{"record":{"id":"8d1da1d1bc253ddf","repo":"FlowiseAI/Flowise","slug":"custom-function-must-return-an-object","errorCode":null,"errorMessage":"Custom function must return an object!","messagePattern":"Custom function must return an object!","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/nodes/sequentialagents/CustomFunction/CustomFunction.ts","lineNumber":173,"sourceCode":"\n            // Create additional sandbox variables\n            const additionalSandbox: ICommonObject = {}\n\n            // Add input variables to sandbox\n            if (Object.keys(inputVars).length) {\n                for (const item in inputVars) {\n                    additionalSandbox[`$${item}`] = inputVars[item]\n                }\n            }\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","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/nodes/sequentialagents/CustomFunction/CustomFunction.ts#L155-L191","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (returnValueAs = 'stateObj')\nreturn JSON.stringify({ count: n })\n\n// after\nreturn { count: n }","handlingStrategy":"type-guard","validationCode":"if (returnValueAs === 'stateObj') {\n  const r = await executeJavaScriptCode(javascriptFunction, sandbox)\n  if (r === null || typeof r !== 'object' || Array.isArray(r)) {\n    throw new Error('Custom function must return a plain object when returnValueAs is stateObj')\n  }\n}","typeGuard":"const isPlainObject = (x: any): x is Record<string, unknown> =>\n  typeof x === 'object' && x !== null && !Array.isArray(x)","tryCatchPattern":null,"preventionTips":["Keep returnValueAs and the function's return type in sync — change them together.","Return object literals explicitly; never JSON.stringify when stateObj is expected."],"tags":["custom-function","return-type","state","runtime"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}