{"record":{"id":"e77240446d93772d","repo":"Zie619/n8n-workflows","slug":"llm-index-is-udefined-or-not-a-valid-integer","errorCode":null,"errorMessage":"'llm_index' is udefined or not a valid integer","messagePattern":"'llm_index' is udefined or not a valid integer","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"workflows/Noop/1838_Noop_Stickynote_Automation_Triggered.json","lineNumber":61,"sourceCode":"      \"webhookId\": \"713a7f98-0e3d-4eb7-aafa-599ca627c8b4\",\n      \"parameters\": {\n        \"options\": {}\n      },\n      \"typeVersion\": 1.1,\n      \"notes\": \"This chatTrigger node performs automated tasks as part of the workflow.\"\n    },\n    {\n      \"id\": \"6fc4f336-09e3-4e79-94e9-e5eff04e4089\",\n      \"name\": \"Switch Model\",\n      \"type\": \"n8n-nodes-base.noOp\",\n      \"position\": [\n        540,\n        320\n      ],\n      \"parameters\": {\n        \"code\": {\n          \"supplyData\": {\n            \"code\": \"let llms = await this.getInputConnectionData('ai_languageModel', 0);\\nllms.reverse(); // reverse array, so the order matches the UI elements\\n\\nconst llm_index = $input.item.json.llm_index;\\nif (!Number.isInteger(llm_index)) {\\n  console.log(\\\"'llm_index' is udefined or not a valid integer\\\");\\n  throw new Error(\\\"'llm_index' is udefined or not a valid integer\\\");\\n}\\n\\nif(typeof llms[llm_index] === 'undefined') {\\n  console.log(`No LLM found with index ${llm_index}`);\\n  throw new Error(`No LLM found with index ${llm_index}`);\\n}\\n\\nreturn llms[llm_index];\"\n          }\n        },\n        \"inputs\": {\n          \"input\": [\n            {\n              \"type\": \"ai_languageModel\",\n              \"required\": true\n            }\n          ]\n        },\n        \"outputs\": {\n          \"output\": [\n            {\n              \"type\": \"ai_languageModel\"\n            }\n          ]\n        }\n      },","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/Zie619/n8n-workflows/blob/94007c1445d9258a7da116646b79473e7c7c3282/workflows/Noop/1838_Noop_Stickynote_Automation_Triggered.json#L43-L79","documentation":"Thrown by the 'Switch Model' noOp supplyData code (workflow 1838). It reads llm_index from $input.item.json and throws when Number.isInteger(llm_index) is false — undefined, a string like '1', null, or NaN all fail. In a supplyData (AI model selector) node this runs every time the connected agent requests its language model.","triggerScenarios":"The agent chain invokes the model selector with an incoming item that has no llm_index field, or carries it as a string (e.g. from a webhook query param). Number.isInteger('1') is false, so a numeric-looking string still throws. If llm_index is absent entirely, undefined fails the check.","commonSituations":"Webhook trigger where callers pass llm_index as a string query parameter; workflows where the item that reaches the agent lacks the field because a mid-chain node (Set/Merge) dropped it; multiple LLMs wired into the selector without any producer of llm_index.","solutions":["Trace which item reaches the agent and ensure a llm_index integer is present — add a Set node producing Number($(\"llm_index\")) before the chain.","Coerce and default before validating (see exampleFix).","If callers send strings, convert: const llm_index = Number($input.item.json.llm_index).","Confirm llms.length after reverse() so a valid index cannot also hit the 'No LLM found with index' branch."],"exampleFix":"// before\nconst llm_index = $input.item.json.llm_index;\nif (!Number.isInteger(llm_index)) {\n  throw new Error(\"'llm_index' is udefined or not a valid integer\");\n}\n\n// after\nconst raw = $input.item.json.llm_index;\nconst llm_index = Number.isInteger(raw) ? raw : Number.parseInt(raw, 10);\nif (!Number.isInteger(llm_index) || llm_index < 0) {\n  throw new Error(`'llm_index' is undefined or not a valid integer (got: ${JSON.stringify(raw)})`);\n}","handlingStrategy":"validation","validationCode":"const raw = $input.item.json.llm_index;\nconst llm_index = Number.isInteger(raw) ? raw : Number.parseInt(raw, 10);\nif (!Number.isInteger(llm_index) || llm_index < 0) {\n  throw new Error(`'llm_index' missing/invalid (got ${JSON.stringify(raw)})`);\n}","typeGuard":"function isLlmIndex(v) {\n  return Number.isInteger(v) || /^\\d+$/.test(String(v ?? ''));\n}","tryCatchPattern":null,"preventionTips":["Coerce webhook/query inputs with Number.parseInt before Number.isInteger checks.","Guarantee a producer of llm_index (Set node) upstream of the agent chain.","Bound-check the index against llms.length so valid-looking indices cannot hit the next throw."],"tags":["n8n","langchain","ai-agent","model-routing","input-validation"],"backgroundTag":null,"analyzedSha":"94007c1445d9258a7da116646b79473e7c7c3282","analyzedAt":"2026-08-15T04:10:37.591Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}