{"record":{"id":"7d81617a29290020","repo":"n8n-io/n8n","slug":"invalid-model-index-modelindex","errorCode":null,"errorMessage":"Invalid model index ${modelIndex}","messagePattern":"Invalid model index (.+?)","errorType":"exception","errorClass":"NodeOperationError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/nodes-langchain/nodes/ModelSelector/ModelSelector.node.ts","lineNumber":183,"sourceCode":"\t\t\t});\n\t\t}\n\t\tmodels.reverse();\n\n\t\tconst rules = this.getNodeParameter('rules.rule', itemIndex, []) as ModeleSelectionRule[];\n\n\t\tif (!rules || rules.length === 0) {\n\t\t\tthrow new NodeOperationError(this.getNode(), 'No rules defined', {\n\t\t\t\titemIndex,\n\t\t\t\tdescription: 'At least one rule must be defined to select a model',\n\t\t\t});\n\t\t}\n\n\t\tfor (let i = 0; i < rules.length; i++) {\n\t\t\tconst rule = rules[i];\n\t\t\tconst modelIndex = rule.modelIndex;\n\n\t\t\tif (modelIndex <= 0 || modelIndex > models.length) {\n\t\t\t\tthrow new NodeOperationError(this.getNode(), `Invalid model index ${modelIndex}`, {\n\t\t\t\t\titemIndex,\n\t\t\t\t\tdescription: `Model index must be between 1 and ${models.length}`,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst conditionsMet = this.getNodeParameter(`rules.rule[${i}].conditions`, itemIndex, false, {\n\t\t\t\textractValue: true,\n\t\t\t}) as boolean;\n\n\t\t\tif (conditionsMet) {\n\t\t\t\tconst selectedModel = models[modelIndex - 1] as BaseChatModel;\n\n\t\t\t\tconst originalCallbacks = getCallbacksArray(selectedModel.callbacks);\n\n\t\t\t\tfor (const currentCallback of originalCallbacks) {\n\t\t\t\t\tif (currentCallback instanceof N8nLlmTracing) {\n\t\t\t\t\t\tcurrentCallback.setParentRunIndex(this.getNextRunIndex());\n\t\t\t\t\t}","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/nodes-langchain/nodes/ModelSelector/ModelSelector.node.ts#L165-L201","documentation":"Thrown by ModelSelector.supplyData() while iterating rules when a rule's modelIndex is out of the valid 1..models.length range. modelIndex is 1-based and must reference an actually connected model; otherwise the rule cannot be honored.","triggerScenarios":"For each rule, `if (modelIndex <= 0 || modelIndex > models.length)` throws with the offending value and a description stating the valid range. Fires when a rule references a model slot that does not exist (e.g. modelIndex 3 when only 2 models are connected) or uses 0/negative.","commonSituations":"Removing a connected model without updating rules that referenced it; hand-edited workflow JSON with wrong modelIndex; off-by-one confusion (treating modelIndex as 0-based).","solutions":["Connect enough models to cover the highest modelIndex referenced by any rule.","Edit each rule's modelIndex to fall within 1..models.length.","After adding/removing a model, re-open each rule and re-pick the model to refresh its index."],"exampleFix":"// before: 2 models connected, rule.modelIndex = 3\n// after: connect a 3rd model, or set rule.modelIndex = 2","handlingStrategy":"validation","validationCode":"for (const r of rules) {\n  if (r.modelIndex <= 0 || r.modelIndex > models.length) {\n    throw new Error(`Rule modelIndex ${r.modelIndex} out of range 1..${models.length}`);\n  }\n}","typeGuard":"function isValidModelIndex(rule, models): boolean {\n  return Number.isInteger(rule.modelIndex) && rule.modelIndex >= 1 && rule.modelIndex <= models.length;\n}","tryCatchPattern":null,"preventionTips":["After adding/removing a model, refresh every rule's modelIndex.","Treat modelIndex as 1-based, never 0.","Validate rule indices against the connected model count before running."],"tags":["model-selector","rules","validation","off-by-one"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}