{"record":{"id":"4b1195d8c4d28516","repo":"Mintplex-Labs/anything-llm","slug":"apipie-chat-this-model-is-not-valid-for-chat-c","errorCode":null,"errorMessage":"ApiPie chat: ${this.model} is not valid for chat completion!","messagePattern":"ApiPie chat: (.+?) is not valid for chat completion!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/apipie/index.js","lineNumber":192,"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        `ApiPie 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":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/apipie/index.js#L174-L210","documentation":"Thrown by ApiPieLLM.getChatCompletion when `await this.isValidChatCompletionModel(this.model)` returns false, i.e. the configured model id is not in ApiPie's accepted chat-model list. It is a deterministic pre-flight check before any network call to apipie.ai, so no request is wasted on an unsupported model.","triggerScenarios":"Calling getChatCompletion with this.model set to something isValidChatCompletionModel rejects — e.g. an embedding-only model id, a typo, a model id from a different provider namespace, or a model ApiPie delisted. this.model comes from modelPreference arg, else APIPIE_LLM_MODEL_PREF, else the hardcoded default.","commonSituations":"User typed a custom model id that isn't a chat model; ApiPie renamed/removed a model and the saved preference is stale; copied a model id from another provider's docs; default fallback changed but env still points at an old id.","solutions":["Set APIPIE_LLM_MODEL_PREF (or pass modelPreference) to a model id that isValidChatCompletionModel accepts — check the provider's current chat-capable model list.","If using a custom model id in the UI, verify it against ApiPie's /v1/models endpoint before saving.","Clear a stale saved preference and let it fall back to the default, then re-pick a supported model.","Update the AnythingLLM provider file if ApiPie's valid-model list changed and isValidChatCompletionModel needs refreshing."],"exampleFix":"// before\n// .env\nAPIPIE_LLM_MODEL_PREF=some-embedding-model-001\n\n// after\n// .env\nAPIPIE_LLM_MODEL_PREF=openrouter/mistral-7b-instruct","handlingStrategy":"validation","validationCode":"// Validate before calling getChatCompletion\nconst valid = await llm.isValidChatCompletionModel(llm.model);\nif (!valid) {\n  throw new Error(\n    `Model '${llm.model}' is not a valid ApiPie chat model. Set APIPIE_LLM_MODEL_PREF to a supported id.`\n  );\n}\nconst res = await llm.getChatCompletion(messages, { temperature });","typeGuard":"/**\n * @param {ApiPieLLM} llm\n * @param {string} model\n * @returns {Promise<boolean>}\n */\nasync function isValidApiPieChatModel(llm, model) {\n  return typeof model === \"string\" && model.length > 0 &&\n    (await llm.isValidChatCompletionModel(model));\n}","tryCatchPattern":"try {\n  const res = await llm.getChatCompletion(messages, { temperature });\n} catch (e) {\n  if (/is not valid for chat completion/.test(e.message)) {\n    return pickFallbackModelAndRetry();\n  }\n  throw e;\n}","preventionTips":["Fetch the provider's current model list on config save and validate the chosen id.","Treat model preference as refreshable state; re-validate periodically against the live catalog.","Provide an admin view of accepted chat models so users can't type arbitrary ids.","Unit-test isValidChatCompletionModel against a representative set of chat vs non-chat ids."],"tags":["config","model-validation","apipie","precondition"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}