{"record":{"id":"f234e32b7a5b429f","repo":"Mintplex-Labs/anything-llm","slug":"cometapi-chat-this-model-is-not-valid-for-chat","errorCode":null,"errorMessage":"CometAPI chat: ${this.model} is not valid for chat completion!","messagePattern":"CometAPI chat: (.+?) is not valid for chat completion!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/cometapi/index.js","lineNumber":198,"sourceCode":"    attachments = [],\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 (!(await this.isValidChatCompletionModel(this.model)))\n      throw new Error(\n        `CometAPI 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":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/cometapi/index.js#L180-L216","documentation":"Thrown by getChatCompletion when isValidChatCompletionModel returns false. That method syncs the local model cache (storage/models/cometapi/models.json) from the CometAPI /models endpoint, then checks whether this.model is a key in that cached object. If the model id is absent — stale cache, never-synced cache, or an invalid/deprecated model — the chat call is blocked before any API request is made.","triggerScenarios":"COMETAPI_LLM_MODEL_PREF is set to a model id that CometAPI no longer lists; the models.json cache file does not exist (first run before sync) or is corrupt; the sync fetch failed silently leaving an empty cache; the model id has trailing whitespace or different casing than the catalog.","commonSituations":"First-ever CometAPI request on a fresh install where the model cache has not been populated; CometAPI retiring a model and the saved preference still points to it; a typo in the model preference env var; the storage directory is read-only so the cache write fails.","solutions":["Call the CometAPI /v1/models endpoint with the API key to confirm the model id is currently listed and matches exactly (including casing).","Delete storage/models/cometapi/models.json and the .cached_at file to force a fresh sync, then retry.","Update COMETAPI_LLM_MODEL_PREF (or the workspace model selection) to a currently valid CometAPI model id.","Verify the storage directory is writable so the model cache can be persisted after sync."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const llm = new CometApiLLM(embedder, modelPref);\nconst isValid = await llm.isValidChatCompletionModel(llm.model);\nif (!isValid)\n  throw new Error(`Model \"${llm.model}\" is not in the CometAPI catalog. Sync the cache or pick a valid id.`);","typeGuard":null,"tryCatchPattern":"try {\n  return await llm.getChatCompletion(messages, { temperature });\n} catch (e) {\n  if (/is not valid for chat completion/i.test(e.message)) {\n    // force cache refresh and retry once\n    fs.unlinkSync(llm.cacheModelPath);\n    await llm.isValidChatCompletionModel(llm.model); // triggers resync\n    return await llm.getChatCompletion(messages, { temperature });\n  }\n  throw e;\n}","preventionTips":["After selecting a CometAPI model, trigger a model cache sync before the first chat request.","Ensure the storage/models/cometapi directory is writable so the cache persists.","Validate the model id against the CometAPI /v1/models endpoint when saving provider settings."],"tags":["cometapi","invalid-model","chat-completion","model-cache","validation"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}