Zie619/n8n-workflows · error · Error

'llm_index' is udefined or not a valid integer

Error message

'llm_index' is udefined or not a valid integer

What it means

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.

Source

Thrown at workflows/Noop/1838_Noop_Stickynote_Automation_Triggered.json:61

      "webhookId": "713a7f98-0e3d-4eb7-aafa-599ca627c8b4",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1,
      "notes": "This chatTrigger node performs automated tasks as part of the workflow."
    },
    {
      "id": "6fc4f336-09e3-4e79-94e9-e5eff04e4089",
      "name": "Switch Model",
      "type": "n8n-nodes-base.noOp",
      "position": [
        540,
        320
      ],
      "parameters": {
        "code": {
          "supplyData": {
            "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];"
          }
        },
        "inputs": {
          "input": [
            {
              "type": "ai_languageModel",
              "required": true
            }
          ]
        },
        "outputs": {
          "output": [
            {
              "type": "ai_languageModel"
            }
          ]
        }
      },

View on GitHub (pinned to 94007c1445)

Solutions

  1. 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.
  2. Coerce and default before validating (see exampleFix).
  3. If callers send strings, convert: const llm_index = Number($input.item.json.llm_index).
  4. Confirm llms.length after reverse() so a valid index cannot also hit the 'No LLM found with index' branch.

Example fix

// before
const llm_index = $input.item.json.llm_index;
if (!Number.isInteger(llm_index)) {
  throw new Error("'llm_index' is udefined or not a valid integer");
}

// after
const raw = $input.item.json.llm_index;
const llm_index = Number.isInteger(raw) ? raw : Number.parseInt(raw, 10);
if (!Number.isInteger(llm_index) || llm_index < 0) {
  throw new Error(`'llm_index' is undefined or not a valid integer (got: ${JSON.stringify(raw)})`);
}
Defensive patterns

Strategy: validation

Validate before calling

const raw = $input.item.json.llm_index;
const llm_index = Number.isInteger(raw) ? raw : Number.parseInt(raw, 10);
if (!Number.isInteger(llm_index) || llm_index < 0) {
  throw new Error(`'llm_index' missing/invalid (got ${JSON.stringify(raw)})`);
}

Type guard

function isLlmIndex(v) {
  return Number.isInteger(v) || /^\d+$/.test(String(v ?? ''));
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of Zie619/n8n-workflows@94007c1445 (2026-08-15). Data as JSON: /api/errors/e77240446d93772d. Report an issue: GitHub.