{"record":{"id":"f021360e4f037a87","repo":"Mintplex-Labs/anything-llm","slug":"togetherai-chat-this-model-is-not-valid-for-ch","errorCode":null,"errorMessage":"TogetherAI chat: ${this.model} is not valid for chat completion!","messagePattern":"TogetherAI chat: (.+?) is not valid for chat completion!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/togetherAi/index.js","lineNumber":183,"sourceCode":"    attachments = [],\n  }) {\n    const prompt = {\n      role: \"system\",\n      content: `${systemPrompt}${this.#appendContext(contextTexts)}`,\n    };\n    return [\n      prompt,\n      ...chatHistory,\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        `TogetherAI 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":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/togetherAi/index.js#L165-L201","documentation":"Thrown by TogetherAiLLM.getChatCompletion when isValidChatCompletionModel(this.model) is false. Together AI's check is stricter than most: it loads the cached model catalog (via togetherAiModels(), persisted to storage/models/togetherai) and requires the id to exist AND have `type === 'chat'`. An embedding, image, or completion-only model is rejected even if the id is valid.","triggerScenarios":"this.model is missing from the cached catalog, OR present but with a type other than 'chat' (e.g. an embedding model id like 'BAAI/bge-large-en-v1.5' or a code-completion-only model). Cache is empty/stale if togetherAiModels() failed to fetch.","commonSituations":"TOGETHER_AI_MODEL_PREF set to an embedding/finetune/image model by mistake; the cached model list at storage/models/togetherai is stale after Together AI added/removed models; the catalog fetch (which hits Together's /models) failed at first run and cached nothing; copy-pasting an OpenAI id.","solutions":["Check storage/models/togetherai for the catalog and confirm the model id is present with type 'chat'; re-fetch via Together AI's /models endpoint if stale.","Switch TOGETHER_AI_MODEL_PREF to a chat model (e.g. meta-llama/Llama-3-70b-chat-hf).","Delete the stale cache so the next togetherAiModels() call repopulates it.","Re-select a chat model in the AnythingLLM UI."],"exampleFix":"// before\nconst llm = new TogetherAiLLM(embedder, \"BAAI/bge-large-en-v1.5\"); // type=embedding -> rejects\nawait llm.getChatCompletion(messages, { temperature: 0.7 });\n\n// after\nconst models = await togetherAiModels();\nconst chat = models.find((m) => m.id === \"meta-llama/Llama-3-70b-chat-hf\" && m.type === \"chat\");\nconst id = chat ? chat.id : \"meta-llama/Llama-3-70b-chat-hf\";\nconst llm2 = new TogetherAiLLM(embedder, id);\nawait llm2.getChatCompletion(messages, { temperature: 0.7 });","handlingStrategy":"validation","validationCode":"const models = await togetherAiModels();\nfunction pickTogetherChatModel(pref) {\n  const m = models.find((x) => x.id === pref && x.type === \"chat\");\n  if (m) return m.id;\n  const any = models.find((x) => x.type === \"chat\");\n  if (any) return any.id;\n  throw new Error(\"Together AI catalog has no chat models — refresh storage/models/togetherai\");\n}\nconst id = pickTogetherChatModel(process.env.TOGETHER_AI_MODEL_PREF);\nconst llm = new TogetherAiLLM(embedder, id);","typeGuard":"function isTogetherChatModel(id, models) {\n  const m = Array.isArray(models) ? models.find((x) => x.id === id) : null;\n  return !!m && m.type === \"chat\";\n}","tryCatchPattern":"try {\n  return await llm.getChatCompletion(messages, opts);\n} catch (e) {\n  if (/not valid for chat completion/i.test(e.message)) {\n    const models = await togetherAiModels();\n    const chat = models.find((m) => m.type === \"chat\");\n    if (!chat) throw e;\n    llm.model = chat.id;\n    return llm.getChatCompletion(messages, opts);\n  }\n  throw e;\n}","preventionTips":["Together AI requires type === 'chat' — never configure an embedding/image model id for chat.","Gate the UI model picker to chat-typed entries from the cached catalog.","Refresh storage/models/togetherai when Together AI adds/retires models.","Unit-test isValidChatCompletionModel against a known chat id and a known non-chat id."],"tags":["validation","model-selection","llm-provider","togetherai","chat-completion"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}