{"record":{"id":"7855e8773127e57b","repo":"Mintplex-Labs/anything-llm","slug":"xai-chat-this-model-is-not-valid-for-chat-comp","errorCode":null,"errorMessage":"xAI chat: ${this.model} is not valid for chat completion!","messagePattern":"xAI chat: (.+?) is not valid for chat completion!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/xai/index.js","lineNumber":120,"sourceCode":"    attachments = [], // This is the specific attachment for only this prompt\n  }) {\n    const prompt = {\n      role: \"system\",\n      content: `${systemPrompt}${this.#appendContext(contextTexts)}`,\n    };\n    return [\n      prompt,\n      ...formatChatHistory(chatHistory, this.#generateContent),\n      {\n        role: \"user\",\n        content: this.#generateContent({ userPrompt, attachments }),\n      },\n    ];\n  }\n\n  async getChatCompletion(messages = null, { temperature = 0.7 }) {\n    if (!this.isValidChatCompletionModel(this.model))\n      throw new Error(\n        `xAI chat: ${this.model} is not valid for chat completion!`\n      );\n\n    const result = await LLMPerformanceMonitor.measureAsyncFunction(\n      this.openai.chat.completions\n        .create({\n          model: this.model,\n          messages,\n          temperature,\n        })\n        .catch((e) => {\n          throw new Error(e.message);\n        })\n    );\n\n    if (\n      !result.output.hasOwnProperty(\"choices\") ||\n      result.output.choices.length === 0","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/xai/index.js#L102-L138","documentation":"Thrown by XAiLLM.getChatCompletion when this.isValidChatCompletionModel(this.model) is false. NOTE: in the current implementation isValidChatCompletionModel unconditionally returns true, so this branch is effectively dead code and the throw is not reachable without overriding that method. If it ever fires, it means the guard was changed to actually validate and the configured model failed.","triggerScenarios":"Not reachable given isValidChatCompletionModel() { return true; } at xai/index.js:65. Would only fire if a subclass or future edit makes the validator reject this.model (e.g. checking MODEL_MAP for 'xai').","commonSituations":"Developers see this string in source search and assume xAI validates models; it does not. Practically the error never surfaces, so encountering it implies a custom subclass that overrode the validator.","solutions":["If you genuinely hit this, inspect whether isValidChatCompletionModel was overridden by a subclass or monkeypatch.","Set XAI_LLM_MODEL_PREF to a real grok model id (e.g. 'grok-beta', 'grok-2-latest') so any future validator passes.","If adding real validation, mirror the MODEL_MAP.get('xai', modelName) lookup used by promptWindowLimit rather than leaving an always-true stub."],"exampleFix":"// before\nisValidChatCompletionModel(_modelName = \"\") {\n  return true;\n}\n\n// after (if you actually want validation)\nisValidChatCompletionModel(modelName = \"\") {\n  return MODEL_MAP.get(\"xai\", modelName) != null;\n}","handlingStrategy":"validation","validationCode":"// Currently a no-op: isValidChatCompletionModel always returns true.\n// If you add real validation, check before calling:\nconst ok = llm.isValidChatCompletionModel(llm.model);\nif (!ok) throw new Error(`xAI model ${llm.model} rejected by validator`);","typeGuard":"/** @param {string} m */\nfunction isLikelyXaiModel(m) {\n  return typeof m === 'string' && /^grok/i.test(m);\n}","tryCatchPattern":"try {\n  await llm.getChatCompletion(messages, { temperature });\n} catch (e) {\n  if (/is not valid for chat completion/.test(e.message)) {\n    // only reachable if isValidChatCompletionModel was overridden — audit subclass\n  }\n  throw e;\n}","preventionTips":["Do not rely on this guard firing — xAI does not validate models today.","If you override isValidChatCompletionModel, mirror MODEL_MAP.get('xai', name) usage.","Set XAI_LLM_MODEL_PREF to a real grok id to be safe."],"tags":["xai","grok","model-validation","dead-code","config"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}