n8n-io/n8n · error · NodeOperationError

Invalid model index ${modelIndex}

Error message

Invalid model index ${modelIndex}

What it means

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.

Source

Thrown at packages/@n8n/nodes-langchain/nodes/ModelSelector/ModelSelector.node.ts:183

			});
		}
		models.reverse();

		const rules = this.getNodeParameter('rules.rule', itemIndex, []) as ModeleSelectionRule[];

		if (!rules || rules.length === 0) {
			throw new NodeOperationError(this.getNode(), 'No rules defined', {
				itemIndex,
				description: 'At least one rule must be defined to select a model',
			});
		}

		for (let i = 0; i < rules.length; i++) {
			const rule = rules[i];
			const modelIndex = rule.modelIndex;

			if (modelIndex <= 0 || modelIndex > models.length) {
				throw new NodeOperationError(this.getNode(), `Invalid model index ${modelIndex}`, {
					itemIndex,
					description: `Model index must be between 1 and ${models.length}`,
				});
			}

			const conditionsMet = this.getNodeParameter(`rules.rule[${i}].conditions`, itemIndex, false, {
				extractValue: true,
			}) as boolean;

			if (conditionsMet) {
				const selectedModel = models[modelIndex - 1] as BaseChatModel;

				const originalCallbacks = getCallbacksArray(selectedModel.callbacks);

				for (const currentCallback of originalCallbacks) {
					if (currentCallback instanceof N8nLlmTracing) {
						currentCallback.setParentRunIndex(this.getNextRunIndex());
					}

View on GitHub (pinned to 5ac6606e81)

Solutions

  1. Connect enough models to cover the highest modelIndex referenced by any rule.
  2. Edit each rule's modelIndex to fall within 1..models.length.
  3. After adding/removing a model, re-open each rule and re-pick the model to refresh its index.

Example fix

// before: 2 models connected, rule.modelIndex = 3
// after: connect a 3rd model, or set rule.modelIndex = 2
Defensive patterns

Strategy: validation

Validate before calling

for (const r of rules) {
  if (r.modelIndex <= 0 || r.modelIndex > models.length) {
    throw new Error(`Rule modelIndex ${r.modelIndex} out of range 1..${models.length}`);
  }
}

Type guard

function isValidModelIndex(rule, models): boolean {
  return Number.isInteger(rule.modelIndex) && rule.modelIndex >= 1 && rule.modelIndex <= models.length;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12). Data as JSON: /api/errors/7d81617a29290020. Report an issue: GitHub.