{"record":{"id":"37a905ac4a333bc1","repo":"FlowiseAI/Flowise","slug":"condition-function-must-return-a-string","errorCode":null,"errorMessage":"Condition function must return a string","messagePattern":"Condition function must return a string","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/components/nodes/sequentialagents/Condition/Condition.ts","lineNumber":287,"sourceCode":"    const selectedTab = tabIdentifier ? tabIdentifier.split(`_${nodeData.id}`)[0] : 'conditionUI'\n    const variables = await getVars(appDataSource, databaseEntities, nodeData, options)\n\n    const flow = {\n        chatflowId: options.chatflowid,\n        sessionId: options.sessionId,\n        chatId: options.chatId,\n        input,\n        state,\n        vars: prepareSandboxVars(variables)\n    }\n\n    if (selectedTab === 'conditionFunction' && conditionFunction) {\n        const sandbox = createCodeExecutionSandbox(input, variables, flow)\n\n        try {\n            const response = await executeJavaScriptCode(conditionFunction, sandbox)\n\n            if (typeof response !== 'string') throw new Error('Condition function must return a string')\n            return response\n        } catch (e) {\n            throw new Error(e)\n        }\n    } else if (selectedTab === 'conditionUI' && conditionUI) {\n        try {\n            const conditionItems: IConditionGridItem[] = typeof conditionUI === 'string' ? JSON.parse(conditionUI) : conditionUI\n\n            for (const item of conditionItems) {\n                if (!item.variable) throw new Error('Condition variable is required!')\n\n                if (item.variable.startsWith('$flow')) {\n                    const variableValue = customGet(flow, item.variable.replace('$flow.', ''))\n                    if (checkCondition(variableValue, item.operation, item.value)) {\n                        return item.output\n                    }\n                } else if (item.variable.startsWith('$vars')) {\n                    const variableValue = customGet(flow, item.variable.replace('$', ''))","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/FlowiseAI/Flowise/blob/abe4a8601a058047b350c260676826e21dd14101/packages/components/nodes/sequentialagents/Condition/Condition.ts#L269-L305","documentation":"On the `conditionFunction` branch, user JS executes in a sandbox and its return value is used to select the next graph branch — so it must be a string matching one of the Condition's output labels. `typeof response !== 'string'` rejects numbers, booleans, objects, and undefined before they reach the graph router.","triggerScenarios":"The condition function returns a boolean (e.g. the raw comparison result), a number, an object, or omits `return` (undefined).","commonSituations":"User wrote `return age > 18` expecting it to branch, but the router needs a label; the function returns the variable itself instead of an output label; the function returns the result of a comparison operator (boolean).","solutions":["Return one of the configured Condition output labels as a literal string — e.g. `return age > 18 ? 'Approved' : 'End'`.","If the value is dynamic, coerce with `String(response)` and ensure it matches an output label.","Verify the function always hits a `return` on every code path."],"exampleFix":"// before\nconditionFunction = \"return age > 18\" // returns boolean -> throws [292]\n\n// after\nconditionFunction = \"return age > 18 ? 'Approved' : 'End'\"","handlingStrategy":"type-guard","validationCode":"function assertConditionFunctionResult(response) {\n  if (typeof response !== 'string') {\n    throw new Error(`Condition function must return a string label, got ${typeof response}: ${JSON.stringify(response)}`)\n  }\n  return response\n}\n\nconst result = await executeJavaScriptCode(conditionFunction, sandbox)\nassertConditionFunctionResult(result)","typeGuard":"function isConditionResult(v): v is string {\n  return typeof v === 'string' && v.length > 0\n}","tryCatchPattern":null,"preventionTips":["Always return one of the configured output labels as a string literal.","Avoid returning raw booleans from comparisons — map them to labels.","Confirm every code path ends in a `return`."],"tags":["condition","code-sandbox","validation","configuration"],"backgroundTag":null,"analyzedSha":"abe4a8601a058047b350c260676826e21dd14101","analyzedAt":"2026-08-12T16:04:40.823Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}